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 131d3c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
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
40 changes: 25 additions & 15 deletions moban/mobanfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,34 @@ 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)

jobs_for_each_engine[primary_template_type].append(target)
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)

count = 0
for template_type in jobs_for_each_engine.keys():
engine = plugins.ENGINES.get_engine(
template_type,
Expand Down
3 changes: 2 additions & 1 deletion moban/mobanfile/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def _handle_explicit_target(options, target):
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=True)
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

0 comments on commit 131d3c6

Please sign in to comment.