o ;s*bL3@sdZdZdZddlZddlZddlZddlmZejdkr4ddl m Z m Z m Z mZmZGd d d e Zndd l m Z m Z mZmZe je _d d Zee _ddZeeZdZdgZGddde ZdS)z0Yaroslav Halchenko, Serg G. Brester (aka sebres)zHCopyright (c) 2007 Yaroslav Halchenko, 2015 Serg G. Brester (aka sebres)GPLN) getLogger)) ConfigParserBasicInterpolationInterpolationMissingOptionError NoOptionErrorNoSectionErrorcs eZdZdZfddZZS)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|SdSdSN__name__)r copy_map_section_optionssuperr _interpolate_some) selfparseroptionaccumrestsectionmapargskwargs __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|SN)r_cp_interpolate_some)rrrrrrrrrrrrFs rcCsNg}|D] }||tj|dd}||vr$tj|r$||q|S)z,Expands config files with local extension. rz.local)appendospathsplitextisfile) filenames newFilenamesfilename localnamerrr_expandConfFilesWithLocalLs  r/SafeConfigParserWithIncludescseZdZdZdZedZedZedZ e j dkr$d#fdd Z nd#d d Z d ifd d Z ddZeddZddZgfddZgfddZddZddZd$ddZd$dd Zd#d!d"ZZS)%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_sharer share_configrrrrrr7s   z%SafeConfigParserWithIncludes.__init__cOs"tj|g|Ri|||_dSr$)r#r7r8r9rrrr7s Fc Csd}d|vr tj|}|rV|d}|d}|}|dkr'd||f}n |dkr.|fnd}|D]#}z |j|||d } | WSttfyU} zWYd} ~ q2d} ~ ww|j||||d } | S) zGet an option value for a given section. In opposite to `get`, it differentiate session-related option name like `sec/opt`. N/rknownKNOWN/default)DEFAULT)raw)rAvars)r1SECTION_OPTNAME_CREsearchgrouplowergetr r ) rrrrArBsoptsecoptseclwrverrrget_exs(     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} | rz|j|} Wn tyYqwd}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<) r1SECTION_OPTSUBST_CREfindalllenrF _sectionsKeyError _defaults_maps) rrrrdefaultsrplcmntsoptreprHrJrIrKusedefrLrrrrs\         *z1SafeConfigParserWithIncludes._map_section_optionscC|jSr$)r8rrrrr:sz)SafeConfigParserWithIncludes.share_configcCst}|jrIdt|ts |nd|}|j|d\}}|dur8||jd}|j|dd}||f|j|<||fStt krEt t d|||fS|}|j|dd}||fS)Nzinc:)NN)r:F) get_includesz Shared file: %s) r1r8 isinstancelistjoinrGreadlogSysgetEffectiveLevellogLevellog)rr-SCPWIhashvcfgirrr_getSharedSCPWIs  z,SafeConfigParserWithIncludes._getSharedSCPWIcCst|ts|g}t|}|jr6dd|}|j|}|dur4g}|D] }||||7}q$||j|<|Sg}|D] }||||7}q:|S)Nz inc-path:r\)r^r_r/r8r`rG2_SafeConfigParserWithIncludes__getIncludesUncached)rr+seenrg fileNamesFullr-rrr _getIncludess   z)SafeConfigParserWithIncludes._getIncludesc Cs$t}z||\}}|sgWSWnty-}ztd||fgWYd}~Sd}~wwtj|}dgfdgfg}|j| vr|D]=\} } | | |jvr| |j| } | dD]$} 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': %sNbeforeafter rr<)r1rjUnicodeDecodeErrorrberrorr'r(dirname SECTION_NAMEsectionsoptionsrGsplitisabsr`rn)rresourcerlrfrrirM resourceDirnewFiles option_name option_list newResources newResourcersrrr__getIncludesUncacheds6    z2SafeConfigParserWithIncludes.__getIncludesUncachedcCrZr$)rTr[rrr get_defaults7z)SafeConfigParserWithIncludes.get_defaultscCrZr$)rRr[rrr get_sections:rz)SafeConfigParserWithIncludes.get_sectionsTcCsLz|j|}Wn tyt|w|r t|t|jBSt|S)zReturn a list of option names for the given section name. Parameter `withDefault` controls the include of names from section `[DEFAULT]` )rRrSr setkeysrTr_)rr withDefaultoptsrrrrw=s  z$SafeConfigParserWithIncludes.optionsc st|ts|g}g}|r|||7}n|}|sgStd||s(t|dkrg}|}|}|D]}||\}} | r|| 7}| || D]e\} t j | } | r| \} } zd=Wn tytYnwtD] } | } | | | <q{|| }t|tr|d| tfddt| Dd| qP|| <qPq4|Sttkrttd|d tjd krtj||d d St||S) Nz Loading files: %sr<r r>csg|] }|dvr|qS)rr).0rirrr xsz5SafeConfigParserWithIncludes.read..z Reading file: %srr3zutf-8)encoding)r^r_rnrbinforQrrrjupdateitemsr1CONDITIONAL_REmatchgroupsrrSrpoprGdict merge_sectioniterrcrdresys version_infor#ra)rr+r]rmretalldallsr-rhrincondkrLs2rrrraLsZ              z!SafeConfigParserWithIncludes.readc Cs|}z||}Wntyt||<}Ynw|s$||dSi}|D]\}}||s=|dkr=||||<q*||dSr )rrSrrr startswith) rrrwprefrrIskrrLrrrrs    z*SafeConfigParserWithIncludes.merge_sectionr$)T)r rr r!rurecompilerCrOrrrr7rNrpropertyr:rjrnrkrrrwrarr"rrrrr1_s*     9   "  =) __author__ __copyright__ __license__r'rrhelpersrr configparserrr#rrr r r rr%r/r rbrd__all__r1rrrrs&