Skip to content

Instantly share code, notes, and snippets.

@SamyCoenen
Created March 27, 2018 10:59
Show Gist options
  • Save SamyCoenen/a837dde0e256defdbcc41c504dda363c to your computer and use it in GitHub Desktop.
Save SamyCoenen/a837dde0e256defdbcc41c504dda363c to your computer and use it in GitHub Desktop.
YAML begin- and endtag checker, written in Python
#!/usr/bin/env python3
# Author: Samy Coenen
# Company: Gluo NV
import os
from argparse import ArgumentParser
import glob
import checkyaml
def get_args():
parser = ArgumentParser(description='Arguments')
parser.add_argument('-p', '--path',
required=True,
action='store',
help='The path of folder or single document to check')
args = parser.parse_args()
return args
def loop(path):
if os.path.isdir(path):
for doc in glob.glob(path + '**/*.yml', recursive=True):
yaml_check_tags(doc)
else:
yaml_check_tags(path)
def yaml_check_tags(path):
changed = False
with open(path, 'r+') as f:
lines = f.readlines()
if len(lines) is 0:
print('File ' + path + ' is empty, skipping...')
return None
# Put pointer back to beginning of file
f.seek(0)
# check first line
if '---' not in lines[0]:
f.write('---\n')
changed = True
# write body
f.writelines(lines)
# check last line
if '...' not in lines[-1]:
f.write('...\n')
if not any('...' in line for line in lines):
f.write('...\n')
print('no ... found')
changed = True
if changed:
print(path + ' has been changed.')
else:
print(path + ' is ok.')
def main():
args = get_args()
loop(args.path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment