Contrib

class file_config.contrib.xml_parser.XMLParser(tree: lxml.etree.Element)[source]

A custom XML parser which is reflective between xml and dictionaries.

Warning

This parser requires type to be set on every value element. This is the only way the XML can be correctly parsed and understood by the loading config.

So this parser is really effective at being reflective with itself. However, it might require hacking to make it work with xml documents that are not built from a config instance in the first place.

classmethod _build_base(element)[source]

Builds a base value from the given etree element.

Parameters

element (lxml.etree.Element) – The element to build from

Returns

The built value

classmethod _build_base_etree(parent, key, value)[source]

Builds a element from a base dictionary entry.

Parameters
  • parent (lxml.etree.Element) – The parent element to add to

  • key (str) – The key of the entry

  • value – The value of th entry

Returns

The built element

Return type

lxml.etree.Element

classmethod _build_dict(root_element, dict_type=<class 'collections.OrderedDict'>)[source]

Builds a dictionary entry from a dictionary parent element.

Parameters
  • root_element (lxml.etree.Element) – The element to build from

  • dict_type – The dictionary class to build with, defaults to collections.OrderedDict, optional

Returns

The built dictionary

Return type

dict

classmethod _build_dict_etree(parent, dictionary)[source]

Builds a element for a dictionary.

Parameters
  • lxml.etree.Element – The parent element to add to

  • dictionary (dict) – The dictionary to build from

Returns

The built dictionary element

Return type

lxml.etree.Element

classmethod _build_list(list_element, dict_type=<class 'collections.OrderedDict'>)[source]

Builds a list entry from a list parent element.

Parameters
  • list_element (lxml.etree.Element) – The element to build from

  • dict_type (class) – The dictionary class to build with, defaults to collections.OrderedDict, optional

Returns

The built list

Return type

list

classmethod _build_list_etree(parent, items)[source]

Builds a element for a list of items.

Parameters
  • parent (lxml.etree.Element) – The parent element to add to

  • items (list) – The list to build elements for

Returns

The built list element

Return type

lxml.etree.Element

classmethod from_dict(dictionary, root='root')[source]

Create an instance of XMLParser from a given dictionary.

Parameters
  • dictionary (dict) – The dictionary to build from

  • root – The name of the root element to use, defaults to “root”, optional

Returns

The new XMLParser instance

Return type

XMLParser

classmethod from_xml(content, encoding='utf-8')[source]

Create an instance of XMLParser from some xml content.

Parameters
  • content (str) – The xml content to build from

  • encoding (str) – The encoding to use for reading the xml content

Returns

The new XMLParser instance

Return type

XMLParser

to_dict(dict_type=<class 'collections.OrderedDict'>)[source]

Get the dictionary representation of the current parser.

Parameters

dict_type (class) – The dictionary type to build

Returns

The resulting dictionary

Return type

dict

to_xml(pretty=False, xml_declaration=False, encoding='utf-8')[source]

Get the xml string of the current parser.

Parameters
  • pretty (bool) – Pretty format the resulting xml string

  • xml_declaration (bool) – Add xml declaration header to resulting xml content

  • encoding (str) – The encoding to use for the resulting xml content

Returns

The xml string of the current parser

Return type

str

class file_config.contrib.ini_parser.INIParser(defaults=None, dict_type=<class 'collections.OrderedDict'>, 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]

A custom INI (config) parser which obeys the Mozilla files specification.

Important

This parser conforms to what Mozilla says configuration files and their encoded values and types should look like. You can find their specification here

Warning

This parser is very unstable across the extremely wide range of ways that INI files can be represented. Mainly this was just created to be reflective with it’s own results rather than building a new configparser.

So building a config instance that parses things like tox.ini might take a bit of hacking to work.

_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 50
_abc_registry = <_weakrefset.WeakSet object>
classmethod _build_dict(parser_dict, delimiter=':', dict_type=<class 'collections.OrderedDict'>)[source]

Builds a dictionary of dict_type given the parser._sections dict.

Parameters
  • parser_dict (dict) – The parser._sections mapping

  • delimiter (str) – The delimiter for nested dictionaries, defaults to “:”, optional

  • dict_type (class) – The dictionary type to use for building the dict, defaults to collections.OrderedDict, optional

Returns

The resulting dictionary

Return type

dict

classmethod _build_parser(dictionary, parser, section_name, delimiter=':', empty_sections=False)[source]

Populates a parser instance with the content of a dictionary.

Parameters
  • dictionary (dict) – The dictionary to use for populating the parser instance

  • parser (configparser.ConfigParser) – The parser instance

  • section_name (str) – The current section name to add the dictionary keys to

  • delimiter (str) – The nested dictionary delimiter character, defaults to “:”, optional

  • empty_sections (bool) – Flag to allow the representation of empty sections to exist, defaults to False, optional

Returns

The populated parser

Return type

configparser.ConfigParser

classmethod _decode_var(string)[source]

Decodes a given string into the appropriate type in Python.

Parameters

string (str) – The string to decode

Returns

The decoded value

classmethod _encode_var(var)[source]

Encodes a variable to the appropriate string format for ini files.

Parameters

var – The variable to encode

Returns

The ini representation of the variable

Return type

str

classmethod from_dict(dictionary, root_section='root', delimiter=':', empty_sections=False)[source]

Create an instance of INIParser from a given dictionary.

Parameters
  • dictionary (dict) – The dictionary to create an instance from

  • root_section (str) – The root key of the ini content, defaults to “root”, optional

  • delimiter (str) – The delimiter character to use for nested dictionaries, defaults to “:”, optional

  • empty_sections (bool) – Flag to allow representation of empty sections to exist, defaults to False, optional

Returns

The new INIParser instance

Return type

INIParser

classmethod from_ini(content)[source]

Create an instance of INIParser from some ini content string.

Parameters

content (str) – The ini content to create an instance from

Returns

The new INIParser instance

Return type

INIParser

is_digit_regex = re.compile('\\d+')
quoted_string_regex = re.compile('^(\\\'.*\\\')|(\\".*\\")$')
requires_quotes = (' ', '\t', '=')
to_dict(delimiter=':', dict_type=<class 'collections.OrderedDict'>)[source]

Get the dictionary representation of the current parser.

Parameters
  • delimiter (str) – The delimiter used for nested dictionaries, defaults to “:”, optional

  • dict_type (class) – The dictionary type to use for building the dictionary reperesentation, defaults to collections.OrderedDict, optional

Returns

The dictionary representation of the parser instance

Return type

dict

to_ini()[source]

Get the ini string of the current parser.

Returns

The ini string of the current parser

Return type

str