anyconfig.template

anyconfig.template module

Template rendering module for jinja2-based template config files.

anyconfig.template.tmpl_env(paths)
Parameters:paths – A list of template search paths
anyconfig.template.copen(filepath, flag='r', encoding=None)

FIXME: How to test this ?

>>> c = copen(__file__)
>>> c is not None
True
anyconfig.template.make_template_paths(template_file, paths=None)

Make up a list of template search paths from given template_file (absolute or relative path to the template file) and/or paths (a list of template search paths given by user).

NOTE: User-given paths will take higher priority over a dir of template_file.

Parameters:
  • template_file – Absolute or relative path to the template file
  • paths – A list of template search paths
Returns:

List of template paths ([str])

>>> make_template_paths("/path/to/a/template")
['/path/to/a']
>>> make_template_paths("/path/to/a/template", ["/tmp"])
['/tmp', '/path/to/a']
>>> os.chdir("/tmp")
>>> make_template_paths("./path/to/a/template")
['/tmp/path/to/a']
>>> make_template_paths("./path/to/a/template", ["/tmp"])
['/tmp', '/tmp/path/to/a']
anyconfig.template.render_s(tmpl_s, ctx=None, paths=None)

Compile and render given template string tmpl_s with context context.

Parameters:
  • tmpl_s – Template string
  • ctx – Context dict needed to instantiate templates
  • paths – Template search paths
Returns:

Compiled result (str)

>>> render_s("aaa") == "aaa"
True
>>> s = render_s('a = {{ a }}, b = "{{ b }}"', {'a': 1, 'b': 'bbb'})
>>> if SUPPORTED:
...     assert s == 'a = 1, b = "bbb"'
anyconfig.template.render_impl(template_file, ctx=None, paths=None)
Parameters:
  • template_file – Absolute or relative path to the template file
  • ctx – Context dict needed to instantiate templates
Returns:

Compiled result (str)

anyconfig.template.render(filepath, ctx=None, paths=None, ask=False)

Compile and render template and return the result as a string.

Parameters:
  • template_file – Absolute or relative path to the template file
  • ctx – Context dict needed to instantiate templates
  • paths – Template search paths
  • ask – Ask user for missing template location if True
Returns:

Compiled result (str)

anyconfig.template.try_render(filepath=None, content=None, **options)

Compile and render template and return the result as a string.

Parameters:
  • filepath – Absolute or relative path to the template file
  • content – Template content (str)
  • options – Keyword options passed to render() defined above.
Returns:

Compiled result (str) or None