a ‚oe&^ã@sTddlZddlZddlZddlmZmZmZmZddlZddl m Z da edededediZ d d d d d d d d d d d d d d d d œZ eeeeedœZdd„ZdZdZdZdZdZdZeƒZdZdZdZdZdddddddddddddœ Zdd„ZGd d!„d!eƒZGd"d#„d#eƒZ e ƒZ!d$d%„Z"Gd&d'„d'e#ƒZ$Gd(d)„d)e$ƒZ%Gd*d+„d+e$ƒZ&Gd,d-„d-e'ƒZ(Gd.d/„d/e$ƒZ)Gd0d1„d1e$ƒZ*Gd2d3„d3e$ƒZ+Gd4d5„d5e+ƒZ,Gd6d7„d7e$ƒZ-Gd8d9„d9e+ƒZ.Gd:d;„d;e$ƒZ/Gdd?„d?e0ƒZ1Gd@dA„dAe0ƒZ2e1e2dBœZ3dCdD„Z4GdEdF„dFe5ƒZ6GdGdH„dHe6ƒZ7GdIdJ„dJeƒZ8dPdKdL„Z9dQdNdO„Z:dS)RéN)ÚBOM_UTF8Ú BOM_UTF16Ú BOM_UTF16_BEÚ BOM_UTF16_LEé)Ú __version__)Úutf_8N)Úutf16_beÚutf_16)Úutf16_ler )r r r r r r)r Úu16Úutf16zutf-16r Ú utf_16_bezutf-16ber Ú utf_16_lezutf-16lerÚu8ÚutfÚutf8úutf-8)rr r r NcCst | ¡¡dkS)Nr)ÚBOM_LISTÚgetÚlower)Úencoding©rú3C:\Program Files\Certbot\pkgs\configobj\__init__.pyÚ match_utf8Dsrz'%s'z"%s"z%sz '"z"""%s"""z'''%s''')ÚDEFAULT_INDENT_TYPEÚDEFAULT_INTERPOLATIONÚConfigObjErrorÚ NestingErrorÚ ParseErrorÚDuplicateErrorÚConfigspecErrorÚ ConfigObjÚ SimpleValÚInterpolationErrorÚInterpolationLoopErrorÚMissingInterpolationOptionÚRepeatSectionErrorÚ ReloadErrorÚ UnreprErrorÚ UnknownTypeÚflatten_errorsÚget_extra_valuesÚ configparserz é TF) Ú interpolationÚ raise_errorsÚ list_valuesÚ create_emptyÚ file_errorÚ configspecÚ stringifyÚ indent_typerÚdefault_encodingÚunreprÚwrite_empty_valuescCs>tdurddlad|}t |¡}| ¡d ¡d ¡dS)Nrza=r)ÚcompilerÚparseÚ getChildren)ÚsÚprrrÚgetObj€s  r?c@s eZdZdS)r*N)Ú__name__Ú __module__Ú __qualname__rrrrr*‰sr*c@s\eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dS)ÚBuildercCstdurt|jjƒ‚t|ƒS©N)Úmr*Ú __class__r@©ÚselfÚorrrÚbuild�s z Builder.buildcCstt|j| ¡ƒƒSrD)ÚlistÚmaprJr<rGrrrÚ build_List”szBuilder.build_ListcCs|jSrD)ÚvaluerGrrrÚ build_Const—szBuilder.build_ConstcCs2i}tt|j| ¡ƒƒ}|D]}t|ƒ||<q|SrD)ÚiterrLrJr<Únext)rHrIÚdÚiÚelrrrÚ build_Dictšs zBuilder.build_DictcCst| |¡ƒSrD)ÚtuplerMrGrrrÚ build_Tuple¡szBuilder.build_TuplecCs6|jdkrdS|jdkrdS|jdkr*dStdƒ‚dS)NÚNoneÚTrueTÚFalseFzUndefined Name)Únamer*rGrrrÚ build_Name¤s   zBuilder.build_NamecCsdtt|j| ¡ƒƒ\}}z t|ƒ}Wnty>tdƒ‚Yn0t|tƒrT|j dkr\tdƒ‚||S)NZAddg) rKrLrOr<ÚfloatÚ TypeErrorr*Ú isinstanceÚcomplexÚreal)rHrIraÚimagrrrÚ build_Add¯s  zBuilder.build_AddcCs| |j¡}t||jƒSrD)rJÚexprÚgetattrÚattrname)rHrIÚparentrrrÚ build_Getattr¹s zBuilder.build_GetattrcCs| | ¡d¡ S©Nr©rOr<rGrrrÚbuild_UnarySub½szBuilder.build_UnarySubcCs| | ¡d¡SrirjrGrrrÚbuild_UnaryAddÀszBuilder.build_UnaryAddN) r@rArBrJrMrOrUrWr\rcrhrkrlrrrrrC�s  rCcCs|s|Sddl}| |¡Sri)ÚastZ literal_eval)r=rmrrrr8Çsr8c@seZdZdZddd„ZdS)rzk This is the base class for all errors that ConfigObj raises. It is a subclass of SyntaxError. ÚNcCs||_||_t ||¡dSrD)ÚlineÚ line_numberÚ SyntaxErrorÚ__init__)rHÚmessagerprorrrrrÕszConfigObjError.__init__)rnNrn©r@rArBÚ__doc__rrrrrrrÐsrc@seZdZdZdS)rzE This error indicates a level of nesting that doesn't match. N©r@rArBrurrrrrÛsrc@seZdZdZdS)rz‘ This error indicates that a line is badly written. It is neither a valid ``key = value`` line, nor a valid section marker line. Nrvrrrrrásrc@seZdZdZdd„ZdS)r(zW A 'reload' operation failed. This exception is a subclass of ``IOError``. cCst |d¡dS)Nz#reload failed, filename is not set.)ÚIOErrorrr©rHrrrrrîszReloadError.__init__Nrtrrrrr(ésr(c@seZdZdZdS)r z: The keyword or section specified already exists. Nrvrrrrr òsr c@seZdZdZdS)r!z7 An error occured whilst parsing a configspec. Nrvrrrrr!øsr!c@seZdZdZdS)r$z,Base class for the two interpolation errors.Nrvrrrrr$þsr$c@seZdZdZdd„ZdS)r%z=Maximum interpolation depth exceeded in string interpolation.cCst |d|¡dS)Nz*interpolation loop detected in value "%s".©r$rr)rHÚoptionrrrrrsþzInterpolationLoopError.__init__Nrtrrrrr%sr%c@seZdZdZdS)r'zk This error indicates additional sections in a section with a ``__many__`` (repeated) section. Nrvrrrrr' sr'c@seZdZdZdd„ZdS)r&z0A value specified for interpolation was missing.cCsd|}t ||¡dS)Nz%missing option "%s" in interpolation.ry)rHrzÚmsgrrrrrsz#MissingInterpolationOption.__init__Nrtrrrrr&sr&c@seZdZdZdS)r)z An error parsing in unrepr mode.Nrvrrrrr)sr)c@s>eZdZdZe d¡ZdZdd„Zdd„Z dd „Z d d „Z d S) ÚInterpolationEnginez– A helper class to help perform string interpolation. This class is an abstract base class; its descendants perform the actual work. ú %\(([^)]*)\)sú%cCs ||_dSrD©Úsection)rHr€rrrrr*szInterpolationEngine.__init__cs0ˆj|vr|S‡‡fdd„‰ˆ||ˆjiƒ}|S)Nc s¸||jf|vrt|ƒ‚d|||jf<ˆj |¡}|r¨ˆ |¡\}}}|durR|}nˆ||||ƒ}| ¡\} } d |d| …||| d…f¡}| t|ƒ} ˆj || ¡}q0|||jf=|S)axThe function that does the actual work. ``value``: the string we're trying to interpolate. ``section``: the section in which that string was found ``backtrail``: a dict to keep track of where we've been, to detect and prevent infinite recursion loops This is similar to a depth-first-search algorithm. rNrn)r[r%Ú_KEYCREÚsearchÚ _parse_matchÚspanÚjoinÚlen) ÚkeyrNr€Z backtrailÚmatchÚkÚvr=Z replacementÚstartÚendZnew_search_start©Úrecursive_interpolaterHrrrŽ4s      z>InterpolationEngine.interpolate..recursive_interpolate)Ú_cookier€)rHr‡rNrr�rÚ interpolate/s  ,zInterpolationEngine.interpolatecCs”|jjj}d|jj_|j}| |¡}|dur8t|tƒs8qr| di¡ |¡}|dur^t|tƒs^qr|j|urjqr|j}q||jj_|durŒt|ƒ‚||fS)zˆHelper function to fetch values from owning section. Returns a 2-tuple: the value, and the section where it was found. FNÚDEFAULT)r€Úmainr/rr_ÚSectionrgr&)rHr‡Z save_interpZcurrent_sectionÚvalrrrÚ_fetchds      zInterpolationEngine._fetchcCs tƒ‚dS)aÁImplementation-dependent helper function. Will be passed a match object corresponding to the interpolation key we just found (e.g., "%(foo)s" or "$foo"). Should look up that key in the appropriate config file section (using the ``_fetch()`` helper function) and return a 3-tuple: (key, value, section) ``key`` is the name of the key we're looking for ``value`` is the value found for that key ``section`` is a reference to the section where it was found ``key`` and ``section`` should be None if no further interpolation should be performed on the resulting value (e.g., if we interpolated "$$" and returned "$"). N)ÚNotImplementedError)rHrˆrrrrƒ†sz InterpolationEngine._parse_matchN) r@rArBruÚreÚcompiler�r�rrr�r•rƒrrrrr|s 5"r|c@s&eZdZdZdZe d¡Zdd„ZdS)ÚConfigParserInterpolationzBehaves like ConfigParser.r~r}cCs"| d¡}| |¡\}}|||fS)Nr)Úgroupr•©rHrˆr‡rNr€rrrrƒŸs z&ConfigParserInterpolation._parse_matchN) r@rArBrur�r—r˜r�rƒrrrrr™šs r™c@s4eZdZdZdZdZe dejej B¡Z dd„Z dS)ÚTemplateInterpolationzBehaves like string.Template.ú$zÐ \$(?: (?P\$) | # Two $ signs (?P[_a-z][_a-z0-9]*) | # $name format {(?P[^}]*)} # ${name} format ) cCs\| d¡p| d¡}|dur4| |¡\}}|||fS| d¡durNd|jdfSd| ¡dfS)NZnamedZbracedZescaped)ršr•Ú _delimiterr›rrrrƒ²s  z"TemplateInterpolation._parse_matchN) r@rArBrur�ržr—r˜Ú IGNORECASEÚVERBOSEr�rƒrrrrrœ¦s úrœ)r-ÚtemplatecGs|j|g|¢RŽSrD)Ú__new__)ÚclsÚargsrrrÚ __newobj__Æsr¥c@s$eZdZdZdd„Zdd„ZdDdd„Zd d „Zd d „Zd d„Z dEdd„Z dd„Z dFdd„Z dd„Z efdd„Zdd„Zdd„ZdGdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„ZeZd*d+„Zd,d-„ZeZd.e_d/d0„Zd1d2„Zd3d4„ZdHd6d7„Zd8d9„Z d:d;„Z!dd?„Z#d@dA„Z$dBdC„Z%dS)Ir“a� A dictionary-like object that represents a section in a config file. It does string interpolation if the 'interpolation' attribute of the 'main' object is set to True. Interpolation is tried first from this object, then from the 'DEFAULT' section of this object, next from the parent and its 'DEFAULT' section, and so on until the main object is reached. A Section will behave like an ordered dictionary - following the order of the ``scalars`` and ``sections`` attributes. You can use this to change the order of members. Iteration follows the order: scalars, then sections. cCs$t ||d¡|j |d¡dS)Nrr)ÚdictÚupdateÚ__dict__©rHÚstaterrrÚ __setstate__ÝszSection.__setstate__cCst|ƒ|jf}t|jf|fSrD)r¦r¨r¥rFr©rrrÚ __reduce__ászSection.__reduce__NcCsT|dur i}t |¡||_||_||_||_| ¡| ¡D]\}}|||<q>dS)zÇ * parent is the section above * depth is the depth level of this section * main is the main ConfigObj * indict is a dictionary to initialise the section with N)r¦rrrgr’Údepthr[Ú _initialiseÚitems)rHrgr­r’Úindictr[ÚentryrNrrrrræs zSection.__init__cCs:g|_g|_i|_i|_d|_g|_i|_g|_d|_dS)NF) ÚscalarsÚsectionsÚcommentsÚinline_commentsr4ÚdefaultsÚdefault_valuesÚ extra_valuesÚ_createdrxrrrr®szSection._initialisecCsxz |j}Wn`tyj|jj}|dkr,t}| ¡}t |d¡}|durXd|j_|YS||ƒ}|_Yn0| ||¡S)NTF) Z_interpolation_engineÚAttributeErrorr’r/rrÚinterpolation_enginesrr�)rHr‡rNZenginer[Úclass_rrrÚ _interpolates   zSection._interpolatecsft ˆˆ¡}ˆjjrbt|tjƒr,ˆ ˆ|¡St|tƒrb‡‡fdd„‰‡fdd„|Dƒ}||krb|S|S)z+Fetch the item and do string interpolation.cst|tjƒrˆ ˆ|¡S|SrD)r_ÚsixÚ string_typesr½)r±)r‡rHrrÚ_check/s  z#Section.__getitem__.._checkcsg|] }ˆ|ƒ‘qSrr)Ú.0r±)rÀrrÚ 3óz'Section.__getitem__..) r¦Ú __getitem__r’r/r_r¾r¿r½rK)rHr‡r”Únewr)rÀr‡rHrrÄ(s    zSection.__getitem__Fc CsHt|tjƒstd|ƒ‚||jvr6g|j|<d|j|<||jvrL|j |¡t|tƒrz||vrj|j   |¡t   |||¡nÊt|t ƒrÆ|sÆ||vrœ|j   |¡|j d}t   ||t|||j||d�¡n~||vrÚ|j  |¡|jj�s6t|tjƒrònDt|ttfƒ�r*|D] }t|tjƒ�std|ƒ‚�qn td|ƒ‚t   |||¡dS)a³ Correctly set a value. Making dictionary values Section instances. (We have to special case 'Section' instances - which are also dicts) Keys must be strings. Values need only be strings (or lists of strings) if ``main.stringify`` is set. ``unrepr`` must be set when setting a value to a dictionary, without creating a new sub-section. zThe key "%s" is not a string.rnr)r°r[zValue is not a string "%s".N)r_r¾r¿Ú ValueErrorr´rµr¶Úremover“r³Úappendr¦Ú __setitem__r­r’r²r5rKrVr^)rHr‡rNr8Z new_depthr±rrrrÉ9sJ           ûý     zSection.__setitem__cCsDt ||¡||jvr$|j |¡n |j |¡|j|=|j|=dS)z-Remove items from the sequence when deleting.N)r¦Ú __delitem__r²rÇr³r´rµ©rHr‡rrrrÊts    zSection.__delitem__cCs&z ||WSty |YS0dS)z>A version of ``get`` that doesn't bypass string interpolation.N©ÚKeyError©rHr‡Údefaultrrrrs  z Section.getcCs|D]}||||<qdS)zD A version of update that uses our ``__setitem__``. Nr)rHr°r±rrrr§‡szSection.updatecCs8z ||}Wn ty,|tur$‚|}Yn0||=|S)z± 'D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised' )rÍÚMISSING)rHr‡rÏr”rrrÚpop�s   z Section.popcCs6|j|j}|stdƒ‚|d}||}||=||fS)zPops the first (key,val)z": 'popitem(): dictionary is empty'r)r²r³rÍ)rHZsequencer‡r”rrrÚpopitemŸs zSection.popitemcCs8t |¡g|_g|_i|_i|_d|_g|_g|_dS)zÔ A version of clear that also affects scalars/sections Also clears comments and configspec. Leaves other attributes alone : depth/main/parent are not affected N) r¦Úclearr²r³r´rµr4r¶r¸rxrrrrÓªs z Section.clearcCs2z ||WSty,|||<||YS0dS)z:A version of setdefault that sets sequence if appropriate.NrÌrÎrrrÚ setdefault¼s   zSection.setdefaultcCstt|j|jt| ¡ƒƒƒS)z8D.items() -> list of D's (key, value) pairs, as 2-tuples)rKÚzipr²r³Úvaluesrxrrrr¯Åsz Section.itemscCs |j|jS)zD.keys() -> list of D's keys©r²r³rxrrrÚkeysÊsz Section.keyscs‡fdd„ˆjˆjDƒS)z D.values() -> list of D's valuescsg|] }ˆ|‘qSrr©rÁr‡rxrrrÂÑrÃz"Section.values..r×rxrrxrrÖÏszSection.valuescCstt| ¡ƒƒS)z=D.iteritems() -> an iterator over the (key, value) items of D)rPrKr¯rxrrrÚ iteritemsÔszSection.iteritemscCst|j|jƒS)z.D.iterkeys() -> an iterator over the keys of D)rPr²r³rxrrrÚiterkeysÙszSection.iterkeyscCstt| ¡ƒƒS)z2D.itervalues() -> an iterator over the values of D)rPrKrÖrxrrrÚ itervaluesàszSection.itervaluescs0‡fdd„‰dd ‡fdd„ˆjˆjDƒ¡S)zx.__repr__() <==> repr(x)cs.z ˆ|WSty(t ˆ|¡YS0dSrD©r&r¦rÄ©r‡rxrrÚ_getvalçs  z!Section.__repr__.._getvalz{%s}ú, cs$g|]}dt|ƒtˆ|ƒƒf‘qS©z%s: %s©ÚreprrÙ©rßrrrÂìsÿz$Section.__repr__..©r…r²r³rxr©rßrHrÚ__repr__ås  ÿzSection.__repr__zx.__str__() <==> str(x)cCs\i}|D]N}||}t|tƒr(| ¡}n&t|tƒr>> n = a.dict() >>> n == a 1 >>> n is a 0 )r_r“r¦rKrV)rHZnewdictr±Ú this_entryrrrr¦õs       z Section.dictcCsRt| ¡ƒD]@\}}||vrDt||tƒrDt|tƒrD|| |¡q |||<q dS)aQ A recursive update - useful for merging config files. >>> a = '''[section1] ... option1 = True ... [[subsection]] ... more_options = False ... # end of file'''.splitlines() >>> b = '''# File is user.ini ... [section1] ... option1 = False ... # end of file'''.splitlines() >>> c1 = ConfigObj(b) >>> c2 = ConfigObj(a) >>> c2.merge(c1) >>> c2 ConfigObj({'section1': {'option1': 'False', 'subsection': {'more_options': 'False'}}}) N)rKr¯r_r¦Úmerge)rHr°r‡r”rrrrés ÿz Section.mergecCs®||jvr|j}n||jvr$|j}n td|ƒ‚| |¡}||}t ||¡t |||¡| |¡| ||¡|j |}|j |}|j |=|j |=||j |<||j |<dS)a Change a keyname to another, without changing position in sequence. Implemented so that transformations can be made on keys, as well as on values. (used by encode and decode) Also renames comments. zKey "%s" not found.N) r²r³rÍÚindexr¦rÊrÉrÇÚinsertr´rµ)rHZoldkeyZnewkeyÚthe_listÚposr”ZcommZinline_commentrrrÚrename,s"          zSection.renameTc Ks i}tt|jƒƒD]d}|j|}z(|||fi|¤Ž}|j|}|||<Wqtyt|r^‚n|j|}d||<Yq0qtt|jƒƒD]€}|j|}|ræz|||fi|¤ŽWn,tyÚ|rÄ‚n|j|}d||<Yn0|j|}||j|f||dœ|¤Ž||<q†|S)a˜ Walk every member and call a function on the keyword and value. Return a dictionary of the return values If the function raises an exception, raise the errror unless ``raise_errors=False``, in which case set the return value to ``False``. Any unrecognised keyword arguments you pass to walk, will be pased on to the function you pass in. Note: if ``call_on_sections`` is ``True`` then - on encountering a subsection, *first* the function is called for the *whole* subsection, and then recurses into it's members. This means your function must be able to handle strings, dictionaries and lists. This allows you to change the key of subsections as well as for ordinary members. The return value when called on the whole subsection has to be discarded. See the encode and decode methods for examples, including functions. .. admonition:: caution You can use ``walk`` to transform the names of members of a section but you mustn't add or delete members. >>> config = '''[XXXXsection] ... XXXXkey = XXXXvalue'''.splitlines() >>> cfg = ConfigObj(config) >>> cfg ConfigObj({'XXXXsection': {'XXXXkey': 'XXXXvalue'}}) >>> def transform(section, key): ... val = section[key] ... newkey = key.replace('XXXX', 'CLIENT1') ... section.rename(key, newkey) ... if isinstance(val, (tuple, list, dict)): ... pass ... else: ... val = val.replace('XXXX', 'CLIENT1') ... section[newkey] = val >>> cfg.walk(transform, call_on_sections=True) {'CLIENT1section': {'CLIENT1key': None}} >>> cfg ConfigObj({'CLIENT1section': {'CLIENT1key': 'CLIENT1value'}}) F)r0Úcall_on_sections)Úranger†r²Ú Exceptionr³Úwalk) rHÚfunctionr0rïZkeywargsÚoutrSr±r”rrrròJs@/         ÿýü z Section.walkcCsn||}|dkrdS|dkr dSz*t|tjƒs6tƒ‚n|jj| ¡WSWntyhtd|ƒ‚Yn0dS)a_ Accepts a key as input. The corresponding value must be a string or the objects (``True`` or 1) or (``False`` or 0). We allow 0 and 1 to retain compatibility with Python 2.2. If the string is one of ``True``, ``On``, ``Yes``, or ``1`` it returns ``True``. If the string is one of ``False``, ``Off``, ``No``, or ``0`` it returns ``False``. ``as_bool`` is not case sensitive. Any other input will raise a ``ValueError``. >>> a = ConfigObj() >>> a['a'] = 'fish' >>> a.as_bool('a') Traceback (most recent call last): ValueError: Value "fish" is neither True nor False >>> a['b'] = 'True' >>> a.as_bool('b') 1 >>> a['b'] = 'off' >>> a.as_bool('b') 0 TFz$Value "%s" is neither True nor FalseN)r_r¾r¿rÍr’Ú_boolsrrÆ)rHr‡r”rrrÚas_boolŸs  zSection.as_boolcCs t||ƒS)ai A convenience method which coerces the specified value to an integer. If the value is an invalid literal for ``int``, a ``ValueError`` will be raised. >>> a = ConfigObj() >>> a['a'] = 'fish' >>> a.as_int('a') Traceback (most recent call last): ValueError: invalid literal for int() with base 10: 'fish' >>> a['b'] = '1' >>> a.as_int('b') 1 >>> a['b'] = '3.2' >>> a.as_int('b') Traceback (most recent call last): ValueError: invalid literal for int() with base 10: '3.2' )ÚintrËrrrÚas_intËszSection.as_intcCs t||ƒS)a> A convenience method which coerces the specified value to a float. If the value is an invalid literal for ``float``, a ``ValueError`` will be raised. >>> a = ConfigObj() >>> a['a'] = 'fish' >>> a.as_float('a') #doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ValueError: invalid literal for float(): fish >>> a['b'] = '1' >>> a.as_float('b') 1.0 >>> a['b'] = '3.2' >>> a.as_float('b') #doctest: +ELLIPSIS 3.2... )r]rËrrrÚas_floatâszSection.as_floatcCs$||}t|ttfƒrt|ƒS|gS)aU A convenience method which fetches the specified value, guaranteeing that it is a list. >>> a = ConfigObj() >>> a['a'] = 1 >>> a.as_list('a') [1] >>> a['a'] = (1,) >>> a.as_list('a') [1] >>> a['a'] = [1] >>> a.as_list('a') [1] )r_rVrK)rHr‡ÚresultrrrÚas_listøszSection.as_listcCs2|j|}t |||¡||jvr.|j |¡|S)a Restore (and return) default value for the specified key. This method will only work for a ConfigObj that was created with a configspec and has been validated. If there is no default value for this key, ``KeyError`` is raised. )r·r¦rÉr¶rÈrÎrrrÚrestore_defaults   zSection.restore_defaultcCs2|jD]}| |¡q|jD]}|| ¡qdS)a' Recursively restore default values to all members that have them. This method will only work for a ConfigObj that was created with a configspec and has been validated. It doesn't delete or modify entries without default values. N)r·rür³Úrestore_defaults)rHr‡r€rrrrýs   zSection.restore_defaults)NN)F)N)N)TF)&r@rArBrur«r¬rrr®r½rÄrÉrÊrr§rÐrÑrÒrÓrÔr¯rØrÖrÚrÛÚ__iter__rÜrçÚ__str__r¦rérîròrörørùrûrürýrrrrr“ÊsJ  ;     ÿ U,r“c@sˆeZdZdZe dej¡Ze dej¡Ze dej¡Z e dej¡Z e dej¡Z e d¡Z e d¡Z e d ¡Ze d ¡Ze efe efd œZd d d d d d d d dœZdFdd„Zdd„ZdGdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„ZdHd*d+„Zd,d-„Z d.d/„Z!d0d1„Z"d2d3„Z#d4d5„Z$d6d7„Z%d8d9„Z&d:d;„Z'dd?„Z)dJd@dA„Z*dBdC„Z+dDdE„Z,dS)Kr"z2An object to read, create, and write config files.a“^ # line start (\s*) # indentation ( # keyword (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'"=].*?) # no quotes ) \s*=\s* # divider (.*) # value (including list values and comments) $ # line end a=^ (\s*) # 1: indentation ((?:\[\s*)+) # 2: section marker open ( # 3: section name open (?:"\s*\S.*?\s*")| # at least one non-space with double quotes (?:'\s*\S.*?\s*')| # at least one non-space with single quotes (?:[^'"\s].*?) # at least one non-space unquoted ) # section name close ((?:\s*\])+) # 4: section marker close \s*(\#.*)? # 5: optional comment $aÙ^ (?: (?: ( (?: (?: (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\#][^,\#]*?) # unquoted ) \s*,\s* # comma )* # match all list items ending in a comma (if any) ) ( (?:".*?")| # double quotes (?:'.*?')| # single quotes (?:[^'",\#\s][^,]*?)| # unquoted (?:(?|j r¦t|dƒ�}|  d¡Wdƒn1sœ0Yg}nªt|t tfƒrÄt |ƒ}n’t|tƒ�r,t|tƒrò‡fdd„‰ˆ||ƒn|D]}||||<qö|`|du�r"| |¡nd|_dSt|dtƒtu�rN| ¡�pJg}ntdƒ‚|�r´| |¡}|D]H}|�rj|d d v�r†�qjd D]}| |¡�rŠ||_�qª�qŠ�q´�qjtd d „|Dƒƒ�sÔJt|ƒƒ‚dd„|Dƒ}| |¡|j�rBd|jdj}t|jƒdk�r&d|} t | ƒ} n |jd} |j| _!|| _"| ‚|`|du�rXd|_n | |¡dS)NÚrbzConfig file not found: "%s".ÚwrncsB|jD]}||||<q|jD]}i||<ˆ||||ƒqdSrDr×)Z in_sectionÚ this_sectionr±r€©Ú set_sectionrrrês   z$ConfigObj._load..set_sectionÚreadz>infile must be a filename, file like object, or list of lines.éÿÿÿÿ)ú Ú )ú rrcss|]}t|tjƒVqdSrD)r_r¾r¿©rÁrorrrÚ rÃz"ConfigObj._load..cSsg|]}| d¡‘qS)r)ÚrstriprrrrrÂrÃz#ConfigObj._load..z at line %s.rrz2Parsing failed with several errors. First error %s)#r_r¾r¿ÚfilenameÚosÚpathÚisfileÚopenÚ readlinesr3rwr2ÚwriterKrVr¦r"Ú_errorsÚ_handle_configspecr4rerÐrr^Ú _handle_bomÚendswithÚnewlinesÚallrãÚ_parserpr†rÚerrorsZconfig) rHrr4ÚhZcontentr±rorŒÚinfor{ÚerrorrrrrÐsj   , (                zConfigObj._loadcCs¾|dur t}d|_g|_|d|_|d|_|d|_|d|_|d|_|d|_|d|_ |d|_ |d |_ d |_ d|_ |d |_|d |_g|_g|_d|_|jr°d |_t |¡dS) Nr0r/r1r2r3r5r6rr7Fr9r8)r rr%r0r/r1r2r3r5r6rr7ÚBOMr)r9r8Úinitial_commentÚ final_commentr4rr“r®)rHrrrrr®0s.           zConfigObj._initialisecs0‡fdd„‰dd ‡fdd„ˆjˆjDƒ¡S)Ncs.z ˆ|WSty(t ˆ|¡YS0dSrDrÝrÞrxrrrßQs  z#ConfigObj.__repr__.._getvalzConfigObj({%s})ràcs$g|]}dt|ƒtˆ|ƒƒf‘qSrárârÙrärrrÂWsÿz&ConfigObj.__repr__..rårxrrærrçPs   ÿÿzConfigObj.__repr__cCsP|jdur&|j ¡tvr&| ||j¡St|ttfƒr>|d}n|}t|tjƒr\| ||j¡S|jdu�r(t|j ¡}|dkrÆtt   ¡ƒD],\}\}}|sœqŠ|  |¡rŠ| ||¡SqŠ| ||j¡St |}|  |¡sæ| ||j¡S|t |ƒd…}t|ttfƒ�r||d<n|}d|_| ||j¡Stt   ¡ƒD]Ì\}\}}t|tjƒ�r4|  |¡�s`�q4nž||_|�sîd|_|t |ƒd…}t|ttfƒ�rœ||d<n|}t|tjƒ�r¼| d¡St|tjƒ�rÞ| d¡ d¡S| |d¡S| ||¡S�q4tj�r"t|tƒ�r"| |d¡St|tjƒ�r@| d¡ d¡S| |d¡SdS)a1 Handle any BOM, and decode if necessary. If an encoding is specified, that *must* be used - but the BOM should still be removed (and the BOM attribute set). (If the encoding is wrongly specified, then a BOM for an alternative encoding won't be discovered or removed.) If an encoding is not specified, UTF8 or UTF16 BOM will be detected and removed. The BOM attribute will be set. UTF16 will be decoded to unicode. NOTE: This method must not be called with an empty ``infile``. Specifying the *wrong* encoding is likely to cause a ``UnicodeDecodeError``. ``infile`` must always be returned as a list of lines, but may be passed in as a single string. Nrr Tr)rrrÚ_decoder_rKrVr¾Z text_typeÚBOMSr¯Ú startswithÚBOM_SETr†r0Ú binary_typeÚ splitlinesÚdecodeZPY2Ústr)rHrroÚencr0rZfinal_encodingÚnewlinerrrr'[s`  ÿ        zConfigObj._handle_bomcCs&t|tjƒr|jr| |j¡S|SdS)z@Decode ASCII strings to unicode if a self.encoding is specified.N)r_r¾r7rr9)rHZaStringrrrÚ_a_to_uÐs zConfigObj._a_to_ucCstt|tjƒr| d¡St|tjƒr@|r6| |¡ d¡S| d¡S|rpt|ƒD]"\}}t|tjƒrL| |¡||<qL|S)zŽ Decode infile to unicode. Using the specified encoding. if is a string, it also needs converting to a list. T)r_r¾r¿r8r7r9Ú enumerate)rHrrrSrorrrr3Øs     zConfigObj._decodecCs&t|tjƒr|jr| |j¡S|SdS)z'Decode element to unicode if necessary.N)r_r¾r7r7r9)rHrorrrÚ_decode_elementñs zConfigObj._decode_elementcCst|tjƒst|ƒS|SdS)zh Used by ``stringify`` within validate, to turn non-string values into strings. N)r_r¾r¿r:©rHrNrrrÚ_strús zConfigObj._strc Cs,|j}|jrd|_g}d}|}t|ƒd}d}d}||k�rî|rDg}|d7}||} |  ¡} | rj|  d¡rzd}| | ¡q2|sŒ||_g}d}d}|j | ¡} | du�rÐ|   ¡\} } }}}| rÌ|j durÌ| |_ |   d¡}||  d¡krö|  d t ||¡q2||jk�r@z| ||¡j}Wn(t�y<|  d t ||¡Yq2Yn0n<||jk�rT|j}n(||jdk�rj|}n|  d t ||¡q2| |¡}||v�r¢|  d t||¡q2t||||d �}|||<||j|<||j|<q2|j | ¡} | du�rþ|  d | ¡t||¡q2|   ¡\} }}| �r$|j du�r$| |_ |dd…dv�rðz| ||||¡\}}}Wn(t�yx|  dt||¡Yq2Ynv0|j�r d}z t|ƒ}WnZt�yì}z@t|ƒtk�r¸d}nd}|  |t||¡WYd}~q2WYd}~n d}~00n°|j�rdd}z t|ƒ}WnXt�y`}z>t|tƒ�r,d}nd}|  |t||¡WYd}~q2WYd}~n d}~00n.zValue "%s" is not a string.ú""rrBrZrYú#Value "%s" cannot be safely quoted.r)r9r_rKrVr†r^r…r¾r¿r5r:r^r1ÚnoquotrÚ wspace_plusÚ_get_single_quoteÚ_get_triple_quote)rHrNr]Zno_lists_no_quotesZ need_tripleZhash_triple_quoteZcheck_for_singleÚquotrrxrr^ÞsH ÿ   "  ÿþ   zConfigObj._quotecCs4d|vrd|vrtd|ƒ‚nd|vr,t}nt}|S)NrZrYra)rÚsquotÚdquot©rHrNrfrrrrd%s zConfigObj._get_single_quotecCsD| d¡dkr(| d¡dkr(td|ƒ‚| d¡dkr.) rr1Ú _nolistvaluerˆrqrHÚ _valueexprLÚ _listvalueexpÚfindall)rHrNrTr1ZsingleZ empty_listrVrìrrxrrQ9s6     zConfigObj._handle_valuec Csè|dd…}|dd…}|j|d}|j|d}| |¡} | dur`t|  ¡ƒ} |  |¡| S| |¡dkrttƒ‚||kr°|d7}|d7}||} |  |¡dkr¶|| 7}qtq¶qttƒ‚| | ¡} | durÎtƒ‚|  ¡\}} ||| |fS)z9Extract the value, where we are in a multiline situation.NrErrrr)Ú _triple_quoterˆrKrHrÈrjrq) rHrNrrSrRrfZnewvalueZ single_lineZ multi_linerTZretvalrorVrrrrOjs0        zConfigObj._multilinec Csˆt|tƒs~zt|dddd�}Wn^tyN}ztd|ƒ‚WYd}~n8d}~0ty|}ztd|ƒ‚WYd}~n d}~00||_dS)zParse the configspec.T)r0r3rzParsing configspec failed: %sNzReading configspec failed: %s)r_r"rr!rwr4)rHr4rWrrrr&�s ý  "zConfigObj._handle_configspeccCs´|j}| d¡}t|tƒr8|jD]}||vr |||_q |jD]p}|dkrLq>||vr’i||<d||_|r’|j |g¡|j|<|j |d¡|j|<t||tƒr>||||_q>dS)zŠ Called by validate. Handles setting the configspec on subsections including sections to be validated by __many__ Ú__many__TrnN) r4rr_r¦r³r¹r´rµr“)rHr€Úcopyr4Úmanyr±rrrÚ_set_configspec¢s"      zConfigObj._set_configspeccCsN|js| | |¡¡}nt|ƒ}d|| |j|dd�¡| d¡|| |¡fS)z.Write an individual line, for the write methodú %s%s%s%s%sFr\z = )r8r?r^rãr=)rHÚ indent_stringr±rèrVr”rrrÚ _write_line¾süzConfigObj._write_linecCs<d|| d|¡|j| |¡dd�| d|¡| |¡fS)zWrite a section marker linervrCFr\rD)r=r^r?)rHrwr­r±rVrrrÚ _write_markerÌs   üzConfigObj._write_markercCs.|sdS|j}| d¡s&|| d¡7}||S)zDeal with a comment.rnrBz # )r6r5r=)rHrVr‹rrrÚ_handle_commentÕs  zConfigObj._handle_commentc sBˆjdurtˆ_g}ˆ d¡}ˆ d¡}|dur~ˆj}dˆ_ˆ}ˆjD]6}ˆ |¡}| ¡}|rr| |¡sr||}| |¡qFˆj|j } |j |j D]¸} | |j vr¦q–|j | D]6} ˆ |  ¡¡} | rØ|  |¡sØ|| } | | | ¡q°|| } ˆ |j| ¡} t| tƒ�r8| ˆ | | j | | ¡¡| ˆj| d�¡q–| ˆ | | | | ¡¡q–|ˆu�r¤ˆjD]<}ˆ |¡}| ¡}|�r�| |¡�s�||}| |¡�q`|ˆ_|ˆu�r²|Sˆjdu�r0|du�r0ˆj�râ‡fdd„|Dƒ}ˆj�r,ˆjdu�s t ˆj ¡¡dk�r,|�s| d ¡t|d |d <|Sˆj�p>> filename = a.filename >>> a.filename = 'test.ini' >>> a.write() >>> a.filename = filename >>> a == ConfigObj('test.ini', raise_errors=True) 1 >>> import os >>> os.remove('test.ini') NrBz# Frcsg|]}| ˆj¡‘qSr)Úencoder)rÁÚlrxrrrÂ/rÃz#ConfigObj.write..rrnrÚmoderÚwin32rrÚasciiÚwb).r6rr=r/r1r?rFr5rÈr­r²r³r¶r´Úlstriprzrµr_r“ryÚextendr$rxr2rrr0rrrrr)rÚlineseprer}ÚsysÚplatformr…r(r¾r7r{r7rr")rHZoutfiler€rôÚcsZcspZint_valroZ stripped_linerwr±Z comment_linerèrVr<ÚoutputZ output_bytesr-rrxrr$ás¦         üü    ÿ ÿÿ  ÿþ   zConfigObj.writecs\ˆdurtˆjdurtdƒ‚ˆr0ddlm}|ˆ_ˆ‰ˆrtˆjjˆ_ˆjjˆ_ˆjjˆ_ˆjjˆ_ˆjj ˆ_ ˆjj ˆ_ ˆj‰ˆ  ˆˆ¡‡‡‡‡‡‡‡fdd„}i‰d}d}‡fdd „ˆj Dƒ} ‡fd d „ˆj Dƒ} ‡fd d „ˆj Dƒ} ˆj D]�} | d vrúqì| ˆj v�s| ˆjv�rVd} d}ˆ�rb| ˆj v�rbˆj | g¡ˆj| <ˆj | d ¡ˆj| <n d} ˆ| }|| ˆ| || ||ƒ\}}qìd}dˆj v�r˜ˆd}ndˆj v�r¬ˆd}|du�rä| D]$} ˆ| }|| ||d||ƒ\}}�qºg} | D]4} d}ˆ�sdˆ| <nd}d| }ˆ |¡ˆ| <�qè| D]4} d}ˆ�s:dˆ| <nd}d| }ˆ |¡ˆ| <�q"ˆj D]¬} ˆˆu�rz| dk�rz�q^ˆ| jdu�r˜|  | ¡�q^ˆ�rƈj | g¡ˆj| <ˆj | d ¡ˆj| <ˆjˆˆˆˆ| d�}|ˆ| <|dk�rôd}n|dk�rd}nd}�q^| ˆ_ˆ�r$ˆj�s$d}|�rDˆ�rDˆ�rDtˆ ¡ƒ }|�rNdS|�rXdSˆS)a; Test the ConfigObj against a configspec. It uses the ``validator`` object from *validate.py*. To run ``validate`` on the current ConfigObj, call: :: test = config.validate(validator) (Normally having previously passed in the configspec when the ConfigObj was created - you can dynamically assign a dictionary of checks to the ``configspec`` attribute of a section though). It returns ``True`` if everything passes, or a dictionary of pass/fails (True/False). If every member of a subsection passes, it will just have the value ``True``. (It also returns ``False`` if all members fail). In addition, it converts the values from strings to their native types if their checks pass (and ``stringify`` is set). If ``preserve_errors`` is ``True`` (``False`` is default) then instead of a marking a fail with a ``False``, it will preserve the actual exception object. This can contain info about the reason for failure. For example the ``VdtValueTooSmallError`` indicates that the value supplied was too small. If a value (or section) is missing it will still be marked as ``False``. You must have the validate module to use ``preserve_errors=True``. You can then use the ``flatten_errors`` function to turn your nested results dictionary into a flattened list of failures - useful for displaying meaningful error messages. NzNo configspec supplied.r)ÚVdtMissingValuec sJˆj |d¡zˆ ˆ|¡ˆj|<Wnttˆjfy@Yn0zˆj|||d�}WnPˆjy¦}z6ˆrxt|ˆjƒr‚dˆ|<n |ˆ|<d}d}WYd}~n¤d}~00d}dˆ|<ˆj sÀ|�rˆj �st|t t fƒrê‡fdd„|Dƒ}n|rü|durüd}n ˆ  |¡}||k�s|�r|ˆ|<ˆ�sB|�rB|ˆj v�rBˆj  |¡||fS)N)ÚmissingFTcsg|]}ˆ |¡‘qSr)rA)rÁÚitemrxrrr°rÃz>ConfigObj.validate..validate_entry..rn)r·rÑZget_default_valuerÍrºÚbaseErrorClassÚcheckr_Ú_vdtMissingValuer5rKrVrAr¶rÈ)r±Úspecr”r‰Úret_trueÚ ret_falserŒrW©r4rsrôÚpreserve_errorsr€rHÚ validatorrrÚvalidate_entry�s<þ      z*ConfigObj.validate..validate_entryTcsg|]}|ˆvr|‘qSrr©rÁr‰)r4rrrÂÁrÃz&ConfigObj.validate..csg|]}|ˆjvr|‘qSr)r²r•rrrrÂÂrÃcsg|]}|ˆjvr|‘qSr)r³r•rrrrÂÃrÃ)rrÚ ___many___rnFrrr–z"Value %r was provided as a sectionz)Section %r was provided as a single valuer‘)r’rsr€)r4rÆZconfigobj.validaterˆr�r1r2rr0r)r6rur²r³r¶r´rrµr‹rÈÚvalidater¸r¹ÚanyrÖ)rHr“r’rsr€rˆr”r�r�Z unvalidatedZincorrect_sectionsZincorrect_scalarsr±r‰r”rtr{rŒrr‘rr—Ss¶$         -  ÿ ÿ  ÿ      ÿ       zConfigObj.validatecCs | ¡| ¡d|_d|_dS)z@Clear ConfigObj instance and restore to 'freshly created' state.N)rÓr®r4r rxrrrÚreset" szConfigObj.resetcCspt|jtjƒstƒ‚|j}i}tD]}|dkr0q"t||ƒ||<q"|j}||d<| ¡|  |¡|  ||¡dS)z´ Reload a ConfigObj from file. This method raises a ``ReloadError`` if the ConfigObj doesn't have a filename attribute pointing to a file. r4N) r_rr¾r¿r(r rer rÓr®r)rHrZcurrent_optionsr±r4rrrÚreload- s zConfigObj.reload)NNNNTFTFFTNNFFF)N)T)NN)FFN)-r@rArBrur—r˜r rMrGrnrormZ_single_line_singleZ_single_line_doubleZ_multi_line_singleZ_multi_line_doublerqrõrrrr®rçr'r=r3r?rAr+rKrJrLr^rdrerQrOr&rurxryrzr$r—r™ršrrrrr"/s€ õ  õèø  ÷    þüü :`  u  ( G  1#  rÿ P r"c@s"eZdZdZdd„Zddd„ZdS) r#aÖ A simple validator. Can be used to check that all members expected are present. To use it, provide a configspec with all your members in (the value given will be ignored). Pass an instance of ``SimpleVal`` to the ``validate`` method of your ``ConfigObj``. ``validate`` will return ``True`` if all members are present, or a dictionary with True/False meaning present/missing. (Whole missing sections will be replaced with ``False``) cCs t|_dSrD)rr‹rxrrrrrS szSimpleVal.__init__FcCs|r | ¡‚|S)z9A dummy check method, always returns the value unchanged.)r‹)rHrŒÚmemberr‰rrrrŒV szSimpleVal.checkN)F)r@rArBrurrrŒrrrrr#G s r#cCsØ|durg}g}|dkr t|ƒS|dks2t|tƒr^| |dd…d|f¡|rV| ¡t|ƒSt| ¡ƒD]X\}}|dkr|qjt| |¡tƒrª| |¡t |||||ƒqj| |dd…||f¡qj|rÐ| ¡t|ƒS)aã An example function that will turn a nested dictionary of results (as returned by ``ConfigObj.validate``) into a flat list. ``cfg`` is the ConfigObj instance being checked, ``res`` is the results dictionary returned by ``validate``. (This is a recursive function, so you shouldn't use the ``levels`` or ``results`` arguments - they are used by the function.) Returns a list of keys that failed. Each member of the list is a tuple:: ([list of sections...], key, result) If ``validate`` was called with ``preserve_errors=False`` (the default) then ``result`` will always be ``False``. *list of sections* is a flattened list of sections that the key was found in. If the section was missing (or a section was expected and a scalar provided - or vice-versa) then key will be ``None``. If the value (or section) was missing then ``result`` will be ``False``. If ``validate`` was called with ``preserve_errors=True`` and a value was present, but failed the check, then ``result`` will be the exception object returned. You can use this as a string that describes the failure. For example *The value "3" is of the wrong type*. NTF) Úsortedr_rñrÈrÑrKr¯rr¦r+)ZcfgÚresZlevelsÚresultsr‡r”rrrr+] s*  r+rcsRg}| ‡fdd„|jDƒ¡|jD](}||jvr$| t||ˆ|fƒ¡q$|S)aô Find all the values and sections not in the configspec from a validated ConfigObj. ``get_extra_values`` returns a list of tuples where each tuple represents either an extra section, or an extra value. The tuples contain two values, a tuple representing the section the value is in and the name of the extra values. For extra values in the top level section the first member will be an empty tuple. For values in the 'foo' section the first member will be ``('foo',)``. For members in the 'bar' subsection of the 'foo' section the first member will be ``('foo', 'bar')``. NOTE: If you call ``get_extra_values`` on a ConfigObj instance that hasn't been validated it will return an empty list. csg|] }ˆ|f‘qSrr)rÁr[©Ú_prependrrr¬ rÃz$get_extra_values..)r‚r¸r³r,)Zconfr rôr[rrŸrr,™ s   r,)NN)r);rr—r„Úcodecsrrrrr¾Z_versionrr:r4rr6rrgrhrbrcrlrkÚobjectrÐÚ__all__rrZMAX_INTERPOL_DEPTHr r?rñr*rCZ_builderr8rqrrrrwr(r r!r$r%r'r&r)r|r™rœr»r¥r¦r“r"r#r+r,rrrrÚsÊ ü ñû ó 7    | þi" <