a =Æ*fa/ã@sŠdZdZdZddlZddlZddlZddlmZddlm Z m Z m Z m Z mZGdd „d e ƒZd d „ZeeƒZd Zd gZGdd „d e ƒZdS)z0Yaroslav Halchenko, Serg G. Brester (aka sebres)zHCopyright (c) 2007 Yaroslav Halchenko, 2015 Serg G. Brester (aka sebres)ZGPLéNé)Ú getLogger)Ú ConfigParserÚBasicInterpolationÚInterpolationMissingOptionErrorÚ NoOptionErrorÚNoSectionErrorcs eZdZdZ‡fdd„Z‡ZS)ÚBasicInterpolationWithNameaˆDecorator 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 sX|rTt|vrT| ¡}||d<| ||||¡tt|ƒj||||||g|¢Ri|¤ŽSdS©NÚ__name__)r ÚcopyÚ_map_section_optionsÚsuperr Ú_interpolate_some) ÚselfÚparserÚoptionZaccumÚrestÚsectionÚmapÚargsÚkwargs©Ú __class__©úC/usr/lib/python3.9/site-packages/fail2ban/client/configparserinc.pyr4s   ÿÿÿz,BasicInterpolationWithName._interpolate_some)r Ú __module__Ú __qualname__Ú__doc__rÚ __classcell__rrrrr &s r cCsNg}|D]@}| |¡tj |¡dd}||vrtj |¡r| |¡q|S)z,Expands config files with local extension. rz.local)ÚappendÚosÚpathÚsplitextÚisfile)Ú filenamesZ newFilenamesÚfilenameZ localnamerrrÚ_expandConfFilesWithLocal?s  r'éÚSafeConfigParserWithIncludescs´eZdZdZdZe d¡Ze d¡Ze d¡Z d!‡fdd„ Z d ifd d „Z d d „Z e dd„ƒZdd„Zgfdd„Zgfdd„Zdd„Zdd„Zd"dd„Zd#dd„Zd$dd „Z‡ZS)%r)a 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 ZINCLUDESz^([\w\-]+)/([^\s>]+)$z%\(([\w\-]+/([^\)]+))\)sz ^(\w+)(\?.+)$Ncs:| ¡}tƒ|d<d|d<tt|ƒj|i|¤Ž||_dS)NZ interpolationú;Zinline_comment_prefixes)r r rr)Ú__init__Ú _cfg_share)rÚ share_configrrrrrr+xs  ÿÿz%SafeConfigParserWithIncludes.__init__Fc CsÄd}d|vrtj |¡}|r®| d¡}| d¡}| ¡}|dkrNd||f}n|dkr\|fnd}|D]H}z|j|||d } | WSttfyª} z WYd} ~ qdd} ~ 00qd|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)ZDEFAULT)Úraw)r3Úvars)r)ÚSECTION_OPTNAME_CREÚsearchÚgroupÚlowerÚgetrr) rrrr3r4ÚsoptÚsecÚoptÚseclwrÚvÚerrrÚget_ex€s$    z#SafeConfigParserWithIncludes.get_exc Csnd|vsd|vrdSd}tj |¡}|s,dS|D]6\}}||vr0|dt|ƒ…} |  ¡} | dkrîd} | dkr z|jd||} Wqìtyœd} Yqì0qòz4z|j| } WntyÈYWq0Yn0| |} Wqòtyêd} Yqò0nd} | r z|j|} WntyYq0Yn0d}z | ||<Wq0z| |jd|<Wn| |j|<Yn0Yq00q0|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%(rNr2r0r1r/) r)ÚSECTION_OPTSUBST_CREÚfindallÚlenr8Ú _sectionsÚKeyErrorÚ _defaultsZ_maps) rrrrÚdefaultsZrplcmntZsoptrepr:r<r;r=Zusedefr>rrrr sP          z1SafeConfigParserWithIncludes._map_section_optionscCs|jS©N)r,©rrrrr-Ösz)SafeConfigParserWithIncludes.share_configcCs¢t}|jr†dt|tƒs|nd |¡}|j |d¡\}}|durj||jd}|j|dd}||f|j|<qšt ¡t kršt  t d|¡n|ƒ}|j|dd}||fS)Nzinc:ú)NN)r-F)Ú get_includesz Shared file: %s) r)r,Ú isinstanceÚlistÚjoinr9ÚreadÚlogSysÚgetEffectiveLevelÚlogLevelÚlog)rr&ÚSCPWIÚhashvÚcfgÚirrrÚ_getSharedSCPWIÚs  z,SafeConfigParserWithIncludes._getSharedSCPWIcCsŽt|tƒs|g}t|ƒ}|jrldd |¡}|j |¡}|durhg}|D]}|| ||¡7}qH||j|<|Sg}|D]}|| ||¡7}qt|S)Nz inc-path:rJ)rLrMr'r,rNr9Ú2_SafeConfigParserWithIncludes__getIncludesUncached)rr%ÚseenrUÚ fileNamesFullr&rrrÚ _getIncludesís    z)SafeConfigParserWithIncludes._getIncludesc Cs&t}z| |¡\}}|sgWSWn:tyZ}z"t d||f¡gWYd}~Sd}~00tj |¡}dgfdgfg}|j|  ¡vr|D]z\} } | |  |j¡vrŒ|  |j| ¡} |   d¡D]H} tj  | ¡rÒ| } ntj || ¡} | |vrêq¼||g}| | | |¡7} q¼qŒ|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ÚbeforeZafterÚ rr/)r)rXÚUnicodeDecodeErrorrPÚerrorr!r"ÚdirnameÚ SECTION_NAMEZsectionsÚoptionsr9ÚsplitÚisabsrNr\)rÚresourcerZrTrrWr?Z resourceDirZnewFilesZ option_nameZ option_listZ newResourcesZ newResourceÚrÚsrrrZ__getIncludesUncacheds.     z2SafeConfigParserWithIncludes.__getIncludesUncachedcCs|jSrH)rFrIrrrÚ get_defaults$sz)SafeConfigParserWithIncludes.get_defaultscCs|jSrH)rDrIrrrÚ get_sections'sz)SafeConfigParserWithIncludes.get_sectionsTcCsPz|j|}Wnty(t|ƒ‚Yn0|rDt| ¡ƒ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]` )rDrErÚsetÚkeysrFrM)rrZ withDefaultZoptsrrrrc*s z$SafeConfigParserWithIncludes.optionsc sªt|tƒs|g}g}|r(|| |¡7}n|}|s4gSt d|¡|sRt|ƒdkrzg}| ¡}| ¡}|D]}| |¡\}} | rj|| 7}|  | ¡¡| ¡  ¡D]Î\} ‰t j   | ¡} | r|  ¡\} } ˆ ¡‰z ˆd=WntyîYn0tˆ ¡ƒD]} ˆ | ¡} | ˆ| | <qü| | ¡}t|tƒrf| d| t‡fdd„t|  ¡ƒDƒƒd¡|  ˆ¡q¤ˆ ¡|| <q¤qj|St ¡tkršt td|d ¡tj||d d S) Nz Loading files: %sr/r r1csg|]}|dˆvr|‘qS)rr)Ú.0rW©rhrrÚ eóz5SafeConfigParserWithIncludes.read..Úz Reading file: %srzutf-8)Úencoding)rLrMr\rPÚinforCrirjrXÚupdateÚitemsr)ÚCONDITIONAL_REÚmatchÚgroupsr rErlÚpopr9ÚdictÚ merge_sectionÚiterrQrRrSÚSafeConfigParserrO)rr%rKr[ÚretZalldÚallsr&rVrWÚnZcondÚkr>Ús2rrnrrO9sR           ÿ z!SafeConfigParserWithIncludes.readc CsŠ| ¡}z ||}Wn ty4tƒ||<}Yn0|sH| |¡dSi}| ¡D]&\}}| |¡sT|dkrT||||<qT| |¡dSr )rjrErzrtruÚ startswith) rrrcZprefrr;Úskrr>rrrr{ss   z*SafeConfigParserWithIncludes.merge_section)N)T)T)N)r rrrrbÚreÚcompiler5rArvr+r@r Úpropertyr-rXr\rYrirjrcrOr{rrrrrr)Rs$   9   "  :)Ú __author__Z __copyright__Z __license__r!r…ÚsysZhelpersrZ configparserrr}rrrrr r'r rPrRÚ__all__r)rrrrÚs