From 3c0874979c119cc68ed6864c16254da5f978cd26 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 02:33:50 +0530 Subject: [PATCH 01/13] :sparkles: Expand the definition of template_type in targets section Closes https://github.com/moremoban/moban/issues/234 --- .../.moban.yml | 7 ++++ .../README.rst | 16 ++++++- .../a.template.jj2 | 1 + moban/definitions.py | 15 ++++++- moban/jinja2/engine.py | 1 + moban/mobanfile/__init__.py | 42 ++++++++++++------- moban/mobanfile/targets.py | 7 +++- moban/plugins/template.py | 10 ++++- tests/test_docs.py | 8 +++- 9 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 docs/level-18-user-defined-template-types/a.template.jj2 diff --git a/docs/level-18-user-defined-template-types/.moban.yml b/docs/level-18-user-defined-template-types/.moban.yml index 5a7870a2..bd20db77 100644 --- a/docs/level-18-user-defined-template-types/.moban.yml +++ b/docs/level-18-user-defined-template-types/.moban.yml @@ -9,3 +9,10 @@ configuration: - jinja2_time.TimeExtension targets: - a.output: a.template.file_type_of_my_choice + - output: b.output + template: a.template.jj2 + template_type: + - overrides: jinja2 + - options: + variable_start_string: '(((' + variable_end_string: ')))' diff --git a/docs/level-18-user-defined-template-types/README.rst b/docs/level-18-user-defined-template-types/README.rst index 49abd720..a2f54cb4 100644 --- a/docs/level-18-user-defined-template-types/README.rst +++ b/docs/level-18-user-defined-template-types/README.rst @@ -32,4 +32,18 @@ file found in level 4:: - a.output: a.template.file_type_of_my_choice -where `template_types` is a dictionary of different custom types +where `template_types` is a dictionary of different custom types. + +Also, you can define your `template` on the fly by putting the template +parameters inside targets. One such example is:: + + targets: + - output: b.output + template: a.template.jj2 + template_type: + - overrides: jinja2 + - options: + block_end_string: '*))' + block_start_string: '((*' + variable_start_string: '(((' + variable_end_string: ')))' diff --git a/docs/level-18-user-defined-template-types/a.template.jj2 b/docs/level-18-user-defined-template-types/a.template.jj2 new file mode 100644 index 00000000..72d9fa24 --- /dev/null +++ b/docs/level-18-user-defined-template-types/a.template.jj2 @@ -0,0 +1 @@ +((( nihao ))) diff --git a/moban/definitions.py b/moban/definitions.py index ce955a7e..1824af95 100644 --- a/moban/definitions.py +++ b/moban/definitions.py @@ -36,14 +36,19 @@ def __init__( data_file, output, template_type=constants.DEFAULT_TEMPLATE_TYPE, + needs_ad_hoc=False, ): self.template_file = template_file self.data_file = data_file self.original_output = output self.template_type = template_type self.output = self.original_output + self.needs_ad_hoc = needs_ad_hoc - self.set_template_type(template_type) + if needs_ad_hoc: + self.set_template_parameters(template_type) + else: + self.set_template_type(template_type) def set_template_type(self, new_template_type): self.template_type = new_template_type @@ -52,6 +57,14 @@ def set_template_type(self, new_template_type): else: self.output = self.original_output + def set_template_parameters(self, template_type): + template_parameters = self.template_type + self.template_type = {} + self.template_type[constants.LABEL_OVERRIDES] = ( + template_parameters[0][constants.LABEL_OVERRIDES]) + self.template_type[constants.TEMPLATE_TYPES_OPTIONS] = ( + template_parameters[1][constants.TEMPLATE_TYPES_OPTIONS]) + def __eq__(self, other): return ( self.template_file == other.template_file diff --git a/moban/jinja2/engine.py b/moban/jinja2/engine.py index 0d525f0d..57b71870 100644 --- a/moban/jinja2/engine.py +++ b/moban/jinja2/engine.py @@ -60,6 +60,7 @@ def __init__(self, template_dirs, options=None): A list template directories will be given to your engine class :param list temp_dirs: a list of template directories + :param dict options: a dictionary containing environmenta parameters """ load_jinja2_extensions() self.template_dirs = template_dirs diff --git a/moban/mobanfile/__init__.py b/moban/mobanfile/__init__.py index 455b30f2..1a1797de 100644 --- a/moban/mobanfile/__init__.py +++ b/moban/mobanfile/__init__.py @@ -118,24 +118,36 @@ def handle_targets(merged_options, targets): list_of_templating_parameters = parse_targets(merged_options, targets) jobs_for_each_engine = defaultdict(list) + count = 0 for target in list_of_templating_parameters: - forced_template_type = merged_options.get( - constants.LABEL_FORCE_TEMPLATE_TYPE - ) - if forced_template_type: - target.set_template_type(forced_template_type) - - template_type = target.template_type - primary_template_type = plugins.ENGINES.get_primary_key(template_type) - if primary_template_type is None: - primary_template_type = merged_options[ - constants.LABEL_TEMPLATE_TYPE - ] - target.set_template_type(primary_template_type) + if target.needs_ad_hoc: + engine = plugins.ENGINES.get_engine( + target.template_type, + merged_options[constants.LABEL_TMPL_DIRS], + merged_options[constants.LABEL_CONFIG_DIR], + needs_ad_hoc=True, + ) + engine.render_to_files([target]) + count = count + engine.number_of_templated_files() + else: + forced_template_type = merged_options.get( + constants.LABEL_FORCE_TEMPLATE_TYPE + ) + if forced_template_type: + target.set_template_type(forced_template_type) + + template_type = target.template_type + primary_template_type = plugins.ENGINES.get_primary_key( + template_type + ) + if primary_template_type is None: + primary_template_type = merged_options[ + constants.LABEL_TEMPLATE_TYPE + ] + target.set_template_type(primary_template_type) - jobs_for_each_engine[primary_template_type].append(target) + jobs_for_each_engine[primary_template_type].append(target) - count = 0 for template_type in jobs_for_each_engine.keys(): engine = plugins.ENGINES.get_engine( template_type, diff --git a/moban/mobanfile/targets.py b/moban/mobanfile/targets.py index d2011ee8..65b9515d 100644 --- a/moban/mobanfile/targets.py +++ b/moban/mobanfile/targets.py @@ -34,11 +34,16 @@ def _handle_explicit_target(options, target): data_file = target.get(constants.LABEL_CONFIG, common_data_file) output = target[constants.LABEL_OUTPUT] template_type = target.get(constants.LABEL_TEMPLATE_TYPE) + needs_ad_hoc = False + if template_type and len(template_type) > 0: + if constants.LABEL_OVERRIDES in template_type[0]: + needs_ad_hoc = True for src, dest, t_type in handle_template( template_file, output, options[constants.LABEL_TMPL_DIRS] ): if template_type: - yield TemplateTarget(src, data_file, dest, template_type) + yield TemplateTarget(src, data_file, dest, template_type, + needs_ad_hoc) else: if t_type: yield TemplateTarget(src, data_file, dest, t_type) diff --git a/moban/plugins/template.py b/moban/plugins/template.py index 0a9f08e1..6c91f5f2 100644 --- a/moban/plugins/template.py +++ b/moban/plugins/template.py @@ -26,8 +26,14 @@ def register_options(self, template_types): # see test_get_user_defined_engine for help self.options_registry.update(template_types) - def get_engine(self, template_type, template_dirs, context_dirs): - if template_type in self.options_registry: + def get_engine(self, template_type, template_dirs, context_dirs, + needs_ad_hoc=False): + if needs_ad_hoc: + engine_cls = self.load_me_now( + template_type[constants.LABEL_OVERRIDES] + ) + options = template_type[constants.TEMPLATE_TYPES_OPTIONS] + elif template_type in self.options_registry: custom_engine_spec = self.options_registry[template_type] engine_cls = self.load_me_now( custom_engine_spec[constants.TEMPLATE_TYPES_BASE_TYPE] diff --git a/tests/test_docs.py b/tests/test_docs.py index 3baa045a..d8ae7d33 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -235,13 +235,19 @@ def test_level_17_force_template_type_from_moban_file(self): folder = "level-17-force-template-type-from-moban-file" self._raw_moban(["moban"], folder, expected, "simple.file") - def test_level_18_user_defined_template_types(self): + def test_level_18_user_defined_template_types_a(self): from datetime import datetime expected = "{date}\n".format(date=datetime.now().strftime("%Y-%m-%d")) folder = "level-18-user-defined-template-types" self._raw_moban(["moban"], folder, expected, "a.output") + def test_level_18_user_defined_template_types_b(self): + expected = "shijie\n" + + folder = "level-18-user-defined-template-types" + self._raw_moban(["moban"], folder, expected, "b.output") + def test_misc_1(self): expected = "test file\n" From 9c3a57db2e13514898b566dc938041eb3305fb90 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 02:49:57 +0530 Subject: [PATCH 02/13] :books: Update changelog --- .moban.cd/changelog.yml | 6 ++++++ CHANGELOG.rst | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index 3e2f58fb..34022d1a 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -1,6 +1,12 @@ name: moban organisation: moremoban releases: +- changes: + - action: Added + details: + - "`#234`: Define template parameters on the fly inside `targets` section" + date: 01.03.2019 + version: 0.4.2 - changes: - action: Added details: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 971fde27..4885242b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,15 @@ Change log ================================================================================ + - +-------------------------------------------------------------------------------- + +Added +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +#. `#234 `_: Define template + parameters on the fly inside `targets` section + 0.4.1 - 28.02.2019 -------------------------------------------------------------------------------- From f84154f4598d4a17f0d9585c64d82a03e4c29963 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 03:12:34 +0530 Subject: [PATCH 03/13] :microscope: Add tests --- tests/test_definitions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_definitions.py b/tests/test_definitions.py index 9348c49f..aba2d386 100644 --- a/tests/test_definitions.py +++ b/tests/test_definitions.py @@ -42,3 +42,11 @@ def test_branch_params(): actual = require.clone_params() expected = {"single_branch": True, "branch": "ghpages", "depth": 2} eq_(expected, actual) + + +def test_set_template_parameters(): + template_type = [{'overrides': 'jinja2'}, {'options': {'param': 'value'}}] + require = TemplateTarget("template_file", "dat_file", "output.copy", + template_type=template_type, needs_ad_hoc=True) + assert require.template_type['overrides'] is template_type[0]['overrides'] + assert require.template_type['options'] is template_type[1]['options'] From 0ff667cdf29d8ac52f8e5aff023b06470ea9892e Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 03:28:42 +0530 Subject: [PATCH 04/13] :books: Update to using extension Closes https://github.com/moremoban/moban/issues/90 --- .../README.rst | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/level-12-use-template-engine-extensions/README.rst b/docs/level-12-use-template-engine-extensions/README.rst index 911a7266..cd04b2ff 100644 --- a/docs/level-12-use-template-engine-extensions/README.rst +++ b/docs/level-12-use-template-engine-extensions/README.rst @@ -34,3 +34,29 @@ Now, let us try to use the extension `with`. To do that, we have to enable the extension in the `.moban.yml` file following the above syntax. Now, the extension can be used in the jinja2 templates. One such example is shown in the `b.template` file. + +.. note: + + For some extensions, you may need to define `template environment parameters`. + In that case, you can take help of our `user defined template types` feature. + Please read level-18 for more info. We have explained it using an example + here. + + Let us consider the example of `jinja2_time`. If you want to use + `datetime_format` attribute, you need to specify the same using environmental + parameters, *i.e* `env.datetime_format = '%a, %d %b %Y %H:%M:%S'`. In order + to do this, you can specify `datetime_format` using environmental parameters, + something like:: + + configuration: + template_types: + my_own_type: + base_type: jinja2 + file_extensions: + - file_type_of_my_choice + options: + datetime_format: %a, %d %b %Y %H:%M:%S + extensions: + - jinja2_time.TimeExtension + targets: + - a.output: a.template.file_type_of_my_choice From dc23e7a29c2837ce015f07300d6d5d9cf2c5dfe1 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 13:16:38 +0530 Subject: [PATCH 05/13] :pencil: Use base_type instead of overrides --- docs/level-12-use-template-engine-extensions/README.rst | 2 +- docs/level-18-user-defined-template-types/.moban.yml | 2 +- docs/level-18-user-defined-template-types/README.rst | 2 +- moban/definitions.py | 4 ++-- moban/mobanfile/targets.py | 2 +- moban/plugins/template.py | 2 +- tests/test_definitions.py | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/level-12-use-template-engine-extensions/README.rst b/docs/level-12-use-template-engine-extensions/README.rst index cd04b2ff..587e91b0 100644 --- a/docs/level-12-use-template-engine-extensions/README.rst +++ b/docs/level-12-use-template-engine-extensions/README.rst @@ -35,7 +35,7 @@ extension in the `.moban.yml` file following the above syntax. Now, the extension can be used in the jinja2 templates. One such example is shown in the `b.template` file. -.. note: +.. note:: For some extensions, you may need to define `template environment parameters`. In that case, you can take help of our `user defined template types` feature. diff --git a/docs/level-18-user-defined-template-types/.moban.yml b/docs/level-18-user-defined-template-types/.moban.yml index bd20db77..3c564d89 100644 --- a/docs/level-18-user-defined-template-types/.moban.yml +++ b/docs/level-18-user-defined-template-types/.moban.yml @@ -12,7 +12,7 @@ targets: - output: b.output template: a.template.jj2 template_type: - - overrides: jinja2 + - base_type: jinja2 - options: variable_start_string: '(((' variable_end_string: ')))' diff --git a/docs/level-18-user-defined-template-types/README.rst b/docs/level-18-user-defined-template-types/README.rst index a2f54cb4..5c62dcf6 100644 --- a/docs/level-18-user-defined-template-types/README.rst +++ b/docs/level-18-user-defined-template-types/README.rst @@ -41,7 +41,7 @@ parameters inside targets. One such example is:: - output: b.output template: a.template.jj2 template_type: - - overrides: jinja2 + - base_type: jinja2 - options: block_end_string: '*))' block_start_string: '((*' diff --git a/moban/definitions.py b/moban/definitions.py index 1824af95..4e0c932d 100644 --- a/moban/definitions.py +++ b/moban/definitions.py @@ -60,8 +60,8 @@ def set_template_type(self, new_template_type): def set_template_parameters(self, template_type): template_parameters = self.template_type self.template_type = {} - self.template_type[constants.LABEL_OVERRIDES] = ( - template_parameters[0][constants.LABEL_OVERRIDES]) + self.template_type[constants.TEMPLATE_TYPES_BASE_TYPE] = ( + template_parameters[0][constants.TEMPLATE_TYPES_BASE_TYPE]) self.template_type[constants.TEMPLATE_TYPES_OPTIONS] = ( template_parameters[1][constants.TEMPLATE_TYPES_OPTIONS]) diff --git a/moban/mobanfile/targets.py b/moban/mobanfile/targets.py index 65b9515d..fb469822 100644 --- a/moban/mobanfile/targets.py +++ b/moban/mobanfile/targets.py @@ -36,7 +36,7 @@ def _handle_explicit_target(options, target): template_type = target.get(constants.LABEL_TEMPLATE_TYPE) needs_ad_hoc = False if template_type and len(template_type) > 0: - if constants.LABEL_OVERRIDES in template_type[0]: + if constants.TEMPLATE_TYPES_BASE_TYPE in template_type[0]: needs_ad_hoc = True for src, dest, t_type in handle_template( template_file, output, options[constants.LABEL_TMPL_DIRS] diff --git a/moban/plugins/template.py b/moban/plugins/template.py index 6c91f5f2..1589bae5 100644 --- a/moban/plugins/template.py +++ b/moban/plugins/template.py @@ -30,7 +30,7 @@ def get_engine(self, template_type, template_dirs, context_dirs, needs_ad_hoc=False): if needs_ad_hoc: engine_cls = self.load_me_now( - template_type[constants.LABEL_OVERRIDES] + template_type[constants.TEMPLATE_TYPES_BASE_TYPE] ) options = template_type[constants.TEMPLATE_TYPES_OPTIONS] elif template_type in self.options_registry: diff --git a/tests/test_definitions.py b/tests/test_definitions.py index aba2d386..e4019987 100644 --- a/tests/test_definitions.py +++ b/tests/test_definitions.py @@ -45,8 +45,8 @@ def test_branch_params(): def test_set_template_parameters(): - template_type = [{'overrides': 'jinja2'}, {'options': {'param': 'value'}}] + template_type = [{'base_type': 'jinja2'}, {'options': {'param': 'value'}}] require = TemplateTarget("template_file", "dat_file", "output.copy", template_type=template_type, needs_ad_hoc=True) - assert require.template_type['overrides'] is template_type[0]['overrides'] + assert require.template_type['base_type'] is template_type[0]['base_type'] assert require.template_type['options'] is template_type[1]['options'] From df95e248e199e104ac7bf28acdec785bccda9b08 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 19:28:48 +0530 Subject: [PATCH 06/13] :pencil: Change logic to use engine temorarily --- .moban.cd/changelog.yml | 4 ++-- CHANGELOG.rst | 2 +- moban/definitions.py | 15 +------------ moban/mobanfile/__init__.py | 42 +++++++++++++------------------------ moban/mobanfile/targets.py | 23 ++++++++++++++++---- moban/plugins/template.py | 10 ++------- tests/test_definitions.py | 8 ------- 7 files changed, 40 insertions(+), 64 deletions(-) diff --git a/.moban.cd/changelog.yml b/.moban.cd/changelog.yml index 34022d1a..7a3af283 100644 --- a/.moban.cd/changelog.yml +++ b/.moban.cd/changelog.yml @@ -5,8 +5,8 @@ releases: - action: Added details: - "`#234`: Define template parameters on the fly inside `targets` section" - date: 01.03.2019 - version: 0.4.2 + date: 01.03.2019 + version: 0.4.2 - changes: - action: Added details: diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4885242b..272797f1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,7 +1,7 @@ Change log ================================================================================ - - +0.4.2 - 01.03.2019 -------------------------------------------------------------------------------- Added diff --git a/moban/definitions.py b/moban/definitions.py index 4e0c932d..ce955a7e 100644 --- a/moban/definitions.py +++ b/moban/definitions.py @@ -36,19 +36,14 @@ def __init__( data_file, output, template_type=constants.DEFAULT_TEMPLATE_TYPE, - needs_ad_hoc=False, ): self.template_file = template_file self.data_file = data_file self.original_output = output self.template_type = template_type self.output = self.original_output - self.needs_ad_hoc = needs_ad_hoc - if needs_ad_hoc: - self.set_template_parameters(template_type) - else: - self.set_template_type(template_type) + self.set_template_type(template_type) def set_template_type(self, new_template_type): self.template_type = new_template_type @@ -57,14 +52,6 @@ def set_template_type(self, new_template_type): else: self.output = self.original_output - def set_template_parameters(self, template_type): - template_parameters = self.template_type - self.template_type = {} - self.template_type[constants.TEMPLATE_TYPES_BASE_TYPE] = ( - template_parameters[0][constants.TEMPLATE_TYPES_BASE_TYPE]) - self.template_type[constants.TEMPLATE_TYPES_OPTIONS] = ( - template_parameters[1][constants.TEMPLATE_TYPES_OPTIONS]) - def __eq__(self, other): return ( self.template_file == other.template_file diff --git a/moban/mobanfile/__init__.py b/moban/mobanfile/__init__.py index 1a1797de..455b30f2 100644 --- a/moban/mobanfile/__init__.py +++ b/moban/mobanfile/__init__.py @@ -118,36 +118,24 @@ def handle_targets(merged_options, targets): list_of_templating_parameters = parse_targets(merged_options, targets) jobs_for_each_engine = defaultdict(list) - count = 0 for target in list_of_templating_parameters: - if target.needs_ad_hoc: - engine = plugins.ENGINES.get_engine( - target.template_type, - merged_options[constants.LABEL_TMPL_DIRS], - merged_options[constants.LABEL_CONFIG_DIR], - needs_ad_hoc=True, - ) - engine.render_to_files([target]) - count = count + engine.number_of_templated_files() - else: - forced_template_type = merged_options.get( - constants.LABEL_FORCE_TEMPLATE_TYPE - ) - if forced_template_type: - target.set_template_type(forced_template_type) - - template_type = target.template_type - primary_template_type = plugins.ENGINES.get_primary_key( - template_type - ) - if primary_template_type is None: - primary_template_type = merged_options[ - constants.LABEL_TEMPLATE_TYPE - ] - target.set_template_type(primary_template_type) + forced_template_type = merged_options.get( + constants.LABEL_FORCE_TEMPLATE_TYPE + ) + if forced_template_type: + target.set_template_type(forced_template_type) + + template_type = target.template_type + primary_template_type = plugins.ENGINES.get_primary_key(template_type) + if primary_template_type is None: + primary_template_type = merged_options[ + constants.LABEL_TEMPLATE_TYPE + ] + target.set_template_type(primary_template_type) - jobs_for_each_engine[primary_template_type].append(target) + jobs_for_each_engine[primary_template_type].append(target) + count = 0 for template_type in jobs_for_each_engine.keys(): engine = plugins.ENGINES.get_engine( template_type, diff --git a/moban/mobanfile/targets.py b/moban/mobanfile/targets.py index fb469822..831eaf7a 100644 --- a/moban/mobanfile/targets.py +++ b/moban/mobanfile/targets.py @@ -1,6 +1,9 @@ +import uuid + from moban import constants from moban.definitions import TemplateTarget from moban.mobanfile.templates import handle_template +import moban.plugins as plugins def parse_targets(options, targets): @@ -34,16 +37,28 @@ def _handle_explicit_target(options, target): data_file = target.get(constants.LABEL_CONFIG, common_data_file) output = target[constants.LABEL_OUTPUT] template_type = target.get(constants.LABEL_TEMPLATE_TYPE) - needs_ad_hoc = False if template_type and len(template_type) > 0: if constants.TEMPLATE_TYPES_BASE_TYPE in template_type[0]: - needs_ad_hoc = True + adhoc_type = uuid.uuid4().hex + file_extension = uuid.uuid4().hex + base_type = template_type[0][constants.TEMPLATE_TYPES_BASE_TYPE] + template_types_options = template_type[1][ + constants.TEMPLATE_TYPES_OPTIONS + ] + the_adhoc_type = { + adhoc_type: { + "file_extensions": [file_extension], + constants.TEMPLATE_TYPES_BASE_TYPE: base_type, + constants.TEMPLATE_TYPES_OPTIONS: template_types_options, + } + } + plugins.ENGINES.register_options(the_adhoc_type) + template_type = file_extension for src, dest, t_type in handle_template( template_file, output, options[constants.LABEL_TMPL_DIRS] ): if template_type: - yield TemplateTarget(src, data_file, dest, template_type, - needs_ad_hoc) + yield TemplateTarget(src, data_file, dest, template_type) else: if t_type: yield TemplateTarget(src, data_file, dest, t_type) diff --git a/moban/plugins/template.py b/moban/plugins/template.py index 1589bae5..0a9f08e1 100644 --- a/moban/plugins/template.py +++ b/moban/plugins/template.py @@ -26,14 +26,8 @@ def register_options(self, template_types): # see test_get_user_defined_engine for help self.options_registry.update(template_types) - def get_engine(self, template_type, template_dirs, context_dirs, - needs_ad_hoc=False): - if needs_ad_hoc: - engine_cls = self.load_me_now( - template_type[constants.TEMPLATE_TYPES_BASE_TYPE] - ) - options = template_type[constants.TEMPLATE_TYPES_OPTIONS] - elif template_type in self.options_registry: + def get_engine(self, template_type, template_dirs, context_dirs): + if template_type in self.options_registry: custom_engine_spec = self.options_registry[template_type] engine_cls = self.load_me_now( custom_engine_spec[constants.TEMPLATE_TYPES_BASE_TYPE] diff --git a/tests/test_definitions.py b/tests/test_definitions.py index e4019987..9348c49f 100644 --- a/tests/test_definitions.py +++ b/tests/test_definitions.py @@ -42,11 +42,3 @@ def test_branch_params(): actual = require.clone_params() expected = {"single_branch": True, "branch": "ghpages", "depth": 2} eq_(expected, actual) - - -def test_set_template_parameters(): - template_type = [{'base_type': 'jinja2'}, {'options': {'param': 'value'}}] - require = TemplateTarget("template_file", "dat_file", "output.copy", - template_type=template_type, needs_ad_hoc=True) - assert require.template_type['base_type'] is template_type[0]['base_type'] - assert require.template_type['options'] is template_type[1]['options'] From 7f4d5e12c1145ab2e1e785f3a69f9b9170d2ccb4 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 01:02:51 +0530 Subject: [PATCH 07/13] :lipstick: Pump up the version --- .moban.cd/moban.yml | 4 ++-- docs/conf.py | 2 +- moban/_version.py | 2 +- setup.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.moban.cd/moban.yml b/.moban.cd/moban.yml index 5b0defad..1ea485bc 100644 --- a/.moban.cd/moban.yml +++ b/.moban.cd/moban.yml @@ -4,8 +4,8 @@ organisation: moremoban author: C. W. contact: wangc_2011@hotmail.com license: MIT -version: 0.4.1 -current_version: 0.4.1 +version: 0.4.2 +current_version: 0.4.2 release: 0.4.1 branch: master master: index diff --git a/docs/conf.py b/docs/conf.py index 07dd937f..1809f20d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ copyright = '2017-2019 Onni Software Ltd. and its contributors' author = 'Onni Software Ltd.' # The short X.Y version -version = '0.4.1' +version = '0.4.2' # The full version, including alpha/beta/rc tags release = '0.4.1' diff --git a/moban/_version.py b/moban/_version.py index 29b35a50..557698b6 100644 --- a/moban/_version.py +++ b/moban/_version.py @@ -1,2 +1,2 @@ -__version__ = "0.4.1" +__version__ = "0.4.2" __author__ = "C. W." diff --git a/setup.py b/setup.py index 877690b5..77f397d0 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ NAME = 'moban' AUTHOR = 'C. W.' -VERSION = '0.4.1' +VERSION = '0.4.2' EMAIL = 'wangc_2011@hotmail.com' LICENSE = 'MIT' ENTRY_POINTS = { From facd9ef8f9fb0d875612ec8790a56716aa610111 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 01:30:47 +0530 Subject: [PATCH 08/13] :microscope: More tests --- tests/mobanfile/test_targets.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/mobanfile/test_targets.py b/tests/mobanfile/test_targets.py index ef4c3575..98dae8c7 100644 --- a/tests/mobanfile/test_targets.py +++ b/tests/mobanfile/test_targets.py @@ -1,4 +1,5 @@ import os +import uuid from nose.tools import eq_ @@ -118,3 +119,20 @@ def test_use_moban_default_template_from_options(self): ) ] eq_(expected, actual) + + def test_ad_hoc_type(self): + target = dict(template=TEMPLATE, output=OUTPUT) + template_type = [{'base_type': 'jinja2'}, + {'options': [{'block_end_string': '*))'}, + {'block_start_string': '((*'}]}] + options = dict( + configuration=CONFIGURATION, + template_type=template_type, + template_dir=TEMPLATE_DIRS, + ) + + actual = list(targets._handle_explicit_target(options, target)) + file_extension = uuid.uuid4().hex + expected = [TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, + file_extension)] + eq_(actual, expected) From f9a77da2d5865f602ff44a0ab468e58f960e9291 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 01:40:18 +0530 Subject: [PATCH 09/13] :pencil: Improve test format --- tests/test_docs.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_docs.py b/tests/test_docs.py index d8ae7d33..44e4c664 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -235,18 +235,14 @@ def test_level_17_force_template_type_from_moban_file(self): folder = "level-17-force-template-type-from-moban-file" self._raw_moban(["moban"], folder, expected, "simple.file") - def test_level_18_user_defined_template_types_a(self): + def test_level_18_user_defined_template_types(self): from datetime import datetime expected = "{date}\n".format(date=datetime.now().strftime("%Y-%m-%d")) folder = "level-18-user-defined-template-types" self._raw_moban(["moban"], folder, expected, "a.output") - def test_level_18_user_defined_template_types_b(self): - expected = "shijie\n" - - folder = "level-18-user-defined-template-types" - self._raw_moban(["moban"], folder, expected, "b.output") + _verify_content("b.output", "shijie\n") def test_misc_1(self): expected = "test file\n" From cf808d853f03b16cdbee55e8f93bd88c83f72c21 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 14:55:16 +0530 Subject: [PATCH 10/13] :pencil: Update to latest syntax --- docs/level-18-user-defined-template-types/.moban.yml | 4 ++-- moban/mobanfile/targets.py | 10 +++++++--- moban/reporter.py | 6 ++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/level-18-user-defined-template-types/.moban.yml b/docs/level-18-user-defined-template-types/.moban.yml index 3c564d89..f4c8a442 100644 --- a/docs/level-18-user-defined-template-types/.moban.yml +++ b/docs/level-18-user-defined-template-types/.moban.yml @@ -12,7 +12,7 @@ targets: - output: b.output template: a.template.jj2 template_type: - - base_type: jinja2 - - options: + base_type: jinja2 + options: variable_start_string: '(((' variable_end_string: ')))' diff --git a/moban/mobanfile/targets.py b/moban/mobanfile/targets.py index 831eaf7a..8746c181 100644 --- a/moban/mobanfile/targets.py +++ b/moban/mobanfile/targets.py @@ -5,6 +5,8 @@ from moban.mobanfile.templates import handle_template import moban.plugins as plugins +from moban import reporter + def parse_targets(options, targets): for target in targets: @@ -38,11 +40,13 @@ def _handle_explicit_target(options, target): output = target[constants.LABEL_OUTPUT] template_type = target.get(constants.LABEL_TEMPLATE_TYPE) if template_type and len(template_type) > 0: - if constants.TEMPLATE_TYPES_BASE_TYPE in template_type[0]: + if constants.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type: + print(reporter.report_file_extension_not_needed()) + if constants.TEMPLATE_TYPES_BASE_TYPE in template_type: adhoc_type = uuid.uuid4().hex file_extension = uuid.uuid4().hex - base_type = template_type[0][constants.TEMPLATE_TYPES_BASE_TYPE] - template_types_options = template_type[1][ + base_type = template_type[constants.TEMPLATE_TYPES_BASE_TYPE] + template_types_options = template_type[ constants.TEMPLATE_TYPES_OPTIONS ] the_adhoc_type = { diff --git a/moban/reporter.py b/moban/reporter.py index c7ec42d6..e79ad989 100644 --- a/moban/reporter.py +++ b/moban/reporter.py @@ -10,6 +10,8 @@ MESSAGE_CLONING_REPO = "Cloning {0}..." MESSAGE_USING_ENV_VARS = "Attempting to use environment vars as data..." MESSAGE_TEMPLATE_NOT_IN_MOBAN_FILE = "{0} is not defined in your moban file!" +MESSAGE_FILE_EXTENSION_NOT_NEEDED = "File extension is not required for ad-hoc\ + type" def report_templating(source_file, destination_file): @@ -84,3 +86,7 @@ def _format_single(message, count): if count == 1: return message.replace("files", "file") return message + + +def report_file_extension_not_needed(): + report_info_message(MESSAGE_FILE_EXTENSION_NOT_NEEDED) From b20efda3c6b58c368b419e4de456512f31ed6156 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 15:03:05 +0530 Subject: [PATCH 11/13] :books: Update docs to latest syntax --- docs/level-18-user-defined-template-types/README.rst | 4 ++-- moban/jinja2/engine.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/level-18-user-defined-template-types/README.rst b/docs/level-18-user-defined-template-types/README.rst index 5c62dcf6..b2bdc566 100644 --- a/docs/level-18-user-defined-template-types/README.rst +++ b/docs/level-18-user-defined-template-types/README.rst @@ -41,8 +41,8 @@ parameters inside targets. One such example is:: - output: b.output template: a.template.jj2 template_type: - - base_type: jinja2 - - options: + base_type: jinja2 + options: block_end_string: '*))' block_start_string: '((*' variable_start_string: '(((' diff --git a/moban/jinja2/engine.py b/moban/jinja2/engine.py index 57b71870..8c00abab 100644 --- a/moban/jinja2/engine.py +++ b/moban/jinja2/engine.py @@ -60,7 +60,7 @@ def __init__(self, template_dirs, options=None): A list template directories will be given to your engine class :param list temp_dirs: a list of template directories - :param dict options: a dictionary containing environmenta parameters + :param dict options: a dictionary containing environmental parameters """ load_jinja2_extensions() self.template_dirs = template_dirs From 6f577c318300cb4a48ebc2889ee08e75e6bff576 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 15:56:37 +0530 Subject: [PATCH 12/13] :microscope: More tests --- tests/test_reporter.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_reporter.py b/tests/test_reporter.py index 5e60daeb..7e8265b5 100644 --- a/tests/test_reporter.py +++ b/tests/test_reporter.py @@ -83,3 +83,14 @@ def test_report_template_not_in_moban_file(): fake_stdout.getvalue(), "Warning: test.jj2 is not defined in your moban file!\n", ) + + +def test_report_file_extension_not_needed(): + patcher = patch("sys.stdout", new_callable=StringIO) + fake_stdout = patcher.start() + reporter.report_file_extension_not_needed() + patcher.stop() + eq_( + fake_stdout.getvalue(), + "Info: File extension is not required for ad-hoc type\n", + ) From b5d35bb092f16cf5adf72e845ceaadaed8e1f4a0 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sun, 3 Mar 2019 16:19:10 +0530 Subject: [PATCH 13/13] :pencil: Address the feedback --- moban/mobanfile/targets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/moban/mobanfile/targets.py b/moban/mobanfile/targets.py index 8746c181..442e9473 100644 --- a/moban/mobanfile/targets.py +++ b/moban/mobanfile/targets.py @@ -41,7 +41,7 @@ def _handle_explicit_target(options, target): template_type = target.get(constants.LABEL_TEMPLATE_TYPE) if template_type and len(template_type) > 0: if constants.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type: - print(reporter.report_file_extension_not_needed()) + reporter.report_file_extension_not_needed() if constants.TEMPLATE_TYPES_BASE_TYPE in template_type: adhoc_type = uuid.uuid4().hex file_extension = uuid.uuid4().hex @@ -51,7 +51,7 @@ def _handle_explicit_target(options, target): ] the_adhoc_type = { adhoc_type: { - "file_extensions": [file_extension], + constants.TEMPLATE_TYPES_FILE_EXTENSIONS: [file_extension], constants.TEMPLATE_TYPES_BASE_TYPE: base_type, constants.TEMPLATE_TYPES_OPTIONS: template_types_options, }