Skip to content

Instantly share code, notes, and snippets.

@tomazzaman
Created July 1, 2016 20:36
Show Gist options
  • Save tomazzaman/79af836ae11d7e1c4f857d4220146d56 to your computer and use it in GitHub Desktop.
Save tomazzaman/79af836ae11d7e1c4f857d4220146d56 to your computer and use it in GitHub Desktop.
Set configuration files with Jinja2
#!/usr/bin/env python
import os
import sys
from jinja2 import Environment, FileSystemLoader
def set_environment(template_dir):
env = Environment(loader=FileSystemLoader(template_dir), trim_blocks=True)
env.globals['env_var'] = lambda varname, default='': os.getenv(varname, default)
env.globals['exists'] = lambda varname: varname in os.environ
return env
def replace_variables():
template_file = os.path.abspath(sys.argv[1])
template_dir = os.path.dirname(template_file)
env = set_environment(template_dir)
print env.get_template(os.path.basename(template_file)).render()
if __name__ == '__main__':
replace_variables()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment