Config reader - cihai._internal.config_reader

exception cihai._internal.config_reader.ConfigfmtNotImplementedError(fmt)[source]

Bases: NotImplementedError

exception cihai._internal.config_reader.ConfigExtensionNotImplementedError(ext, path)[source]

Bases: NotImplementedError

class cihai._internal.config_reader.ConfigReader(content)[source]

Bases: object

Parse string data (YAML and JSON) into a dictionary.

>>> cfg = ConfigReader({ "session_name": "my session" })
>>> cfg.dump("yaml")
'session_name: my session\n'
>>> cfg.dump("json")
'{\n  "session_name": "my session"\n}'
static _load(fmt, content)[source]

Load raw config data and directly return it.

Return type:

Dict[str, Any]

>>> ConfigReader._load("json", '{ "session_name": "my session" }')
{'session_name': 'my session'}
>>> ConfigReader._load("yaml", 'session_name: my session')
{'session_name': 'my session'}
classmethod load(fmt, content)[source]

Load raw config data into a ConfigReader instance (to dump later).

>>> cfg = ConfigReader.load("json", '{ "session_name": "my session" }')
>>> cfg
<cihai._internal.config_reader.ConfigReader object at ...>
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~cihai.\_internal.config\_reader.ConfigReader\``
>>> cfg.content
{'session_name': 'my session'}
>>> cfg = ConfigReader.load("yaml", 'session_name: my session')
>>> cfg
<cihai._internal.config_reader.ConfigReader object at ...>
>>> cfg.content
{'session_name': 'my session'}
classmethod _from_file(path)[source]

Load data from file path directly to dictionary.

YAML file

For demonstration only, create a YAML file:

>>> yaml_file = tmp_path / 'my_config.yaml'
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~typing.Dict\`\\ \\\[\:py\:class\:\`str\`\, \:py\:data\:\`\~typing.Any\`\]`
>>> yaml_file.write_text('session_name: my session', encoding='utf-8')
24

Read YAML file:

>>> ConfigReader._from_file(yaml_file)
{'session_name': 'my session'}

JSON file

For demonstration only, create a JSON file:

>>> json_file = tmp_path / 'my_config.json'
>>> json_file.write_text('{"session_name": "my session"}', encoding='utf-8')
30

Read JSON file:

>>> ConfigReader._from_file(json_file)
{'session_name': 'my session'}
classmethod from_file(path)[source]

Load data from file path.

YAML file

For demonstration only, create a YAML file:

>>> yaml_file = tmp_path / 'my_config.yaml'
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`\~cihai.\_internal.config\_reader.ConfigReader\``
>>> yaml_file.write_text('session_name: my session', encoding='utf-8')
24

Read YAML file:

>>> cfg = ConfigReader.from_file(yaml_file)
>>> cfg
<cihai._internal.config_reader.ConfigReader object at ...>
>>> cfg.content
{'session_name': 'my session'}

JSON file

For demonstration only, create a JSON file:

>>> json_file = tmp_path / 'my_config.json'
>>> json_file.write_text('{"session_name": "my session"}', encoding='utf-8')
30

Read JSON file:

>>> cfg = ConfigReader.from_file(json_file)
>>> cfg
<cihai._internal.config_reader.ConfigReader object at ...>
>>> cfg.content
{'session_name': 'my session'}
static _dump(fmt, content, indent=2, **kwargs)[source]

Dump directly.

Return type:

str

>>> ConfigReader._dump("yaml", { "session_name": "my session" })
'session_name: my session\n'
>>> ConfigReader._dump("json", { "session_name": "my session" })
'{\n  "session_name": "my session"\n}'
dump(fmt, indent=2, **kwargs)[source]

Dump via ConfigReader instance.

>>> cfg = ConfigReader({ "session_name": "my session" })
>>> cfg.dump("yaml")
'session_name: my session\n'
:rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`str\``
>>> cfg.dump("json")
'{\n  "session_name": "my session"\n}'