Skip to content

Commit

Permalink
Updates to allow templates to render even if all variables are not de…
Browse files Browse the repository at this point in the history
…fined.
  • Loading branch information
jeffrimko committed Dec 7, 2017
1 parent 657b2ae commit 824fef5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
32 changes: 21 additions & 11 deletions app/poppage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 #
##==============================================================#
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name = "poppage",
version = "0.6.5",
version = "0.6.6",
author = "Jeff Rimko",
author_email = "[email protected]",
description = "Utility for generating files and directories from templates.",
Expand Down
1 change: 1 addition & 0 deletions tests/_Run_Tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/cli_test_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 824fef5

Please sign in to comment.