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 os
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import dotenv
|
||||||
import toml
|
import toml
|
||||||
import yaml
|
import yaml
|
||||||
from jinja2 import Environment, Template, TemplateError, pass_eval_context
|
from jinja2 import Environment, TemplateError
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
argsparser = argparse.ArgumentParser(description='Jinja Template Helper')
|
argsparser = argparse.ArgumentParser(description='Jinja Template Helper')
|
||||||
|
|
||||||
infilegroup = argsparser.add_mutually_exclusive_group(required=False)
|
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,
|
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',
|
argsparser.add_argument('--output', default='/dev/stdout',
|
||||||
help='Output File')
|
help='Output File')
|
||||||
argsparser.add_argument('infile', metavar="input-file",
|
argsparser.add_argument('infile', metavar="input-file",
|
||||||
@@ -40,6 +44,7 @@ jenv = Environment(autoescape=True,
|
|||||||
keep_trailing_newline=True,
|
keep_trailing_newline=True,
|
||||||
optimized=True)
|
optimized=True)
|
||||||
|
|
||||||
|
|
||||||
# Jinja Filters
|
# Jinja Filters
|
||||||
# -------------
|
# -------------
|
||||||
def filter_env(name, defvalue=""):
|
def filter_env(name, defvalue=""):
|
||||||
@@ -144,6 +149,7 @@ def render_template(template, outfile, variables=None):
|
|||||||
except TemplateError as exc:
|
except TemplateError as exc:
|
||||||
print_error("Rendering Error:", exc.message)
|
print_error("Rendering Error:", exc.message)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Read command line arguments
|
# Read command line arguments
|
||||||
args = argsparser.parse_args()
|
args = argsparser.parse_args()
|
||||||
@@ -153,6 +159,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
# If template was found
|
# If template was found
|
||||||
if tpl:
|
if tpl:
|
||||||
|
if args.env_file:
|
||||||
|
dotenv.load_dotenv(dotenv.find_dotenv(raise_error_if_not_found=False))
|
||||||
# Load content from file, if present
|
# Load content from file, if present
|
||||||
if args.yaml_file:
|
if args.yaml_file:
|
||||||
content = load_yaml_file(args.yaml_file)
|
content = load_yaml_file(args.yaml_file)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
toml>=0.10.2
|
toml>=0.10.2
|
||||||
PyYAML>=5.4.1
|
PyYAML>=5.4.1
|
||||||
Jinja2>=3.0.1
|
Jinja2>=3.0.1
|
||||||
|
python-dotenv>=0.18.0
|
||||||
Reference in new issue
Block a user