Handlers

class file_config.handlers.INIHandler[source]

The INI serialization handler.

Important

INI files cannot support arrays of mappings. Using these in your config and trying to dump to ini will throw a ValueError and will fail serialization.

If you need to use a config with arrays of mappings use toml instead. This use case is one of the many things toml does better than traditional config files.

name = 'ini'
on_configparser_dumps(configparser, config, dictionary, **kwargs)[source]

The configparser dumps method.

Parameters
  • configparser (module) – The configparser module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serialize

  • root (str) – The top-level section of the ini file, defaults to config.__name__, optional

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

Returns

The ini serialization of the given dictionary

Return type

str

on_configparser_loads(configparser, config, content, **kwargs)[source]

The configparser loads method.

Parameters
  • configparser (module) – The configparser module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {'delimiter': ':', 'empty_sections': False, 'root': None}
packages = ('configparser',)
class file_config.handlers.XMLHandler[source]

The XML serialization handler.

name = 'xml'
on_lxml_dumps(lxml, config, dictionary, **kwargs)[source]

The lxml dumps method.

Parameters
  • lxml (module) – The lxml module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary to serialize

  • root (str) – The root tag of the xml document, defaults to the config instance’s name, optional

  • pretty (bool) – Pretty format the resulting xml document, defaults to False, optional

  • xml_declaration (bool) – Add the xml declaration header to the resulting xml document, defaults to False, optional

  • encoding (str) – The encoding to use for the resulting xml document, defaults to “utf-8”, optional

Returns

The XML serialization

Return type

str

on_lxml_loads(lxml, config, content, **kwargs)[source]

The lxml loads method.

Parameters
  • lxml (module) – The lxml module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

  • encoding (str) – The encoding to read the given xml document as, defaults to “utf-8”, optional

Returns

The deserialized dictionary

Return type

dict

options = {'encoding': 'utf-8', 'pretty': False, 'root': None, 'xml_declaration': False}
packages = ('lxml',)
class file_config.handlers.JSONHandler[source]

The JSON serialization handler.

name = 'json'
on_json_dumps(json, config, dictionary, **kwargs)[source]

The json dumps method.

Parameters
  • json (module) – The json module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serialize

  • indent (int) – The amount of spaces to use for indentation, defaults to 0, optional

Returns

The json serialization of the given dictionary

Return type

str

on_json_loads(json, config, content, **kwargs)[source]

The json loads method.

Parameters
  • json (module) – The json module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

on_rapidjson_dumps(rapidjson, config, dictionary, **kwargs)[source]

The rapidjson dumps method.

Parameters
  • rapidjson (module) – The rapidjson module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serialize

  • indent (int) – The amount of spaces to use for indentation, defaults to 0, optional

Returns

The json serialization of the given dictionary

Return type

str

on_rapidjson_loads(rapidjson, config, content, **kwargs)[source]

The rapidjson loads method.

Parameters
  • rapidjson (module) – The rapidjson module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

on_ujson_dumps(ujson, config, dictionary, **kwargs)[source]

The ujson dumps method.

Parameters
  • ujson (module) – The ujson module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serialize

  • indent (int) – The amount of spaces to use for indentation, defaults to 0, optional

Returns

The json serialization of the given dictionary

Return type

str

on_ujson_loads(ujson, config, content, **kwargs)[source]

The ujson loads method.

Parameters
  • ujson (module) – The ujson module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {'indent': None, 'sort_keys': False}
packages = ('rapidjson', 'json', 'ujson')
class file_config.handlers.TOMLHandler[source]

The TOML serialization handler.

name = 'toml'
on_pytoml_dumps(pytoml, config, dictionary, **kwargs)[source]

The pytoml dumps method.

Parameters
  • pytoml (module) – The pytoml module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary to serialize

Returns

The TOML serialization

Return type

str

on_pytoml_loads(pytoml, config, content, **kwargs)[source]

The pytoml loads method.

Parameters
  • pytoml (module) – The pytoml module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

on_toml_dumps(toml, config, dictionary, **kwargs)[source]

The toml dumps method.

Parameters
  • toml (module) – The toml module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary to serialize

  • inline_tables (list) – A list glob patterns to use for derminining which dictionaries should be rendered as inline tables, defaults to [], optional

Returns

