octodns.yaml

Functions

safe_dump(data, fh[, order_mode])

safe_load(stream[, enforce_order, order_mode])

Classes

ContextLoader(stream)

NaturalSortEnforcingLoader(stream)

NaturalSortingDumper(stream[, ...])

SimpleSortEnforcingLoader(stream)

SimpleSortingDumper(stream[, default_style, ...])

SortEnforcingLoader(stream)

SortingDumper(stream[, default_style, ...])

This sorts keys alphanumerically in a "natural" manner where things with the number 2 come before the number 12.

Exceptions

InvalidOrder(order_mode)

class octodns.yaml.ContextLoader(stream)[source]

Bases: SafeLoader

construct_include(node)[source]
flatten_include(node)[source]
construct_mapping(node, deep=False)[source]

Calls our parent and wraps the resulting dict with a ContextDict

classmethod add_flattener(tag, flattener)[source]
flatten_mapping(node)[source]
flatten_yaml_seq(node)[source]
flatten_yaml_map(node)[source]
yaml_constructors = {'!include': <function ContextLoader.construct_include>, '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 ContextLoader.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>}
yaml_flatteners = {'!include': <function ContextLoader.flatten_include>, 'tag:yaml.org,2002:map': <function ContextLoader.flatten_yaml_map>, 'tag:yaml.org,2002:seq': <function ContextLoader.flatten_yaml_seq>}
class octodns.yaml.SortEnforcingLoader(stream)[source]

Bases: ContextLoader

construct_mapping(node, deep=False)[source]

Calls our parent and wraps the resulting dict with a ContextDict

class octodns.yaml.NaturalSortEnforcingLoader(stream)[source]

Bases: SortEnforcingLoader

static KEYGEN(val: ~natsort.utils.SupportsDunderLT | ~natsort.utils.SupportsDunderGT | None | ~typing.Any, *, key: ~typing.Callable[[~typing.Any], ~natsort.utils.SupportsDunderLT | ~natsort.utils.SupportsDunderGT | None] | None = None, string_func: ~typing.Callable[[str | ~pathlib.PurePath], ~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...]] | ~typing.Callable[[str | ~pathlib.PurePath], ~typing.Tuple[~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...], ...]] = <function parse_string_factory.<locals>.func>, bytes_func: ~typing.Callable[[bytes], ~typing.Tuple[bytes] | ~typing.Tuple[~typing.Tuple[bytes]]] = <function parse_bytes_factory.<locals>.<lambda>>, num_func: ~typing.Callable[[~typing.Any], ~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...]] = <function parse_number_or_none_factory.<locals>.func>) Tuple[SupportsDunderLT | SupportsDunderGT, ...]

Key to sort strings and numbers naturally.

It works by splitting the string into components of strings and numbers, and then converting the numbers into actual ints or floats.

Parameters:
  • val (str | bytes | int | float | iterable)

  • key (callable | None) – A key to apply to the val before any other operations are performed.

  • string_func (callable) – If val (or the output of key if given) is of type str, this function will be applied to it. The function must return a tuple.

  • bytes_func (callable) – If val (or the output of key if given) is of type bytes, this function will be applied to it. The function must return a tuple.

  • num_func (callable) – If val (or the output of key if given) is not of type bytes, str, nor is iterable, this function will be applied to it. The function must return a tuple.

Returns:

out – The string split into its string and numeric components. It always starts with a string, and then alternates between numbers and strings (unless it was applied recursively, in which case it will return tuples of tuples, but the lowest-level tuples will then always start with a string etc.).

Return type:

tuple

See also

parse_string_factory, parse_bytes_factory, parse_number_or_none_factory

yaml_constructors = {'!include': <function ContextLoader.construct_include>, '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 SortEnforcingLoader.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>}
class octodns.yaml.SimpleSortEnforcingLoader(stream)[source]

Bases: SortEnforcingLoader

KEYGEN(s)
yaml_constructors = {'!include': <function ContextLoader.construct_include>, '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 SortEnforcingLoader.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>}
exception octodns.yaml.InvalidOrder(order_mode)[source]

Bases: Exception

__init__(order_mode)[source]
octodns.yaml.safe_load(stream, enforce_order=True, order_mode='natural')[source]
class octodns.yaml.SortingDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]

Bases: SafeDumper

This sorts keys alphanumerically in a “natural” manner where things with the number 2 come before the number 12.

See https://www.xormedia.com/natural-sort-order-with-zero-padding/ for more info

_representer(data)[source]
yaml_multi_representers = {<class 'dict'>: <function SortingDumper._representer>, <class 'str'>: <function SafeRepresenter.represent_str>}
yaml_representers = {<class 'NoneType'>: <function SafeRepresenter.represent_none>, <class 'bool'>: <function SafeRepresenter.represent_bool>, <class 'bytes'>: <function SafeRepresenter.represent_binary>, <class 'datetime.date'>: <function SafeRepresenter.represent_date>, <class 'datetime.datetime'>: <function SafeRepresenter.represent_datetime>, <class 'dict'>: <function SortingDumper._representer>, <class 'float'>: <function SafeRepresenter.represent_float>, <class 'int'>: <function SafeRepresenter.represent_int>, <class 'list'>: <function SafeRepresenter.represent_list>, <class 'set'>: <function SafeRepresenter.represent_set>, <class 'str'>: <function SafeRepresenter.represent_str>, <class 'tuple'>: <function SafeRepresenter.represent_list>, None: <function SafeRepresenter.represent_undefined>}
class octodns.yaml.NaturalSortingDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]

Bases: SortingDumper

static KEYGEN(val: ~natsort.utils.SupportsDunderLT | ~natsort.utils.SupportsDunderGT | None | ~typing.Any, *, key: ~typing.Callable[[~typing.Any], ~natsort.utils.SupportsDunderLT | ~natsort.utils.SupportsDunderGT | None] | None = None, string_func: ~typing.Callable[[str | ~pathlib.PurePath], ~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...]] | ~typing.Callable[[str | ~pathlib.PurePath], ~typing.Tuple[~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...], ...]] = <function parse_string_factory.<locals>.func>, bytes_func: ~typing.Callable[[bytes], ~typing.Tuple[bytes] | ~typing.Tuple[~typing.Tuple[bytes]]] = <function parse_bytes_factory.<locals>.<lambda>>, num_func: ~typing.Callable[[~typing.Any], ~typing.Tuple[~typing.Any, ...] | ~typing.Tuple[~typing.Tuple[~typing.Any, ...], ...]] = <function parse_number_or_none_factory.<locals>.func>) Tuple[SupportsDunderLT | SupportsDunderGT, ...]

Key to sort strings and numbers naturally.

It works by splitting the string into components of strings and numbers, and then converting the numbers into actual ints or floats.

Parameters:
  • val (str | bytes | int | float | iterable)

  • key (callable | None) – A key to apply to the val before any other operations are performed.

  • string_func (callable) – If val (or the output of key if given) is of type str, this function will be applied to it. The function must return a tuple.

  • bytes_func (callable) – If val (or the output of key if given) is of type bytes, this function will be applied to it. The function must return a tuple.

  • num_func (callable) – If val (or the output of key if given) is not of type bytes, str, nor is iterable, this function will be applied to it. The function must return a tuple.

Returns:

out – The string split into its string and numeric components. It always starts with a string, and then alternates between numbers and strings (unless it was applied recursively, in which case it will return tuples of tuples, but the lowest-level tuples will then always start with a string etc.).

Return type:

tuple

See also

parse_string_factory, parse_bytes_factory, parse_number_or_none_factory

class octodns.yaml.SimpleSortingDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]

Bases: SortingDumper

KEYGEN(s)
octodns.yaml.safe_dump(data, fh, order_mode='natural', **options)[source]