Config reader - tmuxp._internal.config_reader
¶
Warning
Be careful with these! Internal APIs are not covered by version policies. They can break or be removed between minor versions!
If you need an internal API stabilized please file an issue.
Configuration parser for YAML and JSON files.
- class tmuxp._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.
>>> 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 <tmuxp._internal.config_reader.ConfigReader object at ...> :rtype: :sphinx_autodoc_typehints_type:`ConfigReader`
>>> cfg.content {'session_name': 'my session'}
>>> cfg = ConfigReader.load("yaml", 'session_name: my session') >>> cfg <tmuxp._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\:\`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\:\`\~tmuxp.\_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 <tmuxp._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 <tmuxp._internal.config_reader.ConfigReader object at ...>
>>> cfg.content {'session_name': 'my session'}