3 \X@sdZddlmZddlmZmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddddd d d d d ddddddddddddgZdZdZGdddeZGdddeZGdddeZGd ddeZGd!ddeZGd"d d eZGd#d d eZGd$d d eZGd%d d eZGd&d d eZGd'ddeZeZGd(ddZ Gd)dde Z!Gd*dde Z"Gd+dde Z#Gd,ddeZ$Gd-dde$Z%Gd.dde%Z&Gd/ddeZ'Gd0ddeZ(dS)1aConfiguration file parser. A configuration file consists of sections, lead by a "[section]" header, and followed by "name: value" entries, with continuations and such in the style of RFC 822. Intrinsic defaults can be specified by passing them into the ConfigParser constructor as a dictionary. class: ConfigParser -- responsible for parsing a list of configuration files, and managing the parsed database. methods: __init__(defaults=None, dict_type=_default_dict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=, converters=): Create the parser. When `defaults' is given, it is initialized into the dictionary or intrinsic defaults. The keys must be strings, the values must be appropriate for %()s string interpolation. When `dict_type' is given, it will be used to create the dictionary objects for the list of sections, for the options within a section, and for the default values. When `delimiters' is given, it will be used as the set of substrings that divide keys from values. When `comment_prefixes' is given, it will be used as the set of substrings that prefix comments in empty lines. Comments can be indented. When `inline_comment_prefixes' is given, it will be used as the set of substrings that prefix comments in non-empty lines. When `strict` is True, the parser won't allow for any section or option duplicates while reading from a single source (file, string or dictionary). Default is True. When `empty_lines_in_values' is False (default: True), each empty line marks the end of an option. Otherwise, internal empty lines of a multiline option are kept as part of the value. When `allow_no_value' is True (default: False), options without values are accepted; the value presented for these is None. When `default_section' is given, the name of the special section is named accordingly. By default it is called ``"DEFAULT"`` but this can be customized to point to any other valid section name. Its current value can be retrieved using the ``parser_instance.default_section`` attribute and may be modified at runtime. When `interpolation` is given, it should be an Interpolation subclass instance. It will be used as the handler for option value pre-processing when using getters. RawConfigParser object s don't do any sort of interpolation, whereas ConfigParser uses an instance of BasicInterpolation. The library also provides a ``zc.buildbot`` inspired ExtendedInterpolation implementation. When `converters` is given, it should be a dictionary where each key represents the name of a type converter and each value is a callable implementing the conversion from string to the desired datatype. Every converter gets its corresponding get*() method on the parser object and section proxies. sections() Return all the configuration section names, sans DEFAULT. has_section(section) Return whether the given section exists. has_option(section, option) Return whether the given option exists in the given section. options(section) Return list of configuration options for the named section. read(filenames, encoding=None) Read and parse the iterable of named configuration files, given by name. A single filename is also allowed. Non-existing files are ignored. Return list of successfully read files. read_file(f, filename=None) Read and parse one configuration file, given as a file object. The filename defaults to f.name; it is only used in error messages (if f has no `name' attribute, the string `' is used). read_string(string) Read configuration from a given string. read_dict(dictionary) Read configuration from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section. If the used dictionary type preserves order, sections and their keys will be added in order. Values are automatically converted to strings. get(section, option, raw=False, vars=None, fallback=_UNSET) Return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents override any pre-existing defaults. If `option' is a key in `vars', the value from `vars' is used. getint(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to an integer. getfloat(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a float. getboolean(section, options, raw=False, vars=None, fallback=_UNSET) Like get(), but convert value to a boolean (currently case insensitively defined as 0, false, no, off for False, and 1, true, yes, on for True). Returns False or True. items(section=_UNSET, raw=False, vars=None) If section is given, return a list of tuples with (name, value) for each option in the section. Otherwise, return a list of tuples with (section_name, section_proxy) for each section, including DEFAULTSECT. remove_section(section) Remove the given file section and all its options. remove_option(section, option) Remove the given option from the given section. set(section, option, value) Set the given option. write(fp, space_around_delimiters=True) Write the configuration state in .ini format. If `space_around_delimiters' is True (the default), delimiters between keys and values are surrounded by spaces. )MutableMapping) OrderedDictChainMapNNoSectionErrorDuplicateOptionErrorDuplicateSectionError NoOptionErrorInterpolationErrorInterpolationDepthErrorInterpolationMissingOptionErrorInterpolationSyntaxError ParsingErrorMissingSectionHeaderError ConfigParserSafeConfigParserRawConfigParser InterpolationBasicInterpolationExtendedInterpolationLegacyInterpolation SectionProxyConverterMapping DEFAULTSECTMAX_INTERPOLATION_DEPTHZDEFAULT c@s&eZdZdZdddZddZeZdS) Errorz'Base class for ConfigParser exceptions.cCs||_tj||dS)N)message Exception__init__)selfmsgr"$/usr/lib64/python3.6/configparser.pyrszError.__init__cCs|jS)N)r)r r"r"r#__repr__szError.__repr__N)r)__name__ __module__ __qualname____doc__rr$__str__r"r"r"r#rs rc@seZdZdZddZdS)rz2Raised when no section matches a requested option.cCs$tj|d|f||_|f|_dS)NzNo section: %r)rrsectionargs)r r*r"r"r#rszNoSectionError.__init__N)r%r&r'r(rr"r"r"r#rsc@seZdZdZdddZdS)raRaised when a section is repeated in an input source. Possible repetitions that raise this exception are: multiple creation using the API or in strict parsers when a section is found more than once in a single input file, string or dictionary. NcCst|dg}|dk rRdt|g}|dk r8|jdj||jd|j||}n |jddtj|dj|||_||_ ||_ |||f|_ dS)Nz already existszWhile reading from z [line {0:2d}]z : section rzSection r) reprappendformatextendinsertrrjoinr*sourcelinenor+)r r*r2r3r!rr"r"r#rs     zDuplicateSectionError.__init__)NN)r%r&r'r(rr"r"r"r#rsc@seZdZdZdddZdS)rzRaised by strict parsers when an option is repeated in an input source. Current implementation raises this exception only when an option is found more than once in a single file, string or dictionary. NcCst|dt|dg}|dk rZdt|g}|dk r@|jdj||jd|j||}n |jddtj|dj|||_||_ ||_ ||_ ||||f|_ dS) Nz in section z already existszWhile reading from z [line {0:2d}]z : option rzOption r) r,r-r.r/r0rrr1r*optionr2r3r+)r r*r4r2r3r!rr"r"r#rs     zDuplicateOptionError.__init__)NN)r%r&r'r(rr"r"r"r#rsc@seZdZdZddZdS)rz!A requested option was not found.cCs.tj|d||f||_||_||f|_dS)NzNo option %r in section: %r)rrr4r*r+)r r4r*r"r"r#rs  zNoOptionError.__init__N)r%r&r'r(rr"r"r"r#rsc@seZdZdZddZdS)r z0Base class for interpolation-related exceptions.cCs(tj||||_||_|||f|_dS)N)rrr4r*r+)r r4r*r!r"r"r#rs zInterpolationError.__init__N)r%r&r'r(rr"r"r"r#r sc@seZdZdZddZdS)r zAA string substitution required a setting which was not available.cCs8dj||||}tj||||||_||||f|_dS)NzBad value substitution: option {!r} in section {!r} contains an interpolation key {!r} which is not a valid option name. Raw value: {!r})r.r r referencer+)r r4r*rawvalr5r!r"r"r#r s  z(InterpolationMissingOptionError.__init__N)r%r&r'r(rr"r"r"r#r sc@seZdZdZdS)r zRaised when the source text contains invalid syntax. Current implementation raises this exception when the source text into which substitutions are made does not conform to the required syntax. N)r%r&r'r(r"r"r"r#r sc@seZdZdZddZdS)r z0Raised when substitutions are nested too deeply.cCs0dj||t|}tj|||||||f|_dS)NzRecursion limit exceeded in value substitution: option {!r} in section {!r} contains an interpolation key which cannot be substituted in {} steps. Raw value: {!r})r.rr rr+)r r4r*r6r!r"r"r#rs z InterpolationDepthError.__init__N)r%r&r'r(rr"r"r"r#r sc@s<eZdZdZd ddZeddZejddZdd ZdS) r z>Raised when a configuration file does not follow legal syntax.NcCsX|r|rtdn| r(| r(tdn|r0|}tj|d|||_g|_|f|_dS)Nz:Cannot specify both `filename' and `source'. Use `source'.z%Required argument `source' not given.z"Source contains parsing errors: %r) ValueErrorrrr2errorsr+)r r2filenamer"r"r#r+s   zParsingError.__init__cCstjdtdd|jS)zDeprecated, use `source'.zSThe 'filename' attribute will be removed in future versions. Use 'source' instead.) stacklevel)warningswarnDeprecationWarningr2)r r"r"r#r9:s zParsingError.filenamecCstjdtdd||_dS)zDeprecated, user `source'.zSThe 'filename' attribute will be removed in future versions. Use 'source' instead.r:)r;N)r<r=r>r2)r valuer"r"r#r9Ds cCs*|jj||f|jd||f7_dS)Nz [line %2d]: %s)r8r-r)r r3liner"r"r#r-NszParsingError.append)NN) r%r&r'r(rpropertyr9setterr-r"r"r"r#r (s    c@seZdZdZddZdS)rz@Raised when a key-value pair is found before any section header.cCs8tj|d|||f||_||_||_|||f|_dS)Nz7File contains no section headers. file: %r, line: %d %r)rrr2r3r@r+)r r9r3r@r"r"r#rVsz"MissingSectionHeaderError.__init__N)r%r&r'r(rr"r"r"r#rSsc@s0eZdZdZddZddZddZdd Zd S) rzBDummy interpolation that passes the value through with no changes.cCs|S)Nr")r parserr*r4r?defaultsr"r"r# before_getjszInterpolation.before_getcCs|S)Nr")r rCr*r4r?r"r"r# before_setmszInterpolation.before_setcCs|S)Nr")r rCr*r4r?r"r"r# before_readpszInterpolation.before_readcCs|S)Nr")r rCr*r4r?r"r"r# before_writesszInterpolation.before_writeN)r%r&r'r(rErFrGrHr"r"r"r#rgs c@s2eZdZdZejdZddZddZddZ d S) ra!Interpolation as implemented in the classic ConfigParser. The option values can contain format strings which refer to other values in the same section, or values in the special default section. For example: something: %(dir)s/whatever would resolve the "%(dir)s" to the value of dir. All reference expansions are done late, on demand. If a user needs to use a bare % in a configuration file, she can escape it by writing %%. Other % usage is considered a user error and raises `InterpolationSyntaxError'.z %\(([^)]+)\)scCs$g}|j||||||ddj|S)Nr)_interpolate_somer1)r rCr*r4r?rDLr"r"r#rEszBasicInterpolation.before_getcCs<|jdd}|jjd|}d|kr8td||jdf|S)Nz%%r%z1invalid interpolation syntax in %r at position %d)replace_KEYCREsubr7find)r rCr*r4r? tmp_valuer"r"r#rFs  zBasicInterpolation.before_setcCsp|j||d|d}|tkr&t|||xB|rj|jd} | dkrP|j|dS| dkrv|j|d| || d}|dd} | dkr|jd|dd}q*| dkrV|jj|} | dkrt||d||j| j d} || j d}y || } Wn&t k r"t |||| dYnXd| krJ|j |||| |||dn |j| q*t||d |fq*WdS) NT)rawfallbackrLrrIr:(z'bad interpolation variable reference %rz/'%%' must be followed by '%%' or '(', found: %r)getrr rPr-rNmatchr optionxformgroupendKeyErrorr rJ)r rCr4accumrestr*mapdepthr6pcmvarvr"r"r#rJsF              z$BasicInterpolation._interpolate_someN) r%r&r'r(recompilerNrErFrJr"r"r"r#rws   c@s2eZdZdZejdZddZddZddZ d S) rzyAdvanced variant of interpolation, supports the syntax used by `zc.buildout'. Enables interpolation between sections.z \$\{([^}]+)\}cCs$g}|j||||||ddj|S)NrIr)rJr1)r rCr*r4r?rDrKr"r"r#rEsz ExtendedInterpolation.before_getcCs<|jdd}|jjd|}d|kr8td||jdf|S)Nz$$r$z1invalid interpolation syntax in %r at position %d)rMrNrOr7rP)r rCr*r4r?rQr"r"r#rFs  z ExtendedInterpolation.before_setcCs|j||d|d}|tkr&t|||x|r|jd} | dkrP|j|dS| dkrv|j|d| || d}|dd} | dkr|jd|dd}q*| dkr|jj|} | dkrt||d|| jdj d } || j d}|} |}yrt | dkr |j | d}||}nHt | dkrV| d} |j | d}|j| |dd }nt||d |fWn2t ttfk rt|||d j| dYnXd|kr|j||||| t|j| dd |dn |j|q*t||d |fq*WdS) NT)rRrSrfrrIr:{z'bad interpolation variable reference %r:)rRzMore than one ':' found: %rz-'$' must be followed by '$' or '{', found: %r)rUrr rPr-rNrVr rXsplitrYlenrWrZrrr r1rJdictitems)r rCr4r[r\r*r]r^r6r_r`rapathZsectoptrcr"r"r#rJs^              z'ExtendedInterpolation._interpolate_someN) r%r&r'r(rdrerNrErFrJr"r"r"r#rs  c@s6eZdZdZejdZddZddZe ddZ d S) rz{Deprecated interpolation used in old versions of ConfigParser. Use BasicInterpolation or ExtendedInterpolation instead.z%\(([^)]*)\)s|.c Cs|}t}x|r|d8}|rd|krtj|j|d}|jj||}y ||}Wqtk r} zt|||| jddWYdd} ~ XqXq Pq W|rd|krt ||||S)NrIz%()rCr) r functoolspartial_interpolation_replacerNrOrZr r+r ) r rCr*r4r?varsr6r^rMer"r"r#rEs"  (  zLegacyInterpolation.before_getcCs|S)Nr")r rCr*r4r?r"r"r#rF#szLegacyInterpolation.before_setcCs,|jd}|dkr|jSd|j|SdS)NrIz%%(%s)s)rXrW)rVrCsr"r"r#rq&s z*LegacyInterpolation._interpolation_replaceN) r%r&r'r(rdrerNrErF staticmethodrqr"r"r"r#r s  c s.eZdZdZdZdZdZeZe j ee j Z e j ej dde j Ze j ej dde j Ze j dZddddd d d d d Zd ed fddded ddeeedddZddZddZddZddZddZdfddZdgdd Zdhd"d#Zdid%d&Zdjd'd(Zd d ed)d*d+Z d,d-Z!d d ed)d.d/Z"d d ed)d0d1Z#d d ed)d2d3Z$d d ed)d4d5Z%ed d ffd6d7 Z&d8d9Z'd:d;Z(dd?Z*dld@dAZ+dBdCZ,dDdEZ-dFdGZ.dHdIZ/dJdKZ0dLdMZ1dNdOZ2dPdQZ3dRdSZ4dTdUZ5dVdWZ6dXdYZ7dZd[Z8d\d]Z9d^d^d^d_d`daZ:e;dbdcZ<Z=S)mrz,ConfigParser that does not do interpolation.z \[ # [ (?P
[^]]+) # very permissive! \] # ] a (?P