snowmobile.core.utils.parsing

Contains parsing utilities used by:

Module Contents

Functions

rmerge_dicts(d1: Dict, d2: Dict) → Dict

Recursively merges dictionaries d1 & d2.

up(nm: str)

Utility to truncate upper-casing strings as opposed to str.upper().

strip(val: str, trailing: bool = True, blanks: bool = True, whitespace: bool = False) → str

Utility to strip a variety whitespace from a string.

dict_flatten(attrs: dict, delim: Optional[str] = None, indent_char: Optional[str] = None, bullet_char: Optional[str] = None) → List[Tuple[str, str, str]]

Recursively flattens dictionary to its atomic elements.

p(nm: str) → Tuple[(str, str)]

Utility to parse cfg from dot-prefixed object if included.

snowmobile.core.utils.parsing.rmerge_dicts(d1: Dict, d2: Dict)Dict

Recursively merges dictionaries d1 & d2.

Used for combining contents of snowmobile.toml and snowmobile-ext.toml at the appropriate level within a pair of nested dictionaries.

Note

The order of d1 and d2 matters; if an overlapping key at the same level exists in both dictionaries, the value within d1 will take precedent over the value in d2.

This function is invoked within the __init__() method of Configuration to merge snowmobile.toml and snowmobile-ext.toml, and the prioritization of overlapping values described above is the reason that specifications in snowmobile.toml will take precedent over any overlapping configurations in snowmobile.toml.

Parameters
  • d1 (dict) – First dictionary to merge.

  • d2 (dict) – Second dictionary to merge.

Returns (dict):

Dictionary containing recursively merged contents from d1 and d2

snowmobile.core.utils.parsing.up(nm: str)

Utility to truncate upper-casing strings as opposed to str.upper().

snowmobile.core.utils.parsing.strip(val: str, trailing: bool = True, blanks: bool = True, whitespace: bool = False)str

Utility to strip a variety whitespace from a string.

snowmobile.core.utils.parsing.dict_flatten(attrs: dict, delim: Optional[str] = None, indent_char: Optional[str] = None, bullet_char: Optional[str] = None)List[Tuple[str, str, str]]

Recursively flattens dictionary to its atomic elements.

Flattens a dictionary to its atomic state and performs parsing operations, separating each child key with a delim relative to its parent key.

This flattening enables the parsing of a nested dictionary into a valid string of markdown containing correspondingly nested bullets.

Parameters
  • attrs (dict) – Dictionary of attributes; most likely the attrs_parsed from Statement.

  • delim (str) – Delimiter to use for separating nested keys; defaults to ‘~’;

  • indent_char (str) – Character to use for indents; defaults to a tab (‘ ).

  • bullet_char (str) – Character to use for bullets/sub-bullets; defaults to ‘-‘.

Returns (List[Tuple[str, str, str]]):
A list of tuples containing:
  1. A string of the indentation to use; for 1st-level attributes, this will just be the bullet_char.

  2. The fully stratified key, including parents; for 1st-level attributes this will mirror the original key that was pr.

  3. The value of the associated key; this will always mirror the value that was pr.

snowmobile.core.utils.parsing.p(nm: str)Tuple[str, str]

Utility to parse cfg from dot-prefixed object if included.