Skip to content

Commit

Permalink
✨ Expand the definition of template_type in targets section
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan-b committed Mar 1, 2019
1 parent 00b6c77 commit 3c08749
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 21 deletions.
7 changes: 7 additions & 0 deletions docs/level-18-user-defined-template-types/.moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ')))'
16 changes: 15 additions & 1 deletion docs/level-18-user-defined-template-types/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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: ')))'
1 change: 1 addition & 0 deletions docs/level-18-user-defined-template-types/a.template.jj2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
((( nihao )))
15 changes: 14 additions & 1 deletion moban/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions moban/jinja2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 27 additions & 15 deletions moban/mobanfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion moban/mobanfile/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions moban/plugins/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 3c08749

Please sign in to comment.