diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index b56b009..1650559 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -1,5 +1,9 @@ = PopPage Changelog +== poppage-0.6.6 (2017-12-06) +=== Changed + - Can now render templates even if all the variables are not defined; useful for allowing default values in templates. + == poppage-0.6.5 (2017-11-05) === Fixed - Fixed issued with providing URL INPATH in a defaults file. diff --git a/app/poppage.py b/app/poppage.py index 7669084..1978de4 100644 --- a/app/poppage.py +++ b/app/poppage.py @@ -60,7 +60,7 @@ import qprompt from binaryornot.check import is_binary from docopt import docopt -from jinja2 import FileSystemLoader, Template, meta +from jinja2 import FileSystemLoader, Template, Undefined, meta from jinja2.environment import Environment from jinja2schema import infer, model @@ -81,11 +81,23 @@ ##==============================================================# #: Application version string. -__version__ = "0.6.5" +__version__ = "0.6.6" #: Key separator. KEYSEP = "::" +##==============================================================# +## SECTION: Class Definitions # +##==============================================================# + +class SkipUndefined(Undefined): + def _fail_with_undefined_error(self, *args, **kwargs): + return None + def __getitem__(self, key): + return self + def __getattr__(self, key): + return self + ##==============================================================# ## SECTION: Function Definitions # ##==============================================================# @@ -170,22 +182,21 @@ def check_tmplitems(items, tmpldict, topkey=""): missing = [] return missing -def render_str(tmplstr, tmpldict): +def render_str(tmplstr, tmpldict, bail_miss=False): """Renders the given template string using the given template variable dictionary. Returns the rendered text as a string.""" - env = Environment(extensions=['jinja2_time.TimeExtension']) + env = Environment(undefined=SkipUndefined, extensions=['jinja2_time.TimeExtension']) miss = check_template(tmplstr, tmpldict) if miss: - qprompt.error("Template vars `%s` were not supplied values!" % ( + qprompt.warn("Template vars `%s` were not supplied values!" % ( "/".join(miss))) - return - return Template(tmplstr).render(**tmpldict) + return env.from_string(tmplstr).render(**tmpldict) -def render_file(tmplpath, tmpldict): +def render_file(tmplpath, tmpldict, bail_miss=False): """Renders the template file and the given path using the given template variable dictionary. Returns the rendered text as a string.""" tmplpath = op.abspath(tmplpath) - env = Environment(extensions=['jinja2_time.TimeExtension']) + env = Environment(undefined=SkipUndefined, extensions=['jinja2_time.TimeExtension']) for encoding in ["utf-8", "mbcs"]: try: env.loader = FileSystemLoader(op.dirname(tmplpath), encoding=encoding) @@ -200,10 +211,9 @@ def render_file(tmplpath, tmpldict): tmplstr = fo.read() miss = check_template(tmplstr, tmpldict) if miss: - qprompt.error("Template vars `%s` in `%s` were not supplied values!" % ( + qprompt.warn("Template vars `%s` in `%s` were not supplied values!" % ( "/".join(miss), op.basename(tmplpath))) - return return tmpl.render(**tmpldict) @handle_paths(inpath=0,outpath=2) diff --git a/app/setup.py b/app/setup.py index 061b2e3..e7942b7 100644 --- a/app/setup.py +++ b/app/setup.py @@ -22,7 +22,7 @@ setup( name = "poppage", - version = "0.6.5", + version = "0.6.6", author = "Jeff Rimko", author_email = "jeffrimko@gmail.com", description = "Utility for generating files and directories from templates.", diff --git a/tests/_Run_Tests.py b/tests/_Run_Tests.py index e286d58..56ae276 100644 --- a/tests/_Run_Tests.py +++ b/tests/_Run_Tests.py @@ -22,6 +22,7 @@ def run_tests(): okay = [] for i in os.listdir("."): if i.find("_test_") > -1 and i.endswith(".py"): + print("-- " + i + " --") if 0 != subprocess.call("python " + i, shell=True): fail.append(i) else: diff --git a/tests/cli_test_3.py b/tests/cli_test_3.py index 95f58f7..172a7b7 100644 --- a/tests/cli_test_3.py +++ b/tests/cli_test_3.py @@ -13,10 +13,10 @@ class TestCase(BaseTest): def test_cli_1(test): - """Check for basic check CLI functionality.""" retval = call('check --inpath ./templates/t5.jinja2') test.assertEqual(0, retval) + def test_cli_2(test): retval = call('check') test.assertEqual(1, retval)