Add a hook for yaml files. · lircs/pre-commit-hooks@3e45f53 · GitHub
Skip to content

Commit 3e45f53

Browse files
committed
Add a hook for yaml files.
1 parent 212b3dc commit 3e45f53

8 files changed

Lines changed: 59 additions & 3 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion

pre_commit_hooks/check_yaml.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
import argparse
3+
import sys
4+
import yaml
5+
6+
from pre_commit_hooks.util import entry
7+
8+
9+
@entry
10+
def check_yaml(argv):
11+
parser = argparse.ArgumentParser()
12+
parser.add_argument('filenames', nargs='+', help='Filenames to check.')
13+
args = parser.parse_args(argv)
14+
15+
retval = 0
16+
for filename in args.filenames:
17+
try:
18+
yaml.load(open(filename))
19+
except yaml.YAMLError as e:
20+
print e
21+
retval = 1
22+
return retval
23+
24+
25+
if __name__ == '__main__':
26+
sys.exit(check_yaml())

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
'argparse',
1010
'plumbum',
1111
'pyflakes',
12+
'pyyaml',
1213
'simplejson',
1314
],
1415
entry_points={
1516
'console_scripts': [
17+
'check-yaml = pre_commit_hooks.check_yaml:check_yaml',
1618
'debug-statement-hook = pre_commit_hooks.debug_statement_hook:debug_statement_hook',
17-
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
18-
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
1919
'end-of-file-fixer = pre_commit_hooks.end_of_file_fixer:end_of_file_fixer',
20+
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
21+
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
2022
],
2123
},
2224
)

testing/__init__.py

Whitespace-only changes.

testing/resources/bad_yaml.notyaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# It's surprisingly hard to make invalid yaml
2+
a: "

testing/resources/ok_yaml.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
im: ok yaml

testing/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
import os.path
3+
4+
5+
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
6+
7+
8+
def get_resource_path(path):
9+
return os.path.join(TESTING_DIR, 'resources', path)

tests/check_yaml_test.py

Lines changed: 14 additions & 0 deletions

0 commit comments

Comments
 (0)