Added support for dotenv files

Optimized code
This commit is contained in:
dennis authored and Dennis Twardowsky committed 2021-07-05 23:57:01 +02:00
1 parent f04e8e5aa5
commit e2441e2dd2
2 files changed
+16 -7

No files matched your search

+15 -7
View File
@@ -4,23 +4,27 @@ import json
import os
import socket
import sys
import dotenv
import toml
import yaml
from jinja2 import Environment, Template, TemplateError, pass_eval_context
from jinja2 import Environment, TemplateError
import argparse
argsparser = argparse.ArgumentParser(description='Jinja Template Helper')
infilegroup = argsparser.add_mutually_exclusive_group(required=False)
infilegroup.add_argument('--yaml', dest='yaml_file', type=str, required=False,
help='YAML file path')
help='YAML file path')
infilegroup.add_argument('--json', dest='json_file', type=str, required=False,
help='JSON file path')
help='JSON file path')
infilegroup.add_argument('--toml', dest='toml_file', type=str, required=False,
help='TOML file path')
help='TOML file path')
infilegroup.add_argument('--ini', dest='ini_file', type=str, required=False,
help='INI fil path')
help='INI fil path')
argsparser.add_argument('--env', dest='env_file', type=bool, required=False, default=False,
help='Load .env file from current directory')
argsparser.add_argument('--output', default='/dev/stdout',
help='Output File')
@@ -40,6 +44,7 @@ jenv = Environment(autoescape=True,
keep_trailing_newline=True,
optimized=True)
# Jinja Filters
# -------------
def filter_env(name, defvalue=""):
@@ -116,7 +121,7 @@ def load_json_file(filename):
try:
return json.load(instream)
except json.JSONDecodeError as exc:
print_error("JSON error: [", exc.lineno ,"," ,exc.colno, "]", exc.msg)
print_error("JSON error: [", exc.lineno, ",", exc.colno, "]", exc.msg)
else:
print_error("ERROR:", filename, "- File not found")
@@ -144,6 +149,7 @@ def render_template(template, outfile, variables=None):
except TemplateError as exc:
print_error("Rendering Error:", exc.message)
if __name__ == '__main__':
# Read command line arguments
args = argsparser.parse_args()
@@ -153,6 +159,8 @@ if __name__ == '__main__':
# If template was found
if tpl:
if args.env_file:
dotenv.load_dotenv(dotenv.find_dotenv(raise_error_if_not_found=False))
# Load content from file, if present
if args.yaml_file:
content = load_yaml_file(args.yaml_file)
+1
View File
@@ -1,3 +1,4 @@
toml>=0.10.2
PyYAML>=5.4.1
Jinja2>=3.0.1
python-dotenv>=0.18.0