yaml

Modification of the yaml loader for E3.

Module Contents

Classes

OrderedDictYAMLLoader

A YAML loader that loads mappings into ordered dictionaries.

CaseParser

Parse case statements in an OrderedDict.

Functions

load_ordered(→ collections.OrderedDict)

Load a .yaml file, keep the file order.

load_with_config(→ Any)

Load yaml config files with case statement handling.

load_with_regexp_table(→ dict)

Load a yaml file using regexp tables.

exception yaml.YamlError

Bases: Exception

Common base class for all non-exit exceptions.

class yaml.OrderedDictYAMLLoader(stream: IO[str])

Bases: Loader

A YAML loader that loads mappings into ordered dictionaries.

The loader also support the !include constructor that allows inclusion of yaml files into another yaml

yaml_include(node)
construct_yaml_map(node)
construct_mapping(node, deep=False)
yaml.load_ordered(filename: str) collections.OrderedDict

Load a .yaml file, keep the file order.

class yaml.CaseParser(initial_config: dict, case_prefix: str = 'case_')

Parse case statements in an OrderedDict.

Each time a key starting with case_ (or the prefix you choose) if found, in a block mapping, the value of the sub block matching the key value is added to the result dictionary.

For instance: with an initial config {'param1': 'full', 'param2': 'short'}, the result of the parsing of:

{'case_param1': {
    'full': {
        'case_param2': {
            'full': {'a': 2, 'b': 1, 'c': 'content'},
            'short': {'y': 0, 'c': ''}}},
    'short': {
        'case_param2': {
            'full': {'a': 9, 'b': 5, 'c': 'default'},
            'short': {'y': 3, 'c': ''}}}},
 'value1': 10,
 'value2': '%(c)s',
 'case_param2' : {
        'f.*l': {'value3': 30, 'a': 42}}}

is {'y': 0, 'c': '', 'value2': '', 'value1': 10}

and with {'param1': 'short', 'param2': 'full'} we get:

{'a': 42, 'c': 'default', 'b': 5, 'value3': 30,
 'value2': 'default', 'value1': 10}

Note that values can be redefined and that you can add a python regexp, as supported by the re module, in your case values.

__parse_case(case_key: str, data: dict) Any

Parse a case statement.

Parameters:
  • case_key – the variable on which the case is evaluated

  • data – the dictionary of case conditions

Returns:

the value of the matched element or None

__format_value(value: Any) Any

Format a value.

Parameters:

value – the value to be formatted

Returns:

the result of the expansion

__update_state(key: str, value: Any, cursor: Any, prefix: tuple) None

Update state.

Parameters:
  • key – the key to modify. Leading or trailing ‘+’ in the key name are interpreted respectively as append and prepend operators. For dictionaries these operators are interpreted as an update on the original value.

  • value – the new value

  • cursor – the object to be updated

  • prefix – a tuple of string that gives the position of cursor in self.__state. This is used only for debugging purposes

parse(data: Any) Any

Parse.

Parameters:

data – a python object. Note that dictionaries in that structure should be OrderedDict.

Returns:

a new python object after expansion of case statements and formatting of values

__parse(data: Any, cursor: Any, prefix: tuple) Any

Parse (internal).

Parameters:
  • data – a python object. Note that dictionaries in that structure should be OrderedDict.

  • cursor – a ref to a substructure of self.__state

  • prefix – the current location in self.__state (a tuple of keys)

Returns:

the new cursor object

yaml.load_with_config(filename: str | list[str], config: dict) Any

Load yaml config files with case statement handling.

Parameters:
  • filename – a path or list of path. When a list of path is given, config files are loaded in order each one updating the result of the previous parsing.

  • config – initial state

Returns:

the final object

yaml.load_with_regexp_table(filename: str, selectors: list[str], data: dict) dict

Load a yaml file using regexp tables.

Parameters:
  • filename – the yaml file to load

  • selectors – a list of string that will be used to match the regexps in table

  • data – a dictionary used to replace part of value content

This function expect a yaml file that has the following format:

key1:
    [['regexp1_1', ..., 'regexp1_n', value],
     ['regexp2_1', ..., 'regexp2_n', value],...

key2:
    ...

The returned dictionary will have key1, key2, … as keys. For each key the value is the last element of the first sublist for which the n first first regexp do match, strings in selectors list. So in the yaml file each sublist associated with each key should have exactly len(selectors) + 1 elements.