Config reader - vcspull._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.

class vcspull._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}'
Parameters:

content (RawConfigData)

static _load(fmt, content)[source]

Load raw config data and directly return it.

Return type:

dict[str, t.Any]

Parameters:
  • fmt (FormatLiteral)

  • content (str)

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

Return type:

ConfigReader

Parameters:
  • fmt (FormatLiteral)

  • content (str)

>>> cfg = ConfigReader.load("json", '{ "session_name": "my session" }')
>>> cfg
<tmuxp.config_reader.ConfigReader object at ...>
>>> cfg.content
{'session_name': 'my session'}
>>> cfg = ConfigReader.load("yaml", 'session_name: my session')
>>> cfg
<tmuxp.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:

Return type:

dict[str, Any]

Parameters:

path (Path)

>>> yaml_file = tmp_path / 'my_config.yaml'
>>> 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:

Return type:

ConfigReader

Parameters:

path (Path)

>>> yaml_file = tmp_path / 'my_config.yaml'
>>> yaml_file.write_text('session_name: my session', encoding='utf-8')
24

Read YAML file:

>>> cfg = ConfigReader.from_file(yaml_file)
>>> cfg
<tmuxp.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.config_reader.ConfigReader object at ...>
>>> cfg.content
{'session_name': 'my session'}
static _dump(fmt, content, indent=2, **kwargs)[source]

Dump directly.

Return type:

str

Parameters:
  • fmt (FormatLiteral)

  • content (RawConfigData)

  • indent (int)

  • kwargs (t.Any)

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

Return type:

str

Parameters:
  • fmt (FormatLiteral)

  • indent (int)

  • kwargs (t.Any)

>>> cfg = ConfigReader({ "session_name": "my session" })
>>> cfg.dump("yaml")
'session_name: my session\n'
>>> cfg.dump("json")
'{\n  "session_name": "my session"\n}'
class vcspull._internal.config_reader._DuplicateTrackingSafeLoader(stream)[source]

Bases: SafeLoader

SafeLoader that records duplicate top-level keys.

Initialize the scanner.

Parameters:

stream (str)

yaml_constructors = {'tag:yaml.org,2002:binary': <function SafeConstructor.construct_yaml_binary>, 'tag:yaml.org,2002:bool': <function SafeConstructor.construct_yaml_bool>, 'tag:yaml.org,2002:float': <function SafeConstructor.construct_yaml_float>, 'tag:yaml.org,2002:int': <function SafeConstructor.construct_yaml_int>, 'tag:yaml.org,2002:map': <function _duplicate_tracking_construct_mapping>, 'tag:yaml.org,2002:null': <function SafeConstructor.construct_yaml_null>, 'tag:yaml.org,2002:omap': <function SafeConstructor.construct_yaml_omap>, 'tag:yaml.org,2002:pairs': <function SafeConstructor.construct_yaml_pairs>, 'tag:yaml.org,2002:seq': <function SafeConstructor.construct_yaml_seq>, 'tag:yaml.org,2002:set': <function SafeConstructor.construct_yaml_set>, 'tag:yaml.org,2002:str': <function SafeConstructor.construct_yaml_str>, 'tag:yaml.org,2002:timestamp': <function SafeConstructor.construct_yaml_timestamp>, None: <function SafeConstructor.construct_undefined>}
vcspull._internal.config_reader._duplicate_tracking_construct_mapping(loader, node, deep=False)[source]
Return type:

dict[Any, Any]

Parameters:
class vcspull._internal.config_reader.DuplicateAwareConfigReader(content, *, duplicate_sections=None, top_level_items=None)[source]

Bases: ConfigReader

ConfigReader that tracks duplicate top-level YAML sections.

Parameters:
  • content (RawConfigData)

  • duplicate_sections (dict[str, list[t.Any]] | None)

  • top_level_items (list[tuple[str, t.Any]] | None)

property duplicate_sections: dict[str, list[Any]]

Mapping of top-level keys to the list of duplicated values.

property top_level_items: list[tuple[str, Any]]

Ordered list of top-level items, including duplicates.

classmethod _load_yaml_with_duplicates(content)[source]
Return type:

tuple[dict[str, Any], dict[str, list[Any]], list[tuple[str, Any]]]

Parameters:

content (str)

classmethod _load_from_path(path)[source]
Return type:

tuple[dict[str, Any], dict[str, list[Any]], list[tuple[str, Any]]]

Parameters:

path (Path)

classmethod from_file(path)[source]

Load data from file path.

YAML file

For demonstration only, create a YAML file:

Return type:

DuplicateAwareConfigReader

Parameters:

path (Path)

>>> yaml_file = tmp_path / 'my_config.yaml'
>>> yaml_file.write_text('session_name: my session', encoding='utf-8')
24

Read YAML file:

>>> cfg = ConfigReader.from_file(yaml_file)
>>> cfg
<tmuxp.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.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:

Return type:

dict[str, Any]

Parameters:

path (Path)

>>> yaml_file = tmp_path / 'my_config.yaml'
>>> 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 load_with_duplicates(path)[source]
Return type:

tuple[dict[str, Any], dict[str, list[Any]], list[tuple[str, Any]]]

Parameters:

path (Path)