Added support for dotenv files
Optimized code
This commit is contained in:
1 parent
f04e8e5aa5
commit
e2441e2dd2
2 files changed
+11
-2
No files matched your search
+10
-2
@@ -4,12 +4,13 @@ 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)
|
||||
@@ -22,6 +23,9 @@ infilegroup.add_argument('--toml', dest='toml_file', type=str, required=False,
|
||||
infilegroup.add_argument('--ini', dest='ini_file', type=str, required=False,
|
||||
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')
|
||||
argsparser.add_argument('infile', metavar="input-file",
|
||||
@@ -40,6 +44,7 @@ jenv = Environment(autoescape=True,
|
||||
keep_trailing_newline=True,
|
||||
optimized=True)
|
||||
|
||||
|
||||
# Jinja Filters
|
||||
# -------------
|
||||
def filter_env(name, defvalue=""):
|
||||
@@ -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,3 +1,4 @@
|
||||
toml>=0.10.2
|
||||
PyYAML>=5.4.1
|
||||
Jinja2>=3.0.1
|
||||
python-dotenv>=0.18.0
|
||||
Reference in new issue
Block a user