Coverage for src/hooks/check_yaml_ruamel_pure.py: 0.00%
23 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from sys import argv, exit as sys_exit
5from typing import List
7# Modules libraries
8from ruamel.yaml import YAML
9from ruamel.yaml.error import YAMLError
11# INFO:
12# - New specific implementation for ruamel.yaml pure Python implementation only
13# - Recommendations for 'YAML(typ='safe', pure=True)':
14# - https://stackoverflow.com/a/62160118
15# - https://sourceforge.net/p/ruamel-yaml/tickets/391/
16# - Issues:
17# - https://github.com/pre-commit/pre-commit-hooks/issues/827
18# - https://github.com/pre-commit/pre-commit-hooks/issues/918
19# - https://github.com/pre-commit/pre-commit-hooks/issues/984
20# - https://github.com/pre-commit/pre-commit-hooks/pull/641
21# - https://github.com/pre-commit/pre-commit-hooks/pull/828
23# Check YAML file
24def check_yaml_file(file_path: str) -> bool:
26 # Instantiate YAML class
27 yaml = YAML(
28 typ='safe',
29 pure=True,
30 )
32 # Validate YAML file
33 try:
34 with open(
35 file_path,
36 encoding='utf8',
37 mode='r',
38 ) as file:
39 for _ in yaml.parse(file):
40 pass
41 return True
43 # Failure handlings
44 except YAMLError as exc:
45 print(exc)
46 return False
48# Main, pylint: disable=too-many-branches,too-many-statements
49def main() -> None:
51 # Variables
52 file_paths: List[str]
53 result: bool = True
55 # Validate arguments
56 if len(argv) <= 1:
57 sys_exit(1)
59 # Parse arguments
60 file_paths = argv[1:]
62 # Check YAML files
63 for file_path in file_paths:
64 if not check_yaml_file(file_path):
65 result = False
67 # Result
68 sys_exit(0 if result else 1)
70# Entrypoint
71if __name__ == '__main__': # pragma: no cover
72 main()