From d4d3c8d787d665499abd27b4e980cb2a34702013 Mon Sep 17 00:00:00 2001 From: "Sithideth Viengkhou (Riedel)" Date: Mon, 30 Jun 2025 13:46:37 -0400 Subject: [PATCH 1/2] added license header - test. --- pre_commit_hooks/add_license_header.py | 66 ++++++++++++++++++++++++++ setup.cfg | 1 + 2 files changed, 67 insertions(+) create mode 100644 pre_commit_hooks/add_license_header.py diff --git a/pre_commit_hooks/add_license_header.py b/pre_commit_hooks/add_license_header.py new file mode 100644 index 00000000..27addf51 --- /dev/null +++ b/pre_commit_hooks/add_license_header.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +import argparse +from collections.abc import Generator +from collections.abc import Sequence +from typing import Any +from typing import NamedTuple + +import ruamel.yaml + +yaml = ruamel.yaml.YAML(typ='safe') + + +def _exhaust(gen: Generator[str]) -> None: + for _ in gen: + pass + + +def _parse_unsafe(*args: Any, **kwargs: Any) -> None: + _exhaust(yaml.parse(*args, **kwargs)) + + +def _load_all(*args: Any, **kwargs: Any) -> None: + _exhaust(yaml.load_all(*args, **kwargs)) + + +class Key(NamedTuple): + multi: bool + unsafe: bool + + +LOAD_FNS = { + Key(multi=False, unsafe=False): yaml.load, + Key(multi=False, unsafe=True): _parse_unsafe, + Key(multi=True, unsafe=False): _load_all, + Key(multi=True, unsafe=True): _parse_unsafe, +} + + +def main(argv: Sequence[str] | None = None) -> int: + parser = argparse.ArgumentParser() + parser.add_argument( + '-m', '--multi', '--allow-multiple-documents', action='store_true', + ) + parser.add_argument( + '--unsafe', action='store_true', + help=( + 'Instead of loading the files, simply parse them for syntax. ' + 'A syntax-only check enables extensions and unsafe constructs ' + 'which would otherwise be forbidden. Using this option removes ' + 'all guarantees of portability to other yaml implementations. ' + 'Implies --allow-multiple-documents' + ), + ) + parser.add_argument('filenames', nargs='*', help='Filenames to check.') + args = parser.parse_args(argv) + + load_fn = LOAD_FNS[Key(multi=args.multi, unsafe=args.unsafe)] + + retval = 0 + println("add_license_header.py") + return retval + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/setup.cfg b/setup.cfg index c5e6e0bd..7f3fc747 100644 --- a/setup.cfg +++ b/setup.cfg @@ -60,6 +60,7 @@ console_scripts = requirements-txt-fixer = pre_commit_hooks.requirements_txt_fixer:main sort-simple-yaml = pre_commit_hooks.sort_simple_yaml:main trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:main + add_license_header = pre_commit_hooks.add_license_header:main [bdist_wheel] universal = True From 7bb3f26160a265e740885a7a6b69d59ec6bc1e98 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 30 Jun 2025 17:54:48 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pre_commit_hooks/add_license_header.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre_commit_hooks/add_license_header.py b/pre_commit_hooks/add_license_header.py index 27addf51..09e436fb 100644 --- a/pre_commit_hooks/add_license_header.py +++ b/pre_commit_hooks/add_license_header.py @@ -58,7 +58,7 @@ def main(argv: Sequence[str] | None = None) -> int: load_fn = LOAD_FNS[Key(multi=args.multi, unsafe=args.unsafe)] retval = 0 - println("add_license_header.py") + println('add_license_header.py') return retval