o ;s*bL3ã@sÄdZdZdZddlZddlZddlZddlmZejdkr4ddl m Z m Z m Z mZmZGd d „d e ƒZndd l m Z m Z mZmZe je _d d „Zee _dd„ZeeƒZdZdgZGdd„de ƒZdS)z0Yaroslav Halchenko, Serg G. Brester (aka sebres)zHCopyright (c) 2007 Yaroslav Halchenko, 2015 Serg G. Brester (aka sebres)ÚGPLéNé)Ú getLogger)é)Ú ConfigParserÚBasicInterpolationÚInterpolationMissingOptionErrorÚ NoOptionErrorÚNoSectionErrorcs eZdZdZ‡fdd„Z‡ZS)ÚBasicInterpolationWithNameaDecorator to bring __name__ interpolation back. Original handling of __name__ was removed because of functional deficiencies: http://bugs.python.org/issue10489 commit v3.2a4-105-g61f2761 Author: Lukasz Langa Date: Sun Nov 21 13:41:35 2010 +0000 Issue #10489: removed broken `__name__` support from configparser But should be fine to reincarnate for our use case c s\|r*t|vr,| ¡}||d<| ||||¡tt|ƒj||||||g|¢Ri|¤ŽSdSdS©NÚ__name__)r ÚcopyÚ_map_section_optionsÚsuperr Ú_interpolate_some) ÚselfÚparserÚoptionÚaccumÚrestÚsectionÚmapÚargsÚkwargs©Ú __class__©úA/usr/lib/python3/dist-packages/fail2ban/client/configparserinc.pyr6s   ÿÿÿûz,BasicInterpolationWithName._interpolate_some)r Ú __module__Ú __qualname__Ú__doc__rÚ __classcell__rrrrr (s r )ÚSafeConfigParserrr r cOs0| ||||¡|j|||||g|¢Ri|¤ŽS©N)rÚ_cp_interpolate_some)rrrrrrrrrrrrFs rcCsNg}|D] }| |¡tj |¡dd}||vr$tj |¡r$| |¡q|S)z,Expands config files with local extension. rz.local)ÚappendÚosÚpathÚsplitextÚisfile)Ú filenamesÚ newFilenamesÚfilenameÚ localnamerrrÚ_expandConfFilesWithLocalLs  €r/éÚSafeConfigParserWithIncludescsÊeZdZdZdZe d¡Ze d¡Ze d¡Z e j dkr$d#‡fdd „ Z nd#d d „Z d ifd d „Z dd„Zedd„ƒZdd„Zgfdd„Zgfdd„Zdd„Zdd„Zd$dd„Zd$dd „Zd#d!d"„Z‡ZS)%r1a Class adds functionality to SafeConfigParser to handle included other configuration files (or may be urls, whatever in the future) File should have section [includes] and only 2 options implemented are 'files_before' and 'files_after' where files are listed 1 per line. Example: [INCLUDES] before = 1.conf 3.conf after = 1.conf It is a simple implementation, so just basic care is taken about recursion. Includes preserve right order, ie new files are inserted to the list of read configs before original, and their includes correspondingly so the list should follow the leaves of the tree. I wasn't sure what would be the right way to implement generic (aka c++ template) so we could base at any *configparser class... so I will leave it for the future ÚINCLUDESz^([\w\-]+)/([^\s>]+)$z%\(([\w\-]+/([^\)]+))\)sz ^(\w+)(\?.+)$©rrNcs:| ¡}tƒ|d<d|d<tt|ƒj|i|¤Ž||_dS)NÚ interpolationú;Úinline_comment_prefixes)rr rr1Ú__init__Ú _cfg_share©rÚ share_configrrrrrr7†s  ÿÿ z%SafeConfigParserWithIncludes.__init__cOs"tj|g|¢Ri|¤Ž||_dSr$)r#r7r8r9rrrr7s Fc CsÂd}d|vr tj |¡}|rV| d¡}| d¡}| ¡}|dkr'd||f}n |dkr.|fnd}|D]#}z |j|||d } | WSttfyU} zWYd} ~ q2d} ~ ww|j||||d } | S) zƒGet an option value for a given section. In opposite to `get`, it differentiate session-related option name like `sec/opt`. Nú/érÚknownúKNOWN/Údefault)ÚDEFAULT)Úraw)rAÚvars)r1ÚSECTION_OPTNAME_CREÚsearchÚgroupÚlowerÚgetr r ) rrrrArBÚsoptÚsecÚoptÚseclwrÚvÚerrrÚget_ex“s(     €ÿz#SafeConfigParserWithIncludes.get_exc CsVd|vsd|vr dSd}tj |¡}|sdS|D]\}}||vr¨|dt|ƒ…} |  ¡} | dkrrd} | dkrNz |jd||} Wn1tyMd} Yn'wzz|j| } Wn ty`YWqw| |} Wn tyqd} Ynwd} | r‡z|j|} Wn ty†Yqwd}z| ||<Wqz | |jd|<Wn | |j|<YYqq|S) z° Interpolates values of the section options (name syntax `%(section/option)s`). Fallback: try to wrap missing default options as "default/options" resp. "known/options" r;z%(rNr?r=r>r<) r1ÚSECTION_OPTSUBST_CREÚfindallÚlenrFÚ _sectionsÚKeyErrorÚ _defaultsÚ_maps) rrrrÚdefaultsÚrplcmntÚsoptreprHrJrIrKÚusedefrLrrrr°s\   þ ÿ  þ ÿ  €×*z1SafeConfigParserWithIncludes._map_section_optionscCó|jSr$)r8©rrrrr:ész)SafeConfigParserWithIncludes.share_configcCs®t}|jrIdt|tƒs |nd |¡}|j |d¡\}}|dur8||jd}|j|dd}||f|j|<||fSt ¡t krEt  t d|¡||fS|ƒ}|j|dd}||fS)Nzinc:ú)NN)r:F)Ú get_includesz Shared file: %s) r1r8Ú isinstanceÚlistÚjoinrGÚreadÚlogSysÚgetEffectiveLevelÚlogLevelÚlog)rr-ÚSCPWIÚhashvÚcfgÚirrrÚ_getSharedSCPWIís  úþz,SafeConfigParserWithIncludes._getSharedSCPWIcCsŽt|tƒs|g}t|ƒ}|jr6dd |¡}|j |¡}|dur4g}|D] }|| ||¡7}q$||j|<|Sg}|D] }|| ||¡7}q:|S)Nz inc-path:r\)r^r_r/r8r`rGÚ2_SafeConfigParserWithIncludes__getIncludesUncached)rr+ÚseenrgÚ fileNamesFullr-rrrÚ _getIncludess   z)SafeConfigParserWithIncludes._getIncludesc Cs$t}z| |¡\}}|sgWSWnty-}zt d||f¡gWYd}~Sd}~wwtj |¡}dgfdgfg}|j|  ¡vrƒ|D]=\} } | |  |j¡vr‚|  |j| ¡} |   d¡D]$} tj  | ¡rh| } ntj || ¡} | |vrtq]||g}| | | |¡7} q]qE|dd|g|ddS)zŽ Given 1 config resource returns list of included files (recursively) with the original one as well Simple loops are taken care about z#Error decoding config file '%s': %sNÚbeforeÚafterÚ rr<)r1rjÚUnicodeDecodeErrorrbÚerrorr'r(ÚdirnameÚ SECTION_NAMEÚsectionsÚoptionsrGÚsplitÚisabsr`rn)rÚresourcerlrfrrirMÚ resourceDirÚnewFilesÚ option_nameÚ option_listÚ newResourcesÚ newResourceÚrÚsrrrÚ__getIncludesUncacheds6ÿ€þ    €z2SafeConfigParserWithIncludes.__getIncludesUncachedcCrZr$)rTr[rrrÚ get_defaults7óz)SafeConfigParserWithIncludes.get_defaultscCrZr$)rRr[rrrÚ get_sections:r…z)SafeConfigParserWithIncludes.get_sectionsTcCsLz|j|}Wn tyt|ƒ‚w|r t| ¡ƒt|jƒBSt| ¡ƒS)zŽReturn a list of option names for the given section name. Parameter `withDefault` controls the include of names from section `[DEFAULT]` )rRrSr ÚsetÚkeysrTr_)rrÚ withDefaultÚoptsrrrrw=s ÿ z$SafeConfigParserWithIncludes.optionsc s¶t|tƒs|g}g}|r|| |¡7}n|}|sgSt d|¡|s(t|ƒdkr¹g}| ¡}| ¡}|D]‚}| |¡\}} | r¶|| 7}|  | ¡¡| ¡  ¡D]e\} ‰t j   | ¡} | r‰|  ¡\} } ˆ ¡‰zˆd=Wn tytYnwtˆ ¡ƒD] } ˆ | ¡} | ˆ| | <q{| | ¡}t|tƒr¯| d| t‡fdd„t|  ¡ƒDƒƒd¡|  ˆ¡qPˆ ¡|| <qPq4|St ¡tkrÈt td|d ¡tjd krÕtj||d d St ||¡S) Nz Loading files: %sr<r r>csg|] }|dˆvr|‘qS)rr)Ú.0ri©r‚rrÚ xsz5SafeConfigParserWithIncludes.read..Úz Reading file: %srr3zutf-8)Úencoding)r^r_rnrbÚinforQr„r†rjÚupdateÚitemsr1ÚCONDITIONAL_REÚmatchÚgroupsrrSrˆÚpoprGÚdictÚ merge_sectionÚiterrcrdreÚsysÚ version_infor#ra)rr+r]rmÚretÚalldÚallsr-rhriÚnÚcondÚkrLÚs2rrŒrraLsZ      ÿ    ÿ €   z!SafeConfigParserWithIncludes.readc CsŠ| ¡}z||}Wntytƒ||<}Ynw|s$| |¡dSi}| ¡D]\}}| |¡s=|dkr=||||<q*| |¡dSr )r†rSr—r‘r’Ú startswith) rrrwÚprefržrIÚskr¡rLrrrr˜‰s  ÿ  €z*SafeConfigParserWithIncludes.merge_sectionr$)T)r rr r!ruÚreÚcompilerCrOr“ršr›r7rNrÚpropertyr:rjrnrkr„r†rwrar˜r"rrrrr1_s*     9   "  =)Ú __author__Ú __copyright__Ú __license__r'r¦ršÚhelpersrr›Ú configparserrr#rrr r r rr%r/r rbrdÚ__all__r1rrrrÚs&