Files
squeekboard/tools/yamlfmt
Dorota Czaplejewicz 4a7dc799be ci: Fix formatting
At the same time, reduce indentation level on lists because otherwise the formatter is broken.
2021-12-16 14:03:16 +00:00

44 lines
986 B
Python
Executable File

#!/usr/bin/env python3
"""Checks YAML files for correct formatting.
Usage: yamlfmt.py [--apply] file.yaml
"""
import io
from ruamel.yaml import YAML
import sys
args = sys.argv[:]
try:
args.remove('--apply')
want_apply = True
except ValueError:
want_apply = False
path = args[1]
def dump(yaml, yml):
buf = io.BytesIO()
yaml.dump(yml, buf)
return buf.getvalue().decode('utf-8')
with open(path) as f:
contents = f.read()
yaml = YAML()
yml = yaml.load(contents)
formatted = dump(yaml, yml)
well_formatted = formatted == contents
if not well_formatted:
print('The yaml file is not correctly formatted:', path)
if want_apply:
print('Correcting', path)
with open(path, 'w') as f:
f.write(formatted)
else:
print('Please use the following correction:')
print('----------corrected', path)
print(formatted)
print('----------end corrected', path)
sys.exit(1)