Skip to content

Instantly share code, notes, and snippets.

@PeterMosmans
Created April 7, 2020 06:49
Show Gist options
  • Save PeterMosmans/2589b2fdbf7af13c585c0bb49c79a175 to your computer and use it in GitHub Desktop.
Save PeterMosmans/2589b2fdbf7af13c585c0bb49c79a175 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# ln validate_jinja.py /usr/local/bin/validate_jinja
"""Jinja validator
This script validates Jinja files.
Copyright (C) 2020 Peter Mosmans [Go Forward]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
"""
import sys
from jinja2 import Environment, exceptions
def validate(filename):
"""Validate Jinja template."""
env = Environment()
try:
with open(filename) as template:
_rendered = env.parse(template.read())
except FileNotFoundError as exception:
print(f"Could not open file {filename}: {exception}", file=sys.stderr)
sys.exit(-1)
except exceptions.TemplateSyntaxError as exception:
print(f"Syntax error in {filename}: {exception}", file=sys.stderr)
sys.exit(-1)
def main():
"""Main program loop."""
if len(sys.argv) > 1:
validate(sys.argv[1])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment