[@s\dZddlmZddlmZmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddddd d d d d dddddgZdZdZGdddeZGdddeZGdddeZGdddeZGdddeZGdd d eZGdddeZGdd d eZGdd d eZGd d d eZGd!d d eZeZGd"d#d#ZGd$d%d%eZ Gd&d'd'eZ!Gd(d)d)eZ"Gd*ddeZ#Gd+dde#Z$Gd,dde$Z%Gd-d.d.eZ&dS)/aConfiguration 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): 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. 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 list 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 NoOptionErrorInterpolationErrorInterpolationDepthErrorInterpolationSyntaxError ParsingErrorMissingSectionHeaderError ConfigParserSafeConfigParserRawConfigParser DEFAULTSECTMAX_INTERPOLATION_DEPTHZDEFAULT c@s7eZdZdZdddZddZeZdS)Errorz'Base class for ConfigParser exceptions.cCs||_tj||dS)N)message Exception__init__)selfmsgr"/usr/lib/python3.4/configparser.pyrs zError.__init__cCs|jS)N)r)rrrr__repr__szError.__repr__N)__name__ __module__ __qualname____doc__rr__str__rrrrrs  rc@s"eZdZdZddZdS)rz2Raised when no section matches a requested option.cCs0tj|d|f||_|f|_dS)NzNo section: %r)rrsectionargs)rr#rrrrs zNoSectionError.__init__N)rrr r!rrrrrrs c@s(eZdZdZddddZdS)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 rxdt|g}|dk rU|jdj|n|jd|j||}n|jddtj|dj|||_||_ ||_ |||f|_ dS)Nz already existszWhile reading from z [line {0:2d}]z : section rzSection r) reprappendformatextendinsertrrjoinr#sourcelinenor$)rr#r+r,rrrrrrs        zDuplicateSectionError.__init__)rrr r!rrrrrrs c@s(eZdZdZddddZdS)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 rdt|g}|dk ra|jdj|n|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(r)rrr*r#optionr+r,r$)rr#r-r+r,rrrrrrs           zDuplicateOptionError.__init__)rrr r!rrrrrrs c@s"eZdZdZddZdS)rz!A requested option was not found.cCs?tj|d||f||_||_||f|_dS)NzNo option %r in section: %r)rrr-r#r$)rr-r#rrrrs    zNoOptionError.__init__N)rrr r!rrrrrrs c@s"eZdZdZddZdS)r z0Base class for interpolation-related exceptions.cCs8tj||||_||_|||f|_dS)N)rrr-r#r$)rr-r#rrrrrs  zInterpolationError.__init__N)rrr r!rrrrrr s c@s"eZdZdZddZdS)InterpolationMissingOptionErrorzAA string substitution required a setting which was not available.cCsNd||||f}tj||||||_||||f|_dS)NzNBad value substitution: section: [%s] option : %s key : %s rawval : %s )r r referencer$)rr-r#rawvalr/rrrrrs  z(InterpolationMissingOptionError.__init__N)rrr r!rrrrrr.s 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)rrr r!rrrrr s c@s"eZdZdZddZdS)r z0Raised when substitutions are nested too deeply.cCs?d|||f}tj|||||||f|_dS)NzSValue interpolation too deeply recursive: section: [%s] option : %s rawval : %s )r rr$)rr-r#r0rrrrr sz InterpolationDepthError.__init__N)rrr r!rrrrrr s c@s[eZdZdZddddZeddZejddZdd ZdS) r z>Raised when a configuration file does not follow legal syntax.NcCs}|r|rtdn,| r8| r8tdn|rG|}ntj|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+filenamerrrrs    zParsingError.__init__cCstjdtdd|jS)zDeprecated, use `source'.zSThe 'filename' attribute will be removed in future versions. Use 'source' instead. stacklevel)warningswarnDeprecationWarningr+)rrrrr3&s zParsingError.filenamecCs#tjdtdd||_dS)zDeprecated, user `source'.zSThe 'filename' attribute will be removed in future versions. Use 'source' instead.r4r5N)r6r7r8r+)rvaluerrrr30s cCs3|jj||f|jd||f7_dS)Nz [line %2d]: %s)r2r&r)rr,linerrrr&:szParsingError.append) rrr r!rpropertyr3setterr&rrrrr s   c@s"eZdZdZddZdS)r z@Raised when a key-value pair is found before any section header.cCsNtj|d|||f||_||_||_|||f|_dS)Nz7File contains no section headers. file: %r, line: %d %r)rrr+r,r:r$)rr3r,r:rrrrBs   z"MissingSectionHeaderError.__init__N)rrr r!rrrrrr ?s c@sFeZdZdZddZddZddZdd Zd S) InterpolationzBDummy interpolation that passes the value through with no changes.cCs|S)Nr)rparserr#r-r9defaultsrrr before_getVszInterpolation.before_getcCs|S)Nr)rr>r#r-r9rrr before_setYszInterpolation.before_setcCs|S)Nr)rr>r#r-r9rrr before_read\szInterpolation.before_readcCs|S)Nr)rr>r#r-r9rrr before_write_szInterpolation.before_writeN)rrr r!r@rArBrCrrrrr=Ss    r=c@sIeZdZdZejdZddZddZddZ d S) BasicInterpolationa!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 %\(([^)]+)\)scCs2g}|j||||||ddj|S)Nr)_interpolate_somer*)rr>r#r-r9r?Lrrrr@tszBasicInterpolation.before_getcCsY|jdd}|jjd|}d|krUtd||jdfn|S)Nz%%r%z1invalid interpolation syntax in %r at position %d)replace_KEYCREsubr1find)rr>r#r-r9 tmp_valuerrrrAys  zBasicInterpolation.before_setc Cs|tkr!t|||nx|r|jd}|dkrV|j|dS|dkr|j|d|||d}n|dd} | dkr|jd|dd}q$| dkr|jj|} | dkr t||d|n|j| jd} || j d}y|| } Wn't k rpt |||| YnXd| kr|j |||| |||dq|j| q$t||d|fq$WdS)NrHrrEr5(z'bad interpolation variable reference %rz/'%%' must be followed by '%%' or '(', found: %r) rr rLr&rJmatchr optionxformgroupendKeyErrorr.rF) rr>r-accumrestr#mapdepthpcmZvarvrrrrFsD            z$BasicInterpolation._interpolate_someN) rrr r!recompilerJr@rArFrrrrrDcs   rDc@sIeZdZdZejdZddZddZddZ d S) ExtendedInterpolationzyAdvanced variant of interpolation, supports the syntax used by `zc.buildout'. Enables interpolation between sections.z \$\{([^}]+)\}cCs2g}|j||||||ddj|S)NrEr)rFr*)rr>r#r-r9r?rGrrrr@sz ExtendedInterpolation.before_getcCsY|jdd}|jjd|}d|krUtd||jdfn|S)Nz$$r$z1invalid interpolation syntax in %r at position %d)rIrJrKr1rL)rr>r#r-r9rMrrrrAs  z ExtendedInterpolation.before_setcCs|tkr!t|||nxj|r|jd}|dkrV|j|dS|dkr|j|d|||d}n|dd} | dkr|jd|dd}q$| dkrq|jj|} | dkr t||d|n| jdjd} || j d}|} |} yt | dkrz|j | d} || }nct | dkr| d} |j | d} |j | | dd }nt||d |fWn9t ttfk rt|||dj| YnXd|kra|j|| ||| t|j| dd |dq|j|q$t||d |fq$WdS) Nr_rrEr5{z'bad interpolation variable reference %r:rawTzMore than one ':' found: %rz-'$' must be followed by '$' or '{', found: %r)rr rLr&rJrOr rQsplitrRlenrPgetrSrrr.r*rFdictitems)rr>r-rTrUr#rVrWrXrYrZpathZsectZoptr[rrrrFs\              z'ExtendedInterpolation._interpolate_someN) rrr r!r\r]rJr@rArFrrrrr^s   r^c@sOeZdZdZejdZddZddZe ddZ d S) LegacyInterpolationz{Deprecated interpolation used in old versions of ConfigParser. Use BasicInterpolation or ExtendedInterpolation instead.z%\(([^)]*)\)s|.c Cs|}t}x|r|d8}|rd|krtj|jd|}|jj||}y||}Wqtk r} z!t|||| jdWYdd} ~ XqXqPqW|rd|krt |||n|S)NrEz%(r>r) r functoolspartial_interpolation_replacerJrKrSr.r$r ) rr>r#r-r9varsr0rWrIerrrr@s"   /zLegacyInterpolation.before_getcCs|S)Nr)rr>r#r-r9rrrrA szLegacyInterpolation.before_setcCs:|jd}|dkr%|jSd|j|SdS)NrEz%%(%s)s)rQrP)rOr>srrrrls  z*LegacyInterpolation._interpolation_replaceN) rrr r!r\r]rJr@rA staticmethodrlrrrrris   ricsBeZdZdZdZdZdZeZe j ee j Z e j ej dde j Ze j ej dde j Ze j dZidd 6dd 6dd 6dd 6d d6d d6d d6d d6Zded ddqddrdddddddededdZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zdd*d+Zdd,d-Zd.d/d0Zd1d2d3Zdd4d5Zd6d d7dd8ed9d:Z d;d<Z!d6d d7dd8ed=d>Z"d6d d7dd8ed?d@Z#d6d d7dd8edAdBZ$ed dfdCdDZ%dEdFZ&dGdHZ'dIdJZ(ddKdLZ)ddMdNZ*dOdPZ+dQdRZ,dSdTZ-dUdVZ.dWdXZ/dYdZZ0d[d\Z1d]d^Z2d_d`Z3dadbZ4dcddZ5dedfZ6dgdhZ7didjZ8dkdldmdldndldodpZ9S)srz,ConfigParser that does not do interpolation.z \[ # [ (?P
[^]]+) # very permissive! \] # ] a (?P