pyscaffold.contrib package


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.


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.

exception pyscaffold.contrib.configupdater.NoSectionError(section)[source]

Bases: configparser.Error

Raised when no section matches a requested option.

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.NoOptionError(option, section)[source]

Bases: configparser.Error

A requested option was not found.

exception pyscaffold.contrib.configupdater.NoConfigFileReadError[source]

Bases: configparser.Error

Raised when no configuration file was read but update requested.

exception pyscaffold.contrib.configupdater.ParsingError(source=None, filename=None)[source]

Bases: configparser.Error

Raised when a configuration file does not follow legal syntax.

append(lineno, line)[source]

Deprecated, use `source’.

exception pyscaffold.contrib.configupdater.MissingSectionHeaderError(filename, lineno, line)[source]

Bases: configparser.ParsingError

Raised when a key-value pair is found before any section header.

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,

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)

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.

  • section (str) – section name
  • option (str) – option name

Option object holding key/value pair

Return type:


has_option(section, option)[source]

Checks for the existence of a given option in a given section.

  • section (str) – name of section
  • option (str) – name of option

whether the option exists in the given section

Return type:



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 or Option objects
Return type:list

Returns list of configuration options for the named section.

Parameters:section (str) – name of section
Returns:list of option names
Return type:list

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.

  • 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 the source specifying the name of the file being read. If not given, it is taken from If f has no name attribute, <???> is used.

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

  • string (str) – string containing a configuration
  • source (str) – reference name for file object, default ‘<string>’
remove_option(section, option)[source]

Remove an option.

  • section (str) – section name
  • option (str) – option name

whether the option was actually removed

Return type:



Remove a file section.

Parameters:name – name of the section
Returns:whether the section was actually removed
Return type:bool

Return a list of section names

Returns:list of section names
Return type:list

Returns all section blocks

Returns:list of Section blocks
Return type:list
set(section, option, value=None)[source]

Set an option.

  • section (str) – section name
  • option (str) – option name
  • value (str) – value, default None

Transform to dictionary

Returns:dictionary with same content
Return type:dict

Update the read-in configuration file.


Call ConfigParser to validate config

Parameters:kwargs – are passed to configparser.ConfigParser

Write an .ini-format representation of the configuration state.

Parameters:fp (file-like object) – open file handle

pyscaffold.contrib.ptr module


class pyscaffold.contrib.ptr.CustomizedDist(attrs=None)[source]

Bases: setuptools.dist.Distribution

allow_hosts = None

Specialized version of Distribution.fetch_build_egg that respects respects allow_hosts and index_url.

index_url = None
class pyscaffold.contrib.ptr.PyTest(dist, **kw)[source]

Bases: setuptools.command.test.test

>>> import setuptools
>>> dist = setuptools.Distribution()
>>> cmd = PyTest(dist)
static ensure_setuptools_version()[source]

Due to the fact that pytest-runner is often required (via setup-requires directive) by toolchains that never invoke it (i.e. they’re only installing the package, not testing it), instead of declaring the dependency in the package metadata, assert the requirement at run time.


Set final values for all the options that this command supports. This is always called as late as possible, ie. after any option assignments from the command-line or from other commands have been done. Thus, this is the place to code option dependencies: if ‘foo’ depends on ‘bar’, then it is safe to set ‘foo’ from ‘bar’ as long as ‘foo’ still has the same value it was assigned in ‘initialize_options()’.

This method must be implemented by all command classes.


Set default values for all the options that this command supports. Note that these defaults may be overridden by other commands, by the setup script, by config files, or by the command-line. Thus, this is not the place to code dependencies between options; generally, ‘initialize_options()’ implementations are just a bunch of “ = None” assignments.

This method must be implemented by all command classes.


Extend install_dists to include extras support


Install extras that are indicated by markers or install all extras if ‘–extras’ is indicated.

static marker_passes(marker)[source]

Given an environment marker, return True if the marker is valid and matches this environment.


Override run to ensure requirements are available in this session (but don’t install them anywhere).


Invoke pytest, replacing argv. Return result code.

user_options = [('extras', None, 'Install (all) setuptools extras when running tests'), ('index-url=', None, 'Specify an index url from which to retrieve dependencies'), ('allow-hosts=', None, 'Whitelist of comma-separated hosts to allow when retrieving dependencies'), ('addopts=', None, 'Additional options to be passed verbatim to the pytest runner')]

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:

  1. setuptools_scm v3.1
  2. pytest-runner 4.2
  3. configupdater 0.3

The packages/modules were just copied over.

pyscaffold.contrib.scm_find_files(*args, **kwargs)[source]
pyscaffold.contrib.scm_get_local_dirty_tag(*args, **kwargs)[source]
pyscaffold.contrib.scm_get_local_node_and_date(*args, **kwargs)[source]
pyscaffold.contrib.scm_guess_next_dev_version(*args, **kwargs)[source]
pyscaffold.contrib.scm_parse_archival(*args, **kwargs)[source]
pyscaffold.contrib.scm_parse_git(*args, **kwargs)[source]
pyscaffold.contrib.scm_parse_hg(*args, **kwargs)[source]
pyscaffold.contrib.scm_parse_pkginfo(*args, **kwargs)[source]
pyscaffold.contrib.scm_postrelease_version(*args, **kwargs)[source]
pyscaffold.contrib.write_pbr_json(*args, **kwargs)[source]