The TOML serialization

Return type

str

Dumping inline tables uses fnmatch to compare . delimited dictionary path glob patterns to filter tables

>>> config.dumps_toml(prefer="toml")
name = "My Project"
type = "personal-project"
keywords = [ "example", "test",]
[dependencies.a-dependency]
name = "A Dependency"
version = "v12"
>>> config.dumps_toml(prefer="toml", inline_tables=["dependencies"])
name = "My Project"
type = "personal-project"
keywords = [ "example", "test",]
dependencies = {a-dependency = {name = "A Dependency",version = "v12"}
}
>>> config.dumps_toml(prefer="toml", inline_tables=["dependencies.*"])
name = "My Project"
type = "personal-project"
keywords = [ "example", "test",]
[dependencies]
a-dependency = { name = "A Dependency", version = "v12" }
on_toml_loads(toml, config, content, **kwargs)[source]

The toml loads method.

Parameters
  • toml (module) – The toml module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

on_tomlkit_dumps(tomlkit, config, dictionary, **kwargs)[source]

The tomlkit dumps method.

Parameters
  • tomlkit (module) – The tomlkit module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary to serialize

  • inline_tables (list) – A list glob patterns to use for derminining which dictionaries should be rendered as inline tables, defaults to [], optional

Returns

The TOML serialization

Return type

str

Dumping inline tables uses fnmatch to compare . delimited dictionary path glob patterns to filter tables

>>> config.dumps_toml(prefer="tomlkit")
name = "My Project"
type = "personal-project"
keywords = ["example", "test"]
[dependencies]
[dependencies.a-dependency]
name = "A Dependency"
version = "v12"
>>> config.dumps_toml(prefer="tomlkit", inline_tables=["dependencies"])
name = "My Project"
type = "personal-project"
keywords = ["example", "test"]
dependencies = {a-dependency = {name = "A Dependency",version = "v12"}}
>>> config.dumps_toml(prefer="tomlkit", inline_tables=["dependencies.*"])
name = "My Project"
type = "personal-project"
keywords = ["example", "test"]
[dependencies]
a-dependency = {name = "A Dependency",version = "v12"}
on_tomlkit_loads(tomlkit, config, content, **kwargs)[source]

The tomlkit loads method.

Parameters
  • tomlkit (module) – The tomlkit module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {'inline_tables': []}
packages = ('tomlkit', 'toml', 'pytoml')
class file_config.handlers.YAMLHandler[source]

The YAML serialization handler.

name = 'yaml'
on_yaml_dumps(yaml, config, dictionary, **kwargs)[source]

The pyyaml dumps method.

Parameters
  • yaml (module) – The yaml module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary to seralize

Returns

The serialized content

Return type

str

on_yaml_imported(yaml)[source]

The pyyaml import hook.

Parameters

yaml (module) – The yaml module

on_yaml_loads(yaml, config, content, **kwargs)[source]

The pyyaml loads method.

Parameters
  • yaml (module) – The yaml module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {}
packages = ('yaml',)
class file_config.handlers.PickleHandler[source]

The Pickle serialization handler.

name = 'pickle'
on_pickle_dumps(pickle, config, dictionary, **kwargs)[source]

The pickle dumps method.

Parameters
  • pickle (module) – The pickle module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serailize

Returns

The serialized content

Return type

str

on_pickle_loads(pickle, config, content, **kwargs)[source]

The pickle loads method.

Parameters
  • pickle (module) – The pickle module

  • config (class) – The loading config class

  • content (str) – The content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {}
packages = ('pickle',)
class file_config.handlers.MsgpackHandler[source]

The Message Pack serialization handler.

name = 'msgpack'
on_msgpack_dumps(msgpack, config, dictionary, **kwargs)[source]

The msgpack dumps method.

Parameters
  • msgpack (module) – The msgpack module

  • config (class) – The instance’s config class

  • dictionary (dict) – The dictionary instance to serialize

Returns

The serialized content

Return type

str

on_msgpack_loads(msgpack, config, content, **kwargs)[source]

The msgpack loads method.

Parameters
  • msgpack (module) – The msgpack module

  • config (class) – The loading config class

  • content (dict) – The serialized content to deserialize

Returns

The deserialized dictionary

Return type

dict

options = {}
packages = ('msgpack',)