utilities#

Collection of helper functions that are shared by all sub-hooks.

append_safe(expected_line: str, path: Path) bool[source]#

Add a line to a file if it is not already present.

contains_line(input: Path | TextIOBase | str, expected_line: str) bool[source]#
hash_file(path: Path | str) str[source]#
read(input: Path | TextIOBase | str) str[source]#
write(content: str, target: Path | TextIOBase | str) None[source]#
remove_configs(paths: list[str]) None[source]#
rename_file(old: str, new: str) None[source]#

Rename a file and raise a PrecommitError.

remove_lines(file: Path, pattern: str, flags: RegexFlag = re.IGNORECASE) None[source]#
natural_sorting(text: str) list[float | str][source]#
update_file(relative_path: Path, in_template_folder: bool = False) None[source]#
get_nested_dict(data: dict, keys: list[str]) dict[source]#

Get a nested dictionary from a list of keys, create it if it doesn’t exist.

>>> data = {}
>>> sub_dict = get_nested_dict(data, keys=["a", "b"])
>>> sub_dict["c"] = 1
>>> data
{'a': {'b': {'c': 1}}}

Submodules and Subpackages