microprobe.utils.config.DuplicateConfigParser
- class DuplicateConfigParser(defaults=None, dict_type=<class 'dict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)[source]
Bases:
ConfigParser
,object
A helper class to allow multiple configuration files.
This class extends the base class behavior by allowing to read multiple config files instead of a single one. Configuration options are appended or reset depending on the configuration option type. Scalar values are reset, i.e. last configuration file setting the value is used. List values are appended.
- __init__(defaults=None, dict_type=<class 'dict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)
Methods
__init__
([defaults, dict_type, ...])add_section
(section)Create a new section in the configuration.
clear
()defaults
()get
(section, option[, raw])Get an option value for a given section.
getboolean
(section, option, *[, raw, vars, ...])getfloat
(section, option, *[, raw, vars, ...])getint
(section, option, *[, raw, vars, fallback])has_option
(section, option)Check for the existence of a given option in a given section. If the specified `section' is None or an empty string, DEFAULT is assumed. If the specified `section' does not exist, returns False.
has_section
(section)Indicate whether the named section is present in the configuration.
items
(section[, raw])Return a list of tuples with (name, value) for each option in the section.
keys
()options
(section)Return a list of option names for the given section name.
optionxform
(optionstr)pop
(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem
()Remove a section from the parser and return it as a (section_name, section_proxy) tuple.
read
(filenames[, overwrite_first])Read and parse a filename or a list of filenames.
read_dict
(dictionary[, source])Read configuration from a dictionary.
read_file
(f[, source])Like read() but the argument must be a file-like object.
read_string
(string[, source])Read configuration from a given string.
readfp
(fp[, filename, overwrite_first])Like read() but the argument must be a file-like object.
remove_option
(section, option)Remove an option.
remove_section
(section)Remove a file section.
sections
()Return a list of section names, excluding [DEFAULT]
set
(section, option[, value])Set an option.
setdefault
(k[,d])update
([E, ]**F)If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values
()write
(fp[, write_empty])Write an .ini-format representation of the configuration state.
Attributes
- read(filenames, overwrite_first=False)[source]
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, system wide 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.
- get(section, option, raw=False)[source]
Get an option value for a given section.
If vars is provided, it must be a dictionary. The option is looked up in vars (if provided), section, and in defaults in that order.
All % interpolations are expanded in the return values, unless the optional argument raw is true. Values for interpolation keys are looked up in the same manner as the option.
The section DEFAULT is special.
- items(section, raw=False)[source]
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.
- readfp(fp, filename=None, overwrite_first=False)[source]
Like 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.
- write(fp, write_empty=False)[source]
Write an .ini-format representation of the configuration state.
- BOOLEAN_STATES = {'0': False, '1': True, 'false': False, 'no': False, 'off': False, 'on': True, 'true': True, 'yes': True}
- NONSPACECRE = re.compile('\\S')
- OPTCRE = re.compile('\n (?P<option>.*?) # very permissive!\n \\s*(?P<vi>=|:)\\s* # any number of space/tab,\n # followed by any of t, re.VERBOSE)
- OPTCRE_NV = re.compile('\n (?P<option>.*?) # very permissive!\n \\s*(?: # any number of space/tab,\n (?P<vi>=|:)\\s* # optionally followed , re.VERBOSE)
- SECTCRE = re.compile('\n \\[ # [\n (?P<header>[^]]+) # very permissive!\n \\] # ]\n ', re.VERBOSE)
- add_section(section)
Create a new section in the configuration. Extends RawConfigParser.add_section by validating if the section name is a string.
- clear() None. Remove all items from D.
- property converters
- defaults()
- getboolean(section, option, *, raw=False, vars=None, fallback=<object object>, **kwargs)
- getfloat(section, option, *, raw=False, vars=None, fallback=<object object>, **kwargs)
- getint(section, option, *, raw=False, vars=None, fallback=<object object>, **kwargs)
- has_option(section, option)
Check for the existence of a given option in a given section. If the specified `section’ is None or an empty string, DEFAULT is assumed. If the specified `section’ does not exist, returns False.
- has_section(section)
Indicate whether the named section is present in the configuration.
The DEFAULT section is not acknowledged.
- keys() a set-like object providing a view on D's keys
- options(section)
Return a list of option names for the given section name.
- optionxform(optionstr)
- 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.
- popitem()
Remove a section from the parser and return it as a (section_name, section_proxy) tuple. If no section is present, raise KeyError.
The section DEFAULT is never returned because it cannot be removed.
- read_dict(dictionary, source='<dict>')
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.
All types held in the dictionary are converted to strings during reading, including section names, option names and keys.
Optional second argument is the `source’ specifying the name of the dictionary being read.
- read_file(f, source=None)
Like read() but the argument must be a file-like object.
The `f’ argument must be iterable, returning one line at a time. Optional second argument is the `source’ specifying the name of the file being read. If not given, it is taken from f.name. If `f’ has no `name’ attribute, `<???>’ is used.
- read_string(string, source='<string>')
Read configuration from a given string.
- remove_option(section, option)
Remove an option.
- remove_section(section)
Remove a file section.
- sections()
Return a list of section names, excluding [DEFAULT]
- set(section, option, value=None)
Set an option. Extends RawConfigParser.set by validating type and interpolation syntax on the value.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
- update([E, ]**F) None. Update D from mapping/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values