Skip to content

Instantly share code, notes, and snippets.

@SamyCoenen
Created March 27, 2018 10:55
Show Gist options
  • Save SamyCoenen/059e99c3920f3a7de02f2b5a165b6760 to your computer and use it in GitHub Desktop.
Save SamyCoenen/059e99c3920f3a7de02f2b5a165b6760 to your computer and use it in GitHub Desktop.
YAML syntax checker written in Python
#!/usr/bin/env python3
# Author: Samy Coenen
# Company: Gluo NV
import yaml
import sys
import os, glob
from argparse import ArgumentParser
def get_args():
parser = ArgumentParser(description='Arguments')
parser.add_argument('-d', '--document',
required=True,
action='store',
help='The document or folder of documents to check')
args = parser.parse_args()
return args
def yaml_check(document):
if os.path.isdir(document):
for doc in glob.glob(document + "**/*.yml", recursive=True):
with open(doc, 'r') as stream:
try:
print('Loading ' + str(doc))
yaml.load(stream)
print('Success!')
except yaml.YAMLError as exc:
print(exc)
exit(1)
else:
with open(document, 'r') as stream:
try:
print(yaml.load(stream))
except yaml.YAMLError as exc:
print(exc)
def main():
args = get_args()
yaml_check(args.document)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment