Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions j2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class Jinja2TemplateRenderer(object):
'jinja2.ext.loopcontrols',
)

def __init__(self, cwd, allow_undefined, j2_env_params):
def __init__(self, cwd, allow_undefined, no_compact=False, j2_env_params={}):
# Custom env params
j2_env_params.setdefault('keep_trailing_newline', True)
j2_env_params.setdefault('undefined', jinja2.Undefined if allow_undefined else jinja2.StrictUndefined)
j2_env_params.setdefault('trim_blocks', not no_compact)
j2_env_params.setdefault('lstrip_blocks', not no_compact)
j2_env_params.setdefault('extensions', self.ENABLED_EXTENSIONS)
j2_env_params.setdefault('loader', FilePathLoader(cwd))

Expand Down Expand Up @@ -118,7 +120,10 @@ def render_command(cwd, environ, stdin, argv):
help='Load custom Jinja2 tests from a Python file.')
parser.add_argument('--customize', default=None, metavar='python-file.py', dest='customize',
help='A Python file that implements hooks to fine-tune the j2cli behavior')
parser.add_argument('--undefined', action='store_true', dest='undefined', help='Allow undefined variables to be used in templates (no error will be raised)')
parser.add_argument('--no-compact', action='store_true', dest='no_compact',
help='Do not compact space around Jinja2 blocks.')
parser.add_argument('-U', '--undefined', action='store_true', dest='undefined',
help='Allow undefined variables to be used in templates (no error will be raised.)')
parser.add_argument('-o', metavar='outfile', dest='output_file', help="Output to a file instead of stdout")
parser.add_argument('template', help='Template file to process')
parser.add_argument('data', nargs='?', default=None, help='Input data file path; "-" to use stdin')
Expand Down Expand Up @@ -183,7 +188,7 @@ def render_command(cwd, environ, stdin, argv):
context = customize.alter_context(context)

# Renderer
renderer = Jinja2TemplateRenderer(cwd, args.undefined, j2_env_params=customize.j2_environment_params())
renderer = Jinja2TemplateRenderer(cwd, args.undefined, args.no_compact, j2_env_params=customize.j2_environment_params())
customize.j2_environment(renderer._env)

# Filters, Tests
Expand Down
9 changes: 2 additions & 7 deletions j2cli/context.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import six
import sys

# Patch basestring in for python 3 compat
try:
basestring
except NameError:
basestring = str

#region Parsers

def _parse_ini(data_string):
Expand Down Expand Up @@ -120,7 +115,7 @@ def _parse_env(data_string):
$ j2 config.j2 - < data.env
"""
# Parse
if isinstance(data_string, basestring):
if isinstance(data_string, six.string_types):
data = filter(
lambda l: len(l) == 2 ,
(
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
wheel
nose
exdoc
six
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

install_requires=[
'jinja2 >= 2.7.2',
'six >= 1.10',
],
extras_require={
'yaml': [pyyaml_version,]
Expand Down