diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml index 105ce2d..dd4c951 100644 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -1,5 +1,6 @@ + diff --git a/src/renderTemplate.py b/src/renderTemplate.py index c73b684..048dbcf 100755 --- a/src/renderTemplate.py +++ b/src/renderTemplate.py @@ -24,8 +24,10 @@ 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') infilegroup.add_argument('--env', dest='env_file', action='store_true', - help='Load environment variables') + help='Load environment variables (from .env)') +argsparser.add_argument('--showvars', dest='show_vars', action='store_true', + help='Show all input variables and exit') argsparser.add_argument('--output', default='/dev/stdout', help='Output File') argsparser.add_argument('infile', metavar="input-file", @@ -33,8 +35,8 @@ argsparser.add_argument('infile', metavar="input-file", # Print Errors -def print_error(*args, **kwargs): - print(*args, file=sys.stderr, **kwargs) +def print_error(*cmdargs, **kwargs): + print(*cmdargs, file=sys.stderr, **kwargs) # Jinja Environment @@ -46,6 +48,7 @@ jenv = Environment(autoescape=True, # Jinja Context Functions +# ----------------------- @jinja2.pass_context def get_filtered_variables(c, filter_str=None): ret_val = {} @@ -89,16 +92,20 @@ jenv.filters['hostbyname'] = filter_hostbyname jenv.globals['variables'] = get_filtered_variables + # Loader # ------ def load_template(filename): try: with open(filename) as file_: - template = jenv.from_string(file_.read()) - return template + input_template = jenv.from_string(file_.read()) + return input_template except TemplateError as exc: - print_error("Template Error:", exc.message) - return None + print_error("ERROR: Reading template failed:", exc.message) + except FileNotFoundError: + print_error("ERROR:", filename, "- Template file not found") + + return None def load_yaml_file(filename): @@ -122,7 +129,7 @@ def load_toml_file(filename): except toml.TomlDecodeError as exc: print_error("TOML error:", exc) else: - print_error("ERROR:", filename, "- File not found") + print_error("ERROR:", filename, "- TOML Input file not found") return None @@ -135,7 +142,7 @@ def load_json_file(filename): except json.JSONDecodeError as exc: print_error("JSON error: [", exc.lineno, ",", exc.colno, "]", exc.msg) else: - print_error("ERROR:", filename, "- File not found") + print_error("ERROR:", filename, "- JSON Input file not found") return None @@ -146,36 +153,34 @@ def load_ini_file(filename): iniparser.read(filename) return iniparser.items() else: - print_error("ERROR:", filename, "- File not found") + print_error("ERROR:", filename, "- INI Input file not found") return None # Render # ------ -def render_template(template, outfile, variables=None): +def render_template(tpl, outfile, variables=None): try: with open(outfile, 'w') as file_: - file_.write(template.render(variables)) if variables else file_.write(template.render()) - + output_content = tpl.render(variables) if variables else tpl.render() + file_.write(output_content) except TemplateError as exc: print_error("Rendering Error:", exc.message) - print_error("Values:") - for i, v in variables.items(): - print_error(" %s = %s", i, v) if __name__ == '__main__': # Read command line arguments args = argsparser.parse_args() - tpl = load_template(args.infile) + # Load template file to ram + template = load_template(args.infile) content = None - # If template was found - if tpl: + if template: + # If template was found if args.env_file: - content = os.environ | dotenv_values("../test/.env") + content = os.environ | dotenv_values(".env") elif args.yaml_file: content = load_yaml_file(args.yaml_file) elif args.json_file: @@ -185,9 +190,12 @@ if __name__ == '__main__': 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) + if content: + if args.show_vars: + for i, v in content.items(): + print_error(i, "=", v) + else: + # Render template with or without external content + render_template(template, args.output, content) if content else render_template(template, args.output) exit(0)