Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/ConfigParser.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�
��c@sjdZddkZddddddd	d
ddd
ddg
ZdZdZdefd��YZdefd��YZdefd��YZdefd��YZ	defd��YZ
de
fd��YZde
fd��YZde
fd��YZ
d	efd��YZd
efd��YZd
fd��YZdefd��YZdefd ��YZdS(!sConfiguration file parser.

A setup 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.

The option values can contain format strings which refer to other values in
the same section, or values in a 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.

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)
        create the parser and specify a dictionary of intrinsic defaults.  The
        keys must be strings, the values must be appropriate for %()s string
        interpolation.  Note that `__name__' is always an intrinsic default;
        its value is the section's name.

    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)
        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.

    readfp(fp, filename=None)
        read and parse one configuration file, given as a file object.
        The filename defaults to fp.name; it is only used in error
        messages (if fp has no `name' attribute, the string `<???>' is used).

    get(section, option, raw=False, vars=None)
        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.

    getint(section, options)
        like get(), but convert value to an integer

    getfloat(section, options)
        like get(), but convert value to a float

    getboolean(section, options)
        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, raw=False, vars=None)
        return a list of tuples with (name, value) for each option
        in the section.

    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)
        write the configuration state in .ini format
i�NtNoSectionErrortDuplicateSectionErrort
NoOptionErrortInterpolationErrortInterpolationDepthErrortInterpolationSyntaxErrortParsingErrortMissingSectionHeaderErrortConfigParsertSafeConfigParsertRawConfigParsertDEFAULTSECTtMAX_INTERPOLATION_DEPTHtDEFAULTi
tErrorcBs)eZdZdd�Zd�ZeZRS(s'Base class for ConfigParser exceptions.tcCs||_ti||�dS(N(tmessaget	Exceptiont__init__(tselftmsg((s/sys/lib/python/ConfigParser.pyRms	cCs|iS(N(R(R((s/sys/lib/python/ConfigParser.pyt__repr__qs(t__name__t
__module__t__doc__RRt__str__(((s/sys/lib/python/ConfigParser.pyRjs	cBseZdZd�ZRS(s2Raised when no section matches a requested option.cCs$ti|d|f�||_dS(NsNo section: %r(RRtsection(RR((s/sys/lib/python/ConfigParser.pyRys(RRRR(((s/sys/lib/python/ConfigParser.pyRvscBseZdZd�ZRS(s*Raised when a section is multiply-created.cCs!ti|d|�||_dS(NsSection %r already exists(RRR(RR((s/sys/lib/python/ConfigParser.pyR�s(RRRR(((s/sys/lib/python/ConfigParser.pyR}scBseZdZd�ZRS(s!A requested option was not found.cCs0ti|d||f�||_||_dS(NsNo option %r in section: %r(RRtoptionR(RRR((s/sys/lib/python/ConfigParser.pyR�s	(RRRR(((s/sys/lib/python/ConfigParser.pyR�scBseZdZd�ZRS(s0Base class for interpolation-related exceptions.cCs&ti||�||_||_dS(N(RRRR(RRRR((s/sys/lib/python/ConfigParser.pyR�s	(RRRR(((s/sys/lib/python/ConfigParser.pyR�stInterpolationMissingOptionErrorcBseZdZd�ZRS(sAA string substitution required a setting which was not available.cCs9d||||f}ti||||�||_dS(NsNBad value substitution:
	section: [%s]
	option : %s
	key    : %s
	rawval : %s
(RRt	reference(RRRtrawvalRR((s/sys/lib/python/ConfigParser.pyR�s(RRRR(((s/sys/lib/python/ConfigParser.pyR�scBseZdZRS(sjRaised when the source text into which substitutions are made
    does not conform to the required syntax.(RRR(((s/sys/lib/python/ConfigParser.pyR�scBseZdZd�ZRS(s0Raised when substitutions are nested too deeply.cCs-d|||f}ti||||�dS(NsSValue interpolation too deeply recursive:
	section: [%s]
	option : %s
	rawval : %s
(RR(RRRRR((s/sys/lib/python/ConfigParser.pyR�s(RRRR(((s/sys/lib/python/ConfigParser.pyR�scBs eZdZd�Zd�ZRS(s>Raised when a configuration file does not follow legal syntax.cCs*ti|d|�||_g|_dS(Ns File contains parsing errors: %s(RRtfilenameterrors(RR((s/sys/lib/python/ConfigParser.pyR�s	cCs3|ii||f�|id||f7_dS(Ns
	[line %2d]: %s(R tappendR(Rtlinenotline((s/sys/lib/python/ConfigParser.pyR!�s(RRRRR!(((s/sys/lib/python/ConfigParser.pyR�s	cBseZdZd�ZRS(s@Raised when a key-value pair is found before any section header.cCs<ti|d|||f�||_||_||_dS(Ns7File contains no section headers.
file: %s, line: %d
%r(RRRR"R#(RRR"R#((s/sys/lib/python/ConfigParser.pyR�s		(RRRR(((s/sys/lib/python/ConfigParser.pyR�scBs7eZdd�Zd�Zd�Zd�Zd�Zd�Zd�Z	dd�Z
d�Zd	�Zd
�Z
d�Zd�Zhed
<ed<ed<ed<ed<ed<ed<ed<Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zeid�Zeid�Zd�ZRS( cCsTh|_h|_|o7x4|i�D]"\}}||i|i|�<q&WndS(N(t	_sectionst	_defaultstitemstoptionxform(Rtdefaultstkeytvalue((s/sys/lib/python/ConfigParser.pyR�s		
cCs|iS(N(R%(R((s/sys/lib/python/ConfigParser.pyR(�scCs
|ii�S(s3Return a list of section names, excluding [DEFAULT](R$tkeys(R((s/sys/lib/python/ConfigParser.pytsections�scCs1||ijot|��nh|i|<dS(s�Create a new section in the configuration.

        Raise DuplicateSectionError if a section by the specified name
        already exists.
        N(R$R(RR((s/sys/lib/python/ConfigParser.pytadd_section�scCs
||ijS(s~Indicate whether the named section is present in the configuration.

        The DEFAULT section is not acknowledged.
        (R$(RR((s/sys/lib/python/ConfigParser.pythas_section�scCsky|i|i�}Wntj
ot|��nX|i|i�d|jo|d=n|i�S(s9Return a list of option names for the given section name.R(R$tcopytKeyErrorRtupdateR%R+(RRtopts((s/sys/lib/python/ConfigParser.pytoptions�s
cCs�t|t�o
|g}ng}x^|D]V}yt|�}Wntj
o
q*nX|i||�|i�|i|�q*W|S(s�Read and parse a filename or a list of filenames.

        Files that cannot be opened are silently ignored; this is
        designed so that you can specify a list of potential
        configuration file locations (e.g. current directory, user's
        home directory, systemwide directory), and all existing
        configuration files in the list will be read.  A single
        filename may also be given.

        Return list of successfully read files.
        (t
isinstancet
basestringtopentIOErrort_readtcloseR!(Rt	filenamestread_okRtfp((s/sys/lib/python/ConfigParser.pytread�s

cCsN|djo-y
|i}Wq:tj
o
d}q:Xn|i||�dS(sLike read() but the argument must be a file-like object.

        The `fp' argument must have a `readline' method.  Optional
        second argument is the `filename', which if not given, is
        taken from fp.name.  If fp has no `name' attribute, `<???>' is
        used.

        s<???>N(tNonetnametAttributeErrorR8(RR<R((s/sys/lib/python/ConfigParser.pytreadfps	

cCs�|i|�}||ijoO|tjot|��n||ijo|i|Sq�t||��nV||i|jo|i||Sn/||ijo|i|Snt||��dS(N(R'R$RRR%R(RRRtopt((s/sys/lib/python/ConfigParser.pytget s
cCs�y|i|}Wn6tj
o*|tjot|��nh}nX|ii�}|i|�d|jo|d=n|i�S(NR(R$R0RRR%R/R1R&(RRtd2td((s/sys/lib/python/ConfigParser.pyR&0s


cCs||i||��S(N(RC(RRtconvR((s/sys/lib/python/ConfigParser.pyt_get=scCs|i|t|�S(N(RGtint(RRR((s/sys/lib/python/ConfigParser.pytgetint@scCs|i|t|�S(N(RGtfloat(RRR((s/sys/lib/python/ConfigParser.pytgetfloatCst1tyesttruetont0tnotfalsetoffcCsJ|i||�}|i�|ijotd|�n|i|i�S(NsNot a boolean: %s(RCtlowert_boolean_statest
ValueError(RRRtv((s/sys/lib/python/ConfigParser.pyt
getbooleanIscCs
|i�S(N(RT(Rt	optionstr((s/sys/lib/python/ConfigParser.pyR'OscCs�|p
|tjo |i|�}||ijSnI||ijotSn1|i|�}||i|jp
||ijSdS(s=Check for the existence of a given option in a given section.N(RR'R%R$tFalse(RRR((s/sys/lib/python/ConfigParser.pyt
has_optionRscCsl|p
|tjo
|i}n4y|i|}Wntj
ot|��nX|||i|�<dS(sSet an option.N(RR%R$R0RR'(RRRR*tsectdict((s/sys/lib/python/ConfigParser.pytset^s
c
Cs	|iok|idt�xF|ii�D]5\}}|id|t|�idd�f�q+W|id�nx�|iD]�}|id|�x[|i|i�D]F\}}|djo-|id|t|�idd�f�q�q�W|id�qWdS(s?Write an .ini-format representation of the configuration state.s[%s]
s%s = %s
s
s
	RN(R%twriteRR&tstrtreplaceR$(RR<R)R*R((s/sys/lib/python/ConfigParser.pyR^is
-

	(cCs�|p
|tjo
|i}n4y|i|}Wntj
ot|��nX|i|�}||j}|o||=n|S(sRemove an option.(RR%R$R0RR'(RRRR\texisted((s/sys/lib/python/ConfigParser.pyt
remove_optionxs
cCs(||ij}|o|i|=n|S(sRemove a file section.(R$(RRRa((s/sys/lib/python/ConfigParser.pytremove_section�ss\[(?P<header>[^]]+)\]s9(?P<option>[^:=\s][^:=]*)\s*(?P<vi>[:=])\s*(?P<value>.*)$cCs�d}d}d}d}x�to�|i�}|pPn|d}|i�djp|ddjoqn|idd�di�djo|ddjoqn|di�oG|dj	o:|o3|i�}|od|||f||<q�q|ii|�}	|	op|	i	d�}
|
|i
jo|i
|
}n7|
tjo
|i}nh|
d	<}||i
|
<d}q|djot
|||��q|ii|�}	|	o�|	i	d
dd�\}}}|djoPd|joC|id�}
|
djo#||
di�o||
 }qBn|i�}|djo
d}n|i|i��}|||<q|pt|�}n|i|t|��qW|o
|�ndS(s�Parse a sectioned setup file.

        The sections in setup file contains a title line at the top,
        indicated by a name in square brackets (`[]'), plus key/value
        options lines, indicated by `name: value' format lines.
        Continuations are represented by an embedded newline then
        leading whitespace.  Blank lines, lines beginning with a '#',
        and just about everything else are ignored.
        iiRs#;tremtrRs%s
%stheaderRRtviR*t=t:t;i�s""N(RhRi(R>tTruetreadlinetstriptsplitRTtisspacetSECTCREtmatchtgroupR$RR%RtOPTCREtfindR'trstripRR!trepr(RR<tfpnametcursecttoptnameR"teR#R*tmotsectnameRgtoptvaltpos((s/sys/lib/python/ConfigParser.pyR8�s\


$4% 




"

N(RRR>RR(R,R-R.R3R=RARCR&RGRIRKRkRZRURXR'R[R]R^RbRctretcompileRpRsR8(((s/sys/lib/python/ConfigParser.pyR
�s6			
					
			(&							
		cBsGeZedd�Zedd�Zd�Zeid�Z	d�Z
RS(c
Cs|ii�}y|i|i|�Wn0tj
o$|tjot|��qZnX|o4x1|i�D]\}}|||i|�<qnWn|i|�}y||}Wn"tj
ot	||��nX|o|Sn|i
||||�SdS(s�Get an option value for a given section.

        All % interpolations are expanded in the return values, based on the
        defaults passed into the constructor, unless the optional argument
        `raw' is true.  Additional substitutions may be provided using the
        `vars' argument, which must be a dictionary whose contents overrides
        any pre-existing defaults.

        The section DEFAULT is special.
        N(R%R/R1R$R0RRR&R'Rt_interpolate(RRRtrawtvarsRER)R*((s/sys/lib/python/ConfigParser.pyRC�s$

cCs2|ii�}y|i|i|�Wn0tj
o$|tjot|��qZnX|o4x1|i�D]\}}|||i|�<qnWn|i	�}d|jo|i
d�n|o-g}|D]}	||	||	fq�~Sn<g}
|D])}	|
|	|i||	||	|�fq�SdS(s�Return a list of tuples with (name, value) for each option
        in the section.

        All % interpolations are expanded in the return values, based on the
        defaults passed into the constructor, unless the optional argument
        `raw' is true.  Additional substitutions may be provided using the
        `vars' argument, which must be a dictionary whose contents overrides
        any pre-existing defaults.

        The section DEFAULT is special.
        RN(R%R/R1R$R0RRR&R'R+tremoveR�(RRR�R�RER)R*R3t_[1]Rt_[2]((s/sys/lib/python/ConfigParser.pyR&s$


&cCs�|}t}x|ow|d8}d|jo[|ii|i|�}y||}Wq�tj
o"}t||||d��q�XqPqWd|jot|||��n|S(Nis%(i(Rt_KEYCREtsubt_interpolation_replaceR0RR(RRRRR�R*tdepthRz((s/sys/lib/python/ConfigParser.pyR�/s



s%\(([^)]*)\)s|.cCs?|id�}|djo|i�Snd|i|�SdS(Nis%%(%s)s(RrR>R'(RRqts((s/sys/lib/python/ConfigParser.pyR�Ds
N(RRRZR>RCR&R�RR�R�R�(((s/sys/lib/python/ConfigParser.pyR�s
  	cBs5eZd�Zeid�iZd�Zd�ZRS(cCs/g}|i|||||d�di|�S(NiR(t_interpolate_sometjoin(RRRRR�tL((s/sys/lib/python/ConfigParser.pyR�Nss
%\(([^)]+)\)scCs�|tjot|||��nx�|o�|id�}|djo|i|�dSn|djo|i|| �||}n|dd!}|djo|id�|d}q&|djo�|i|�}	|	djot||d|��n|i|	id��}
||	i	�}y||
}Wn(t
j
ot||||
��nXd|jo$|i||||||d�q�|i|�q&t||d|f��q&WdS(Nt%iiit(s'bad interpolation variable reference %rs/'%%' must be followed by '%%' or '(', found: %r(
RRRtR!t_interpvar_matchR>RR'RrtendR0RR�(RRtaccumtrestRtmapR�tptctmtvarRW((s/sys/lib/python/ConfigParser.pyR�VsB









	
cCs:t|t�ptd��nti||||�dS(sASet an option.  Extend ConfigParser.set: check for string values.soption values must be stringsN(R4R5t	TypeErrorRR](RRRR*((s/sys/lib/python/ConfigParser.pyR]|s(	RRR�RR�RqR�R�R](((s/sys/lib/python/ConfigParser.pyR	Ls		&(RRt__all__RRRRRRRRRRRRRR
RR	(((s/sys/lib/python/ConfigParser.pys<module>Xs.			
�_

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.