First Issue
This commit is contained in:
commit
c9051d6307
12 files changed
+237
No files matched your search
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
Generated
+4
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
Generated
+8
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/JinjaTemplating.iml" filepath="$PROJECT_DIR$/.idea/JinjaTemplating.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Executable
+171
@@ -0,0 +1,171 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import configparser
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
import toml
|
||||||
|
import yaml
|
||||||
|
from jinja2 import Environment, Template, TemplateError, pass_eval_context
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
argsparser = argparse.ArgumentParser(description='Jinja Template Helper')
|
||||||
|
|
||||||
|
infilegroup = argsparser.add_mutually_exclusive_group(required=True)
|
||||||
|
infilegroup.add_argument('--yaml', dest='yaml_file', type=str, required=False,
|
||||||
|
help='YAML file path')
|
||||||
|
infilegroup.add_argument('--json', dest='json_file', type=str, required=False,
|
||||||
|
help='JSON file path')
|
||||||
|
infilegroup.add_argument('--toml', dest='toml_file', type=str, required=False,
|
||||||
|
help='TOML file path')
|
||||||
|
infilegroup.add_argument('--ini', dest='ini_file', type=str, required=False,
|
||||||
|
help='INI fil path')
|
||||||
|
|
||||||
|
argsparser.add_argument('--output', default='/dev/stdout',
|
||||||
|
help='Output File')
|
||||||
|
argsparser.add_argument('infile', metavar="input-file",
|
||||||
|
help="Input File")
|
||||||
|
|
||||||
|
|
||||||
|
# Print Errors
|
||||||
|
def print_error(*args, **kwargs):
|
||||||
|
print(*args, file=sys.stderr, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# Jinja Environment
|
||||||
|
# -----------------
|
||||||
|
jenv = Environment(autoescape=True,
|
||||||
|
trim_blocks=False,
|
||||||
|
keep_trailing_newline=True,
|
||||||
|
optimized=True)
|
||||||
|
|
||||||
|
# Jinja Filters
|
||||||
|
# -------------
|
||||||
|
def filter_env(name, defvalue=""):
|
||||||
|
return os.getenv(name, defvalue)
|
||||||
|
|
||||||
|
|
||||||
|
def filter_domain2ldap(value, unit='dc', delimiter='.'):
|
||||||
|
parts = value.split(delimiter)
|
||||||
|
concat_domain = ""
|
||||||
|
for part in parts:
|
||||||
|
if len(concat_domain) > 0:
|
||||||
|
concat_domain += ","
|
||||||
|
concat_domain = concat_domain + unit + "=" + part
|
||||||
|
return concat_domain
|
||||||
|
|
||||||
|
|
||||||
|
def filter_hostbyaddr(value):
|
||||||
|
addr = socket.gethostbyaddr(value)
|
||||||
|
if addr:
|
||||||
|
return addr[0]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_hostbyname(value):
|
||||||
|
return socket.gethostbyname(value)
|
||||||
|
|
||||||
|
|
||||||
|
jenv.filters['env'] = filter_env
|
||||||
|
jenv.filters['ldap_dn'] = filter_domain2ldap
|
||||||
|
jenv.filters['hostbyaddr'] = filter_hostbyaddr
|
||||||
|
jenv.filters['hostbyname'] = filter_hostbyname
|
||||||
|
|
||||||
|
|
||||||
|
# Loader
|
||||||
|
# ------
|
||||||
|
def load_template(filename):
|
||||||
|
try:
|
||||||
|
with open(filename) as file_:
|
||||||
|
template = jenv.from_string(file_.read())
|
||||||
|
return template
|
||||||
|
except TemplateError as exc:
|
||||||
|
print_error("Template Error:", exc.message)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_yaml_file(filename):
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
with open(filename, 'r') as instream:
|
||||||
|
try:
|
||||||
|
return yaml.safe_load(instream)
|
||||||
|
except yaml.YAMLError as exc:
|
||||||
|
print_error("YAML error:", exc)
|
||||||
|
else:
|
||||||
|
print_error("ERROR:", filename, "- File not found")
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_toml_file(filename):
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
with open(filename, 'r') as instream:
|
||||||
|
try:
|
||||||
|
return toml.load(instream)
|
||||||
|
except toml.TomlDecodeError as exc:
|
||||||
|
print_error("TOML error:", exc)
|
||||||
|
else:
|
||||||
|
print_error("ERROR:", filename, "- File not found")
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_json_file(filename):
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
with open(filename, 'r') as instream:
|
||||||
|
try:
|
||||||
|
return json.load(instream)
|
||||||
|
except json.JSONDecodeError as exc:
|
||||||
|
print_error("JSON error: [", exc.lineno ,"," ,exc.colno, "]", exc.msg)
|
||||||
|
else:
|
||||||
|
print_error("ERROR:", filename, "- File not found")
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def load_ini_file(filename):
|
||||||
|
iniparser = configparser.ConfigParser()
|
||||||
|
if os.path.isfile(filename):
|
||||||
|
iniparser.read(filename)
|
||||||
|
return iniparser.items()
|
||||||
|
else:
|
||||||
|
print_error("ERROR:", filename, "- File not found")
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
# Render
|
||||||
|
# ------
|
||||||
|
def render_template(template, outfile, variables=None):
|
||||||
|
try:
|
||||||
|
with open(outfile, 'w') as file_:
|
||||||
|
file_.write(template.render(variables)) if variables else file_.write(template.render())
|
||||||
|
|
||||||
|
except TemplateError as exc:
|
||||||
|
print_error("Rendering Error:", exc.message)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# Read command line arguments
|
||||||
|
args = argsparser.parse_args()
|
||||||
|
|
||||||
|
tpl = load_template(args.infile)
|
||||||
|
content = None
|
||||||
|
|
||||||
|
# If template was found
|
||||||
|
if tpl:
|
||||||
|
# Load content from file, if present
|
||||||
|
if args.yaml_file:
|
||||||
|
content = load_yaml_file(args.yaml_file)
|
||||||
|
elif args.json_file:
|
||||||
|
content = load_json_file(args.json_file)
|
||||||
|
elif args.toml_file:
|
||||||
|
content = load_toml_file(args.toml_file)
|
||||||
|
elif args.ini_file:
|
||||||
|
content = load_ini_file(args.ini_file)
|
||||||
|
|
||||||
|
# Render template with or without external content
|
||||||
|
render_template(tpl, args.output, content) if content else render_template(tpl, args.output)
|
||||||
|
else:
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
exit(0)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Type": "{{ "TESTVAR" | env }}",
|
||||||
|
"Level1": {
|
||||||
|
"Key": "{{ level1.key }}",
|
||||||
|
"Value": {{ level1.value }},
|
||||||
|
},
|
||||||
|
"LDAP": "{{ "example.com" | ldap_dn("ou") }}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[level1]
|
||||||
|
key=TestIni
|
||||||
|
value=42
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"level1": {
|
||||||
|
"key": "TestJson",
|
||||||
|
"value": 42
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
title = "Test"
|
||||||
|
|
||||||
|
[level1]
|
||||||
|
key = "TestToml"
|
||||||
|
value = 42
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
level1:
|
||||||
|
key: "TestYaml"
|
||||||
|
value: 42
|
||||||
|
|
||||||
Reference in new issue
Block a user