ssg.rule_yaml module

The rule_yaml module provides various utility functions for handling YAML files containing Jinja macros, without having to parse the macros.

ssg.rule_yaml.add_key_value(contents, key, start_line, new_value)[source]

Adds a new key to contents with the given value after line start_line, returning the result. Also adds a blank line afterwards.

Does not modify the value of contents.

ssg.rule_yaml.find_section_lines(file_contents, sec)[source]

Parses the given file_contents as YAML to find the section with the given identifier. Note that this does not call into the yaml library and thus correctly handles jinja macros at the expense of not being a strictly valid yaml parsing.

Returns a list of namedtuples (start, end) of the lines where section exists.

ssg.rule_yaml.get_section_lines(file_path, file_contents, key_name)[source]

From the given file_path and file_contents, find the lines describing the section key_name and returns the line range of the section.

ssg.rule_yaml.get_yaml_contents(rule_obj)[source]

From a rule_obj description, return a namedtuple of (path, contents); where path is the path to the rule YAML and contents is the list of lines in the file.

ssg.rule_yaml.has_duplicated_subkeys(file_path, file_contents, sections)[source]

Checks whether a section has duplicated keys. Note that these are silently eaten by the YAML parser we use.

ssg.rule_yaml.parse_from_yaml(file_contents, lines)[source]

Parse the given line range as a yaml, returning the parsed object.

ssg.rule_yaml.remove_lines(contents, lines)[source]

Remove the lines of the section from the parsed file, returning the new contents.

Does not modify the passed in contents.

ssg.rule_yaml.sort_section_keys(file_path, file_contents, sections, sort_func=None)[source]

Sort subkeys in a YAML file’s section.

ssg.rule_yaml.update_key_value(contents, key, old_value, new_value)[source]

Find key in the contents of a file and replace its value with the new value, returning the resulting file. This validates that the old value is constant and hasn’t changed since parsing its value.

Raises a ValueError when the key cannot be found in the given contents.

Does not modify the value of contents.