From a962097db402819f10406c8d528a6c738f0b8e8c Mon Sep 17 00:00:00 2001 From: chfw Date: Sun, 6 Jan 2019 14:00:40 +0000 Subject: [PATCH 1/9] :sparkles: use template string as an input for '-t'. resolve #154 --- moban/jinja2/engine.py | 13 ++++++++++--- moban/plugins.py | 31 +++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/moban/jinja2/engine.py b/moban/jinja2/engine.py index eef41c02..10a59726 100644 --- a/moban/jinja2/engine.py +++ b/moban/jinja2/engine.py @@ -1,8 +1,9 @@ -from jinja2 import Environment, FileSystemLoader +from jinja2 import Template, Environment, FileSystemLoader from lml.loader import scan_plugins_regex from lml.plugin import PluginInfo, PluginManager +from jinja2.exceptions import TemplateNotFound -import moban.constants as constants +from moban import constants, exceptions JINJA2_LIBRARIES = "^moban_jinja2_.+$" JINJA2_EXENSIONS = [ @@ -91,9 +92,15 @@ def get_template(self, template_file): template file exists at: '/User/moban-pro/my-template/templates/myfile.jj2' """ - template = self.jj2_environment.get_template(template_file) + try: + template = self.jj2_environment.get_template(template_file) + except TemplateNotFound: + raise exceptions.FileNotFound("%s does not exist" % template_file) return template + def get_template_from_string(self, string): + return Template(string) + def apply_template(self, template, data, output): """ It is not expected this function to write content to file system. diff --git a/moban/plugins.py b/moban/plugins.py index a258fe2a..4b7b5927 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -48,10 +48,15 @@ def number_of_templated_files(self): def render_to_file(self, template_file, data_file, output_file): self.file_count = 1 data = self.context.get_data(data_file) - template = self.engine.get_template(template_file) - template_abs_path = utils.get_template_path( - self.template_dirs, template_file - ) + try: + template = self.engine.get_template(template_file) + template_abs_path = utils.get_template_path( + self.template_dirs, template_file + ) + except exceptions.TemplateFileMissing: + template = self.engine.get_template_from_string(template_file) + template_abs_path = "string template" + flag = self.apply_template( template_abs_path, template, data, output_file ) @@ -65,15 +70,21 @@ def apply_template(self, template_abs_path, template, data, output_file): ) rendered_content = utils.strip_off_trailing_new_lines(rendered_content) rendered_content = rendered_content.encode("utf-8") - flag = HASH_STORE.is_file_changed( - output_file, rendered_content, template_abs_path - ) - if flag: + try: + flag = HASH_STORE.is_file_changed( + output_file, rendered_content, template_abs_path + ) + if flag: + utils.write_file_out( + output_file, rendered_content, strip=False, encode=False + ) + utils.file_permissions_copy(template_abs_path, output_file) + return flag + except exceptions.FileNotFound: utils.write_file_out( output_file, rendered_content, strip=False, encode=False ) - utils.file_permissions_copy(template_abs_path, output_file) - return flag + return True def render_to_files(self, array_of_param_tuple): sta = Strategy(array_of_param_tuple) From f0d28a617accd1be4e729505b1c1dd0b67b76389 Mon Sep 17 00:00:00 2001 From: chfw Date: Sun, 6 Jan 2019 14:23:54 +0000 Subject: [PATCH 2/9] :sparkles: provide short form string template. #154 --- moban/constants.py | 1 + moban/main.py | 15 ++++++++++++++- moban/plugins.py | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/moban/constants.py b/moban/constants.py index ae11d2f9..fb21c2e0 100644 --- a/moban/constants.py +++ b/moban/constants.py @@ -21,6 +21,7 @@ LABEL_CONFIG_DIR = "configuration_dir" LABEL_PLUGIN_DIRS = "plugin_dir" LABEL_TEMPLATE = "template" +POSITIONAL_LABEL_TEMPLATE = "template2" LABEL_TMPL_DIRS = "template_dir" LABEL_OUTPUT = "output" LABEL_TEMPLATE_TYPE = "template_type" diff --git a/moban/main.py b/moban/main.py index 17e75722..c4aa696b 100644 --- a/moban/main.py +++ b/moban/main.py @@ -94,6 +94,13 @@ def create_parser(): parser.add_argument( "-m", "--%s" % constants.LABEL_MOBANFILE, help="custom moban file" ) + parser.add_argument( + constants.POSITIONAL_LABEL_TEMPLATE, + metavar="template", + type=str, + nargs="?", + help="string templates", + ) return parser @@ -153,8 +160,14 @@ def handle_command_line(options): act upon command options """ options = merge(options, constants.DEFAULT_OPTIONS) + print(options) if options[constants.LABEL_TEMPLATE] is None: - raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE) + if options[constants.POSITIONAL_LABEL_TEMPLATE] is None: + raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE) + else: + options[constants.LABEL_TEMPLATE] = options[ + constants.POSITIONAL_LABEL_TEMPLATE + ] engine = plugins.ENGINES.get_engine( options[constants.LABEL_TEMPLATE_TYPE], options[constants.LABEL_TMPL_DIRS], diff --git a/moban/plugins.py b/moban/plugins.py index 4b7b5927..fc332414 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -53,7 +53,7 @@ def render_to_file(self, template_file, data_file, output_file): template_abs_path = utils.get_template_path( self.template_dirs, template_file ) - except exceptions.TemplateFileMissing: + except exceptions.FileNotFound: template = self.engine.get_template_from_string(template_file) template_abs_path = "string template" From 9585cdc17df33d85584fd6ceb8df5ce961d550af Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 08:03:35 +0000 Subject: [PATCH 3/9] :hammer: code refactoring --- moban/constants.py | 2 +- moban/main.py | 28 +++++++++++++++------------- moban/plugins.py | 26 ++++++++++++++++++-------- tests/test_engine.py | 13 ++++++++++++- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/moban/constants.py b/moban/constants.py index fb21c2e0..d02aed8f 100644 --- a/moban/constants.py +++ b/moban/constants.py @@ -21,7 +21,7 @@ LABEL_CONFIG_DIR = "configuration_dir" LABEL_PLUGIN_DIRS = "plugin_dir" LABEL_TEMPLATE = "template" -POSITIONAL_LABEL_TEMPLATE = "template2" +POSITIONAL_LABEL_TEMPLATE = "template_in_string" LABEL_TMPL_DIRS = "template_dir" LABEL_OUTPUT = "output" LABEL_TEMPLATE_TYPE = "template_type" diff --git a/moban/main.py b/moban/main.py index c4aa696b..0ece570e 100644 --- a/moban/main.py +++ b/moban/main.py @@ -160,24 +160,26 @@ def handle_command_line(options): act upon command options """ options = merge(options, constants.DEFAULT_OPTIONS) - print(options) - if options[constants.LABEL_TEMPLATE] is None: - if options[constants.POSITIONAL_LABEL_TEMPLATE] is None: - raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE) - else: - options[constants.LABEL_TEMPLATE] = options[ - constants.POSITIONAL_LABEL_TEMPLATE - ] engine = plugins.ENGINES.get_engine( options[constants.LABEL_TEMPLATE_TYPE], options[constants.LABEL_TMPL_DIRS], options[constants.LABEL_CONFIG_DIR], ) - engine.render_to_file( - options[constants.LABEL_TEMPLATE], - options[constants.LABEL_CONFIG], - options[constants.LABEL_OUTPUT], - ) + if options[constants.LABEL_TEMPLATE] is None: + if options[constants.POSITIONAL_LABEL_TEMPLATE] is None: + raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE) + else: + engine.render_string_to_file( + options[constants.POSITIONAL_LABEL_TEMPLATE], + options[constants.LABEL_CONFIG], + options[constants.LABEL_OUTPUT], + ) + else: + engine.render_to_file( + options[constants.LABEL_TEMPLATE], + options[constants.LABEL_CONFIG], + options[constants.LABEL_OUTPUT], + ) engine.report() HASH_STORE.save_hashes() exit_code = reporter.convert_to_shell_exit_code( diff --git a/moban/plugins.py b/moban/plugins.py index fc332414..5ba36cee 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -48,14 +48,10 @@ def number_of_templated_files(self): def render_to_file(self, template_file, data_file, output_file): self.file_count = 1 data = self.context.get_data(data_file) - try: - template = self.engine.get_template(template_file) - template_abs_path = utils.get_template_path( - self.template_dirs, template_file - ) - except exceptions.FileNotFound: - template = self.engine.get_template_from_string(template_file) - template_abs_path = "string template" + template = self.engine.get_template(template_file) + template_abs_path = utils.get_template_path( + self.template_dirs, template_file + ) flag = self.apply_template( template_abs_path, template, data, output_file @@ -64,6 +60,20 @@ def render_to_file(self, template_file, data_file, output_file): reporter.report_templating(template_file, output_file) self.templated_count += 1 + def render_string_to_file( + self, template_in_string, data_file, output_file + ): + self.file_count = 1 + template = self.engine.get_template_from_string(template_in_string) + template_abs_path = template_in_string[:10] + "..." + data = self.context.get_data(data_file) + flag = self.apply_template( + template_abs_path, template, data, output_file + ) + if flag: + reporter.report_templating(template_abs_path, output_file) + self.templated_count += 1 + def apply_template(self, template_abs_path, template, data, output_file): rendered_content = self.engine.apply_template( template, data, output_file diff --git a/tests/test_engine.py b/tests/test_engine.py index 447adc51..9ce98862 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -48,7 +48,7 @@ def __init__(self, template_dirs): pass -@patch('moban.plugins.PluginManager.load_me_now', return_value=FakeEngine) +@patch("moban.plugins.PluginManager.load_me_now", return_value=FakeEngine) def test_default_mako_type(_): # fake mako engine = ENGINES.get_engine("fake", [], "") assert engine.engine_cls.__name__ == "FakeEngine" @@ -119,3 +119,14 @@ def test_environ_variables_as_data(): content = output_file.read() eq_(content, "foo") os.unlink(output) + + +def test_string_template(): + output = "test.txt" + path = os.path.join("tests", "fixtures") + engine = BaseEngine([path], path, Engine) + engine.render_string_to_file("{{simple}}", "simple.yaml", output) + with open(output, "r") as output_file: + content = output_file.read() + eq_(content, "yaml") + os.unlink(output) From e86fd1c752e045129447f5be6a5f564d5da6e9ff Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 08:09:27 +0000 Subject: [PATCH 4/9] :microscope: test command line options --- tests/integration_tests/test_command_line_options.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/integration_tests/test_command_line_options.py b/tests/integration_tests/test_command_line_options.py index a4c667c9..666fe3f8 100644 --- a/tests/integration_tests/test_command_line_options.py +++ b/tests/integration_tests/test_command_line_options.py @@ -82,6 +82,18 @@ def test_default_options(self, fake_template_doer): "a.jj2", "data.yml", "moban.output" ) + @patch("moban.plugins.BaseEngine.render_string_to_file") + def test_string_template(self, fake_template_doer): + string_template = "{{HELLO}}" + test_args = ["moban", string_template] + with patch.object(sys, "argv", test_args): + from moban.main import main + + main() + fake_template_doer.assert_called_with( + string_template, "data.yml", "moban.output" + ) + @raises(SystemExit) def test_no_argments(self): test_args = ["moban"] From 0e1af854bb74627035c613862557c613da64d74a Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 18:04:53 +0000 Subject: [PATCH 5/9] :microscope: test jinja2 engine get template from string --- tests/test_jinja2_engine.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/test_jinja2_engine.py b/tests/test_jinja2_engine.py index d3d888f1..1a979971 100644 --- a/tests/test_jinja2_engine.py +++ b/tests/test_jinja2_engine.py @@ -5,7 +5,7 @@ from moban.jinja2.engine import Engine -def test_handlebars_template_not_found(): +def test_jinja2_template(): path = os.path.join("tests", "fixtures", "jinja_tests") engine = Engine([path]) template = engine.get_template("file_tests.template") @@ -13,3 +13,13 @@ def test_handlebars_template_not_found(): result = engine.apply_template(template, data, None) expected = "yes\nhere" eq_(expected, result) + + +def test_jinja2_template_string(): + path = os.path.join("tests", "fixtures", "jinja_tests") + engine = Engine([path]) + template = engine.get_template_from_string("{{test}}") + data = dict(test="here") + result = engine.apply_template(template, data, None) + expected = "here" + eq_(expected, result) From 5d6470e294b28899a982d1b46623fa09945579d2 Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 18:10:01 +0000 Subject: [PATCH 6/9] :books: update readme --- README.rst | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index e699b4f4..075c2ac3 100644 --- a/README.rst +++ b/README.rst @@ -50,7 +50,17 @@ or clone it and install it: Quick start ================================================================================ -Here is a simple example: +.. code-block:: bash + + $ HELLO="world" moban "{{HELLO}}" + Warning: Both data.yml and /.../.moban.cd/data.yml does not exist + Warning: Attempting to use environment vars as data... + Templating {{HELLO}}... to moban.output + Templated 1 file. + $ cat moban.output + world + +A bit formal example: .. code-block:: bash From 5c2326a0d64a4e75b01196a1a96306d10879c4d2 Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 18:11:45 +0000 Subject: [PATCH 7/9] :books: command options update --- README.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 075c2ac3..ceb401bb 100644 --- a/README.rst +++ b/README.rst @@ -87,15 +87,19 @@ moban.output will contain:: Usage ================================================================================ -:: +code-block:: bash usage: moban [-h] [-cd CONFIGURATION_DIR] [-c CONFIGURATION] [-td [TEMPLATE_DIR [TEMPLATE_DIR ...]]] [-t TEMPLATE] [-o OUTPUT] [-f] [-m MOBANFILE] - + [template] + Yet another jinja2 cli command for static text generation + positional arguments: + template string templates + optional arguments: -h, --help show this help message and exit -cd CONFIGURATION_DIR, --configuration_dir CONFIGURATION_DIR From 6cfafdee8e389e42cd1440dc74cc9099eb6b9e45 Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 18:16:46 +0000 Subject: [PATCH 8/9] :books: explain environment variables. resolves #155 --- README.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ceb401bb..71dd2757 100644 --- a/README.rst +++ b/README.rst @@ -52,7 +52,8 @@ Quick start .. code-block:: bash - $ HELLO="world" moban "{{HELLO}}" + $ export HELLO="world" + $ moban "{{HELLO}}" Warning: Both data.yml and /.../.moban.cd/data.yml does not exist Warning: Attempting to use environment vars as data... Templating {{HELLO}}... to moban.output @@ -60,6 +61,12 @@ Quick start $ cat moban.output world +Or simply +.. code-block:: bash + + $ HELLO="world" moban "{{HELLO}}" + + A bit formal example: .. code-block:: bash @@ -79,6 +86,8 @@ moban.output will contain:: world +Please note that data.yml will take precedence over environment variables. + `the tutorial`_ has more use cases. .. _the tutorial: http://moban.readthedocs.org/en/latest/#tutorial From 7909bc97475369c8e166ae9e1e110101f6e5930d Mon Sep 17 00:00:00 2001 From: chfw Date: Thu, 10 Jan 2019 18:18:08 +0000 Subject: [PATCH 9/9] :lipstick: update version number --- .moban.cd/changelog.yml | 6 ++++++ .moban.cd/moban.yml | 6 +++--- CHANGELOG.rst | 9 +++++++++ docs/conf.py | 4 ++-- moban/_version.py | 2 +- setup.py | 8 ++++---- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index c6563382..452d5d2d 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -1,6 +1,12 @@ name: moban organisation: moremoban releases: +- changes: + - action: Updated + details: + - "`#154`: introduce first ever positional argument for string base template." + date: 11-1-2019 + version: 0.3.8 - changes: - action: Updated details: diff --git a/.moban.cd/moban.yml b/.moban.cd/moban.yml index 96a4c863..e354076a 100644 --- a/.moban.cd/moban.yml +++ b/.moban.cd/moban.yml @@ -3,9 +3,9 @@ organisation: moremoban author: C. W. contact: wangc_2011@hotmail.com license: MIT -version: 0.3.7 -current_version: 0.3.7 -release: 0.3.7 +version: 0.3.8 +current_version: 0.3.8 +release: 0.3.8 branch: master command_line_interface: "moban" entry_point: "moban.main:main" diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e3d12085..fcf58fae 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,15 @@ Change log ================================================================================ +0.3.8 - 11-1-2019 +-------------------------------------------------------------------------------- + +Updated +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +#. `#154 `_: introduce first ever + positional argument for string base template. + 0.3.7 - 6-1-2019 -------------------------------------------------------------------------------- diff --git a/docs/conf.py b/docs/conf.py index b8bd3a7b..ce753fd0 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -28,9 +28,9 @@ author = u'C. W.' # The short X.Y version -version = u'0.3.7' +version = u'0.3.8' # The full version, including alpha/beta/rc tags -release = u'0.3.7' +release = u'0.3.8' # -- General configuration --------------------------------------------------- diff --git a/moban/_version.py b/moban/_version.py index 2c02a9fe..846cb583 100644 --- a/moban/_version.py +++ b/moban/_version.py @@ -1,2 +1,2 @@ -__version__ = "0.3.7" +__version__ = "0.3.8" __author__ = "C. W." diff --git a/setup.py b/setup.py index dbaca9af..decfd6ff 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ NAME = 'moban' AUTHOR = 'C. W.' -VERSION = '0.3.7' +VERSION = '0.3.8' EMAIL = 'wangc_2011@hotmail.com' LICENSE = 'MIT' ENTRY_POINTS = { @@ -25,7 +25,7 @@ 'Yet another jinja2 cli command for static text generation' ) URL = 'https://github.com/moremoban/moban' -DOWNLOAD_URL = '%s/archive/0.3.7.tar.gz' % URL +DOWNLOAD_URL = '%s/archive/0.3.8.tar.gz' % URL FILES = ['README.rst', 'CONTRIBUTORS.rst', 'CHANGELOG.rst'] KEYWORDS = [ 'python', @@ -60,8 +60,8 @@ # You do not need to read beyond this line PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format( sys.executable) -GS_COMMAND = ('gs moban v0.3.7 ' + - "Find 0.3.7 in changelog for more details") +GS_COMMAND = ('gs moban v0.3.8 ' + + "Find 0.3.8 in changelog for more details") NO_GS_MESSAGE = ('Automatic github release is disabled. ' + 'Please install gease to enable it.') UPLOAD_FAILED_MSG = (