From de0dfaa9b48e5c8d7c4c47a8109d3bfa25f14149 Mon Sep 17 00:00:00 2001 From: Manolis Stamatogiannakis Date: Sat, 27 Jul 2019 23:27:02 +0200 Subject: [PATCH 1/3] Turned on whitespace control for Jinja2 block. This behaviour can be turned of with the --no-compact flag. See: http://jinja.pocoo.org/docs/2.10/templates/#whitespace-control Rationale: The common use of Jinja2 is to serve web requests in realtime. In this context, it is reasonable to turn off whitespace control to save some processing time. However, j2cli is meant mostly as a batch processing tool. I.e. it is invoked periodically, the generated output persists for much longer, and it is more likely to be inspected by a human. Therefore, enabling whitespace control by default to produce better looking output makes sense. --- j2cli/cli.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/j2cli/cli.py b/j2cli/cli.py index fae229a..56ba3e2 100644 --- a/j2cli/cli.py +++ b/j2cli/cli.py @@ -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)) @@ -118,6 +120,8 @@ 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('--no-compact', action='store_true', dest='no_compact', + help='Do not compact space around Jinja2 blocks.') 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('-o', metavar='outfile', dest='output_file', help="Output to a file instead of stdout") parser.add_argument('template', help='Template file to process') @@ -183,7 +187,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 From 846804c48f00984fff235a618620e0767c42f0e4 Mon Sep 17 00:00:00 2001 From: Manolis Stamatogiannakis Date: Sat, 27 Jul 2019 23:59:44 +0200 Subject: [PATCH 2/3] Added shorthand -U for --undefined. --- j2cli/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/j2cli/cli.py b/j2cli/cli.py index 56ba3e2..727ec08 100644 --- a/j2cli/cli.py +++ b/j2cli/cli.py @@ -122,7 +122,8 @@ def render_command(cwd, environ, stdin, argv): help='A Python file that implements hooks to fine-tune the j2cli behavior') parser.add_argument('--no-compact', action='store_true', dest='no_compact', help='Do not compact space around Jinja2 blocks.') - 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('-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') From cea0f31c982025ec6308ff21c380374cc2985968 Mon Sep 17 00:00:00 2001 From: Manolis Stamatogiannakis Date: Sun, 28 Jul 2019 15:15:01 +0200 Subject: [PATCH 3/3] Using cross-version string type provided by six. --- j2cli/context.py | 9 ++------- requirements-dev.txt | 1 + setup.py | 1 + 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/j2cli/context.py b/j2cli/context.py index f8035d4..547727e 100644 --- a/j2cli/context.py +++ b/j2cli/context.py @@ -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): @@ -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 , ( diff --git a/requirements-dev.txt b/requirements-dev.txt index 2ac3dff..f837c52 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ wheel nose exdoc +six diff --git a/setup.py b/setup.py index 71410d6..5733ee5 100755 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ install_requires=[ 'jinja2 >= 2.7.2', + 'six >= 1.10', ], extras_require={ 'yaml': [pyyaml_version,]