U AT^fc@sdZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddddd d d d d ddddddddddddgZ eZdZdZGdddeZGdddeZGdddeZGd 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 objects 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)ChainMapNNoSectionErrorDuplicateOptionErrorDuplicateSectionError NoOptionErrorInterpolationErrorInterpolationDepthErrorInterpolationMissingOptionErrorInterpolationSyntaxError ParsingErrorMissingSectionHeaderError ConfigParserSafeConfigParserRawConfigParser InterpolationBasicInterpolationExtendedInterpolationLegacyInterpolation SectionProxyConverterMapping DEFAULTSECTMAX_INTERPOLATION_DEPTHZDEFAULTi c@s&eZdZdZdddZddZeZdS) Errorz'Base class for ConfigParser exceptions.cCs||_t||dSN)message Exception__init__)selfmsgr $/usr/lib64/python3.8/configparser.pyrszError.__init__cC|jSr)rrr r r!__repr__zError.__repr__N)r)__name__ __module__ __qualname____doc__rr$Z__str__r r r r!rs rc@eZdZdZddZdS)rz2Raised when no section matches a requested option.cCs$t|d|f||_|f|_dS)NzNo section: %r)rrsectionargsrr+r r r!rszNoSectionError.__init__Nr&r'r(r)rr r r r!rc@eZdZdZdddZdS)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|d||d|||}n |ddt|d|||_||_ ||_ |||f|_ dS)N already existsWhile reading from  [line {0:2d}]z : section rzSection r) reprappendformatextendinsertrrjoinr+sourcelinenor,)rr+r:r;rrr r r!rs     zDuplicateSectionError.__init__NNr.r r r r!rsc@r0)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@|d||d|||}n |ddt|d|||_||_ ||_ ||_ ||||f|_ dS) Nz in section r1r2r3z : option rzOption r) r4r5r6r7r8rrr9r+optionr:r;r,)rr+r=r:r;rrr r r!rs"    zDuplicateOptionError.__init__r<r.r r r r!rsc@r*)rz!A requested option was not found.cCs.t|d||f||_||_||f|_dS)NzNo option %r in section: %rrrr=r+r,)rr=r+r r r!rs zNoOptionError.__init__Nr.r r r r!rr/c@r*)rz0Base class for interpolation-related exceptions.cCs(t||||_||_|||f|_dSrr>)rr=r+rr r r!rs zInterpolationError.__init__Nr.r r r r!rr/c@r*)r zAA string substitution required a setting which was not available.cCs8d||||}t||||||_||||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})r6rr referencer,)rr=r+rawvalr?rr r r!r sz(InterpolationMissingOptionError.__init__Nr.r r r r!r r/c@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@r*)rz0Raised when substitutions are nested too deeply.cCs0d||t|}t|||||||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})r6rrrr,)rr=r+r@rr r r!rsz InterpolationDepthError.__init__Nr.r r r r!rr/c@s<eZdZdZd ddZeddZejddZdd ZdS) r z>Raised when a configuration file does not follow legal syntax.NcCsT|r|rtdn|s$|s$tdn|r,|}t|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) ValueErrorrrr:errorsr,)rr:filenamer r r!r,s  zParsingError.__init__cCstjdtdd|jS)zDeprecated, use `source'.SThe 'filename' attribute will be removed in future versions. Use 'source' instead.Z stacklevelwarningswarnDeprecationWarningr:r#r r r!rC; zParsingError.filenamecCstjdtdd||_dS)zDeprecated, user `source'.rDrErFNrGrvaluer r r!rCErKcCs*|j||f|jd||f7_dS)Nz [line %2d]: %s)rBr5r)rr;liner r r!r5OzParsingError.appendr<) r&r'r(r)rpropertyrCZsetterr5r r r r!r )s   c@r*)r z@Raised when a key-value pair is found before any section header.cCs8t|d|||f||_||_||_|||f|_dS)Nz7File contains no section headers. file: %r, line: %d %r)rrr:r;rNr,)rrCr;rNr r r!rWsz"MissingSectionHeaderError.__init__Nr.r r r r!r Tr/c@s0eZdZdZddZddZddZdd Zd S) rzBDummy interpolation that passes the value through with no changes.cC|Srr )rparserr+r=rMdefaultsr r r! before_getkr%zInterpolation.before_getcCrQrr rrRr+r=rMr r r! before_setnr%zInterpolation.before_setcCrQrr rUr r r! before_readqr%zInterpolation.before_readcCrQrr rUr r r! before_writetr%zInterpolation.before_writeN)r&r'r(r)rTrVrWrXr r r r!rhs c@2eZdZdZedZddZddZddZ 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 %\(([^)]+)\)sc C$g}|||||||dd|SNr_interpolate_somer9rrRr+r=rMrSZLr r r!rTzBasicInterpolation.before_getcC<|dd}|jd|}d|kr8td||df|S)Nz%%r%1invalid interpolation syntax in %r at position %dreplace_KEYCREsubrAfindrrRr+r=rMZ tmp_valuer r r!rV  zBasicInterpolation.before_setc Csj|j||d|d}|tkr&t||||rf|d} | dkrL||dS| dkrr||d| || d}|dd} | dkr|d|dd}q&| dkrR|j|} | dkrt||d||| d} || d}z || } Wn&t k rt |||| dYnXd| krF| |||| |||dn || q&t||d |fq&dS) NTrawfallbackrbrr\rEz('bad interpolation variable reference %rz/'%%' must be followed by '%%' or '(', found: %r)getrrrhr5rfmatchr optionxformgroupendKeyErrorr r^)rrRr=accumrestr+mapdepthr@pcmZvarvr r r!r^s`            z$BasicInterpolation._interpolate_someN r&r'r(r)recompilerfrTrVr^r r r r!rxs  c@rY) rzyAdvanced variant of interpolation, supports the syntax used by `zc.buildout'. Enables interpolation between sections.z \$\{([^}]+)\}c CrZr[r]r_r r r!rTr`z ExtendedInterpolation.before_getcCra)Nz$$r$rcrdrir r r!rVrjz ExtendedInterpolation.before_setc Cs|j||d|d}|tkr&t||||r|d} | dkrL||dS| dkrr||d| || d}|dd} | dkr|d|dd}q&| dkr|j|} | dkrt||d|| d d } || d}|} |}zrt | dkr| | d}||}nHt | dkrR| d} | | d}|j| |dd }nt||d |fWn2t ttfk rt|||d | dYnXd|kr|||||| t|j| dd |dn ||q&t||d |fq&dS) NTrkrrr\rEZ{rn:)rlzMore than one ':' found: %rz-'$' must be followed by '$' or '{', found: %r)rorrrhr5rfrpr rrZsplitrslenrqrtrrr r9r^dictitems)rrRr=rurvr+rwrxr@ryrzr{ZpathZsectZoptr|r r r!r^sx           z'ExtendedInterpolation._interpolate_someNr}r r r r!rs  c@s6eZdZdZedZddZddZe ddZ d S) rz{Deprecated interpolation used in old versions of ConfigParser. Use BasicInterpolation or ExtendedInterpolation instead.z%\(([^)]*)\)s|.c Cs|}t}|r|d8}|rd|krtj|j|d}|j||}z ||}Wqtk r} zt|||| jddW5d} ~ XYqXqqq|rd|krt ||||S)Nr\z%()rRr) r functoolspartial_interpolation_replacerfrgrtr r,r) rrRr+r=rMvarsr@rxreer r r!rTs0    zLegacyInterpolation.before_getcCrQrr rUr r r!rV$r%zLegacyInterpolation.before_setcCs,|d}|dkr|Sd||SdS)Nr\z%%(%s)s)rrrq)rprRZsr r r!r's z*LegacyInterpolation._interpolation_replaceN) r&r'r(r)r~rrfrTrVZ staticmethodrr r r r!r s  c s6eZdZdZdZdZdZeZe ee j Z e ej dde j Ze ej dde j Ze dZddddd d d d d Zd ed fd d d ddeeedddZddZddZddZddZddZddddZdeddZdfd d!Zdgd#d$Zdhd%d&Zd d ed'd(d)Z d*d+Z!d d ed'd,d-Z"d d ed'd.d/Z#d d ed'd0d1Z$d d ed'd2d3Z%ed d ffd4d5 Z&d6d7Z'd8d9Z(d:d;Z)didd?Z+d@dAZ,dBdCZ-dDdEZ.dFdGZ/dHdIZ0dJdKZ1dLdMZ2dNdOZ3dPdQZ4dRdSZ5dTdUZ6dVdWZ7dXdYZ8dZd[Z9d\d]Z:d^d^d^d_d`daZ;eS)krz,ConfigParser that does not do interpolation.z \[ # [ (?P
[^]]+) # very permissive! \] # ] a (?P