Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand the definition of template_type in targets section #245

Merged
merged 13 commits into from
Mar 3, 2019
6 changes: 6 additions & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
@@ -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
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
version: 0.4.2
- changes:
- action: Added
details:
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Change log
================================================================================

-
--------------------------------------------------------------------------------

Added
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#234 <https://github.com/moremoban/moban/issues/234>`_: Define template
parameters on the fly inside `targets` section

0.4.1 - 28.02.2019
--------------------------------------------------------------------------------

Expand Down
26 changes: 26 additions & 0 deletions docs/level-12-use-template-engine-extensions/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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:
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
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: 8 additions & 0 deletions tests/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
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):
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
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