From 3c0874979c119cc68ed6864c16254da5f978cd26 Mon Sep 17 00:00:00 2001 From: Ayan Banerjee Date: Sat, 2 Mar 2019 02:33:50 +0530 Subject: [PATCH] :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"