yaml_tools.utils module

Shared utility code.

class yaml_tools.utils.SortedSet(iterable)[source]

Bases: Set

Alternate set implementation favoring space over speed, while not requiring the set elements to be hashable. We also add a sort method.

sort()[source]

Why not be sorted?

class yaml_tools.utils.StrYAML(*, typ=None, pure=False, output=None, plug_ins=None)[source]

Bases: YAML

New API likes dumping straight to file/stdout, so we subclass and create ‘inefficient’ custom string dumper.

Parameters:
  • typ (Optional[Union[List[Text], Text]])

  • pure (Any)

  • output (Any)

  • plug_ins (Any)

dump(data, stream=None, **kw)[source]
yaml_tools.utils.get_filelist(dirpath, filepattern='*.txt', debug=False)[source]

Get path objects matching filepattern starting at dirpath and return a list of matching paths for any files found.

Parameters:
  • dirpath (str) – directory to start file search

  • filepattern (str) – file extension glob

  • debug (bool) – increase output verbosity

Return type:

List

yaml_tools.utils.get_profile_ids(prog_opts, debug=False)[source]

Replacement for get_filelist() when using the NIST profile ID text files (which are now packaged with the YAML config files).

Parameters:
  • prog_opts (Dict)

  • debug (bool)

Return type:

List[str]

yaml_tools.utils.get_profile_type(filename, debug=False)[source]

Get oscal profile type from filename, where profile type is one of the exported profile names, ie, HIGH, MODERATE, LOW, or PRIVACY.

Parameters:
  • filename (str)

  • debug (bool)

Return type:

str

yaml_tools.utils.load_config(prog_name='ymltoxml', pkg='yaml_tools.data', file_encoding='utf-8', debug=False)[source]

Load yaml configuration file and munchify the data. If local file is not found in current directory, the default will be loaded.

Parameters:
  • prog_name (str) – filename of calling script (no extension)

  • pkg (str) – name of calling package.path for importlib

  • file_encoding (str) – file encoding of config file

  • debug (bool) – enable extra processing info

Return type:

Tuple[Munch, Path]

yaml_tools.utils.pystache_render(*args, **kwargs)[source]

Render pystache template with strict mode enabled.

Return type:

Any

yaml_tools.utils.replace_angles(data)[source]

Replace angle bracket with original curly brace.

Parameters:

data (str)

Return type:

str

yaml_tools.utils.replace_curlys(data)[source]

Replace original outside curly brace with angle bracket.

Parameters:

data (str)

Return type:

str

yaml_tools.utils.restore_xml_comments(xmls)[source]

Turn tagged comment elements back into xml comments.

Parameters:

xmls (str) – xml (file) output from unparse

Returns:

processed xml string

Return type:

str

yaml_tools.utils.sort_from_parent(input_data, prog_opts)[source]

Sort a list based on whether the target sort key has a parent key.

Parameters:
  • input_data (Dict) – Dict obj representing YAML input data

  • prog_opts (Dict) – configuration options

Returns:

sorted input

Return type:

Dict

yaml_tools.utils.str_yaml_dumper(data, prog_opts)[source]

Small StrYAML() dump wrapper.

Parameters:
  • data (Dict)

  • prog_opts (Dict)

Return type:

Any

yaml_tools.utils.text_data_writer(outdata, prog_opts)[source]

Text data writer with optional formatting (default is raw); uses config setting for output format. Supports the same text data formats supported by the text_file_reader() input function:

  • csv

  • json

  • yaml

  • raw

Sends formatted data to stdout; redirect to a file as needed.

Parameters:
  • outdata (Dict) – data to be written to stdout

  • prog_opts (Dict) – configuration options

yaml_tools.utils.text_file_reader(file, prog_opts)[source]

Text file reader for specific data types including raw text. Tries to handle YAML, JSON, CSV, text files with IDs, and plain ASCII text. Read and parse the file data if file is one of the expected types and return data objects. For all supported types of data, return a dictionary (or a list if input is a sequence).

Parameters:
  • file (Path) – filename/path to read

  • prog_opts (Dict) – configuration options

Returns:

file data as dict or list

Raises:

FileTypeError – if input file extension is not in EXTENSIONS

Return type:

Any