mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-02 10:56:52 +00:00
add check-yaml-sorted script and tests
This commit is contained in:
parent
6306a48f7d
commit
5b166227c9
5 changed files with 165 additions and 0 deletions
51
pre_commit_hooks/check_yaml_sorted.py
Normal file
51
pre_commit_hooks/check_yaml_sorted.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
"""Pre-commit hook to check that yaml files are sorted at the top-level.
|
||||
|
||||
Does not modify files. Simply parses and compares the stringified values of
|
||||
each element of the top-level object.
|
||||
|
||||
This allows it to handle all kinds of cases
|
||||
- lists of scalars
|
||||
- dicts by top-level key
|
||||
- lists of dicts by first key name
|
||||
- lists of dicts with same keys by first value...
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from itertools import tee
|
||||
from typing import Any
|
||||
from typing import Iterable
|
||||
from typing import Sequence
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def is_sorted(iterable: Iterable[Any]) -> bool:
|
||||
a_iter, b_iter = tee(str(e) for e in iterable)
|
||||
next(b_iter, None)
|
||||
for a, b in zip(a_iter, b_iter):
|
||||
if a > b:
|
||||
print(f'Items ({a[:32]}..., {b[:32]}...)')
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to check')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retval = 0
|
||||
|
||||
for filename in args.filenames:
|
||||
with open(filename) as fp:
|
||||
data = yaml.safe_load(fp)
|
||||
if not is_sorted(data):
|
||||
print(f'In file {filename}, items are out of order.')
|
||||
retval += 1
|
||||
|
||||
return retval
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue