From 7d1cc5222755574b1a7371e5559abe5525665026 Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Wed, 11 Sep 2019 12:53:33 +0000 Subject: [PATCH] Add optional comment removal in include_raw --- CHANGES.md | 4 ++++ cloudbender/__init__.py | 2 +- cloudbender/jinja.py | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 991e841..efc3bda 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changelog +## 0.7.5 +- Added warning if rendered templates exceed max. inline size of 51200 bytes +- Added optional removal of comments during include_raw processing to reduce user-data size + ## 0.7.4 - Fix for only Iterate in use diff --git a/cloudbender/__init__.py b/cloudbender/__init__.py index 799db8e..f211bf8 100644 --- a/cloudbender/__init__.py +++ b/cloudbender/__init__.py @@ -2,7 +2,7 @@ import logging __author__ = "Stefan Reimer" __email__ = "stefan@zero-downtimet.net" -__version__ = "0.7.4" +__version__ = "0.7.5" # Set up logging to ``/dev/null`` like a library is supposed to. diff --git a/cloudbender/jinja.py b/cloudbender/jinja.py index da78ab3..a290187 100644 --- a/cloudbender/jinja.py +++ b/cloudbender/jinja.py @@ -46,13 +46,23 @@ def option(context, attribute, default_value=u'', source='options'): @jinja2.contextfunction -def include_raw_gz(context, files=None, gz=True): +def include_raw_gz(context, files=None, gz=True, remove_comments=False): jenv = context.environment output = '' for name in files: output = output + jinja2.Markup(jenv.loader.get_source(jenv, name)[0]) - # logger.debug(output) + if remove_comments: + # Remove full line comments but not shebang + _re = re.compile(r'^\s*#[^!]') + stripped_output = '' + for curline in output.splitlines(): + if re.match(_re, curline): + logger.debug("Removed {}".format(curline)) + else: + stripped_output = stripped_output + curline + + output = stripped_output if not gz: return(output)