pyscaffold.contrib package¶
Submodules¶
pyscaffold.contrib.configupdater module¶
Configuration file updater.
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.
The basic idea of ConfigUpdater is that a configuration file consists of three kinds of building blocks: sections, comments and spaces for separation. A section itself consists of three kinds of blocks: options, comments and spaces. This gives us the corresponding data structures to describe a configuration file.
A general block object contains the lines which were parsed and make up the block. If a block object was not changed then during writing the same lines that were parsed will be used to express the block. In case a block, e.g. an option, was changed, it is marked as updated and its values will be transformed into a corresponding string during an update of a configuration file.
Note
ConfigUpdater was created by starting from Python’s ConfigParser source code and changing it according to my needs. Thus this source code is subject to the PSF License in a way but I am not a lawyer.
-
class
pyscaffold.contrib.configupdater.
ConfigUpdater
(allow_no_value=False, *, delimiters='=', ':', comment_prefixes='#', ';', inline_comment_prefixes=None, strict=True, space_around_delimiters=True)[source]¶ Bases:
pyscaffold.contrib.configupdater.Container
,collections.abc.MutableMapping
Parser for updating configuration files.
- ConfigUpdater follows the API of ConfigParser with some differences:
inline comments are treated as part of a key’s value,
only a single config file can be updated at a time,
empty lines in values are not valid,
the original case of sections and keys are kept,
control over the position of a new section/key.
Following features are deliberately not implemented:
interpolation of values,
propagation of parameters from the default section,
conversions of values,
passing key/value-pairs with
default
argument,non-strict mode allowing duplicate sections and keys.
-
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)[source]¶ Create a new section in the configuration.
Raise DuplicateSectionError if a section by the specified name already exists. Raise ValueError if name is DEFAULT.
- Parameters
section (str or
Section
) – name or Section type
-
get
(section, option)[source]¶ Gets an option value for a given section.
- Parameters
section (str) – section name
option (str) – option name
- Returns
Option object holding key/value pair
- Return type
Option
-
has_option
(section, option)[source]¶ Checks for the existence of a given option in a given section.
- Parameters
section (str) – name of section
option (str) – name of option
- Returns
whether the option exists in the given section
- Return type
bool
-
has_section
(section)[source]¶ Returns whether the given section exists.
- Parameters
section (str) – name of section
- Returns
wether the section exists
- Return type
bool
-
items
(section=<object object>)[source]¶ Return a list of (name, value) tuples for options or sections.
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_type) for each section.
- Parameters
section (str) – optional section name, default UNSET
- Returns
list of
Section
orOption
objects- Return type
list
-
options
(section)[source]¶ Returns list of configuration options for the named section.
- Parameters
section (str) – name of section
- Returns
list of option names
- Return type
list
-
optionxform
(optionstr)[source]¶ Converts an option key to lower case for unification
- Parameters
optionstr (str) – key name
- Returns
unified option name
- Return type
str
-
read
(filename, encoding=None)[source]¶ Read and parse a filename.
- Parameters
filename (str) – path to file
encoding (str) – encoding of file, default None
-
read_file
(f, source=None)[source]¶ 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 thesource
specifying the name of the file being read. If not given, it is taken from f.name. Iff
has noname
attribute,<???>
is used.- Parameters
f – file like object
source (str) – reference name for file object, default None
-
read_string
(string, source='<string>')[source]¶ Read configuration from a given string.
- Parameters
string (str) – string containing a configuration
source (str) – reference name for file object, default ‘<string>’
-
remove_option
(section, option)[source]¶ Remove an option.
- Parameters
section (str) – section name
option (str) – option name
- Returns
whether the option was actually removed
- Return type
bool
-
remove_section
(name)[source]¶ Remove a file section.
- Parameters
name – name of the section
- Returns
whether the section was actually removed
- Return type
bool
-
sections_blocks
()[source]¶ Returns all section blocks
- Returns
list of
Section
blocks- Return type
list
-
set
(section, option, value=None)[source]¶ Set an option.
- Parameters
section (str) – section name
option (str) – option name
value (str) – value, default None
-
exception
pyscaffold.contrib.configupdater.
DuplicateOptionError
(section, option, source=None, lineno=None)[source]¶ Bases:
configparser.Error
Raised 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.
-
exception
pyscaffold.contrib.configupdater.
DuplicateSectionError
(section, source=None, lineno=None)[source]¶ Bases:
configparser.Error
Raised 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.
-
exception
pyscaffold.contrib.configupdater.
MissingSectionHeaderError
(filename, lineno, line)[source]¶ Bases:
configparser.ParsingError
Raised when a key-value pair is found before any section header.
-
exception
pyscaffold.contrib.configupdater.
NoConfigFileReadError
[source]¶ Bases:
configparser.Error
Raised when no configuration file was read but update requested.
-
exception
pyscaffold.contrib.configupdater.
NoOptionError
(option, section)[source]¶ Bases:
configparser.Error
A requested option was not found.
Module contents¶
Contribution packages used by PyScaffold
All packages inside contrib
are external packages that come with their
own licences and are not part of the PyScaffold source code itself.
The reason for shipping these dependencies directly is to avoid problems in
the resolution of setup_requires
dependencies that occurred more often
than not, see issues #71 and #72.
Currently the contrib packages are:
setuptools_scm v3.3.3
pytest-runner 5.1
configupdater 1.0
The packages/modules were just copied over.