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
version: 0.4.2
- changes:
- action: Added
details:
Expand Down
4 changes: 2 additions & 2 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
current_version: 0.4.2
release: 0.4.1
branch: master
master: index
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
================================================================================

0.4.2 - 01.03.2019
--------------------------------------------------------------------------------

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
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

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:
base_type: 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:
base_type: 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 )))
2 changes: 1 addition & 1 deletion moban/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.4.1"
__version__ = "0.4.2"
__author__ = "C. W."
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 environmental parameters
"""
load_jinja2_extensions()
self.template_dirs = template_dirs
Expand Down
24 changes: 24 additions & 0 deletions moban/mobanfile/targets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import uuid

from moban import constants
from moban.definitions import TemplateTarget
from moban.mobanfile.templates import handle_template
import moban.plugins as plugins

from moban import reporter


def parse_targets(options, targets):
Expand Down Expand Up @@ -34,6 +39,25 @@ 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)
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.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type:
reporter.report_file_extension_not_needed()
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
if constants.TEMPLATE_TYPES_BASE_TYPE in template_type:
adhoc_type = uuid.uuid4().hex
file_extension = uuid.uuid4().hex
base_type = template_type[constants.TEMPLATE_TYPES_BASE_TYPE]
template_types_options = template_type[
constants.TEMPLATE_TYPES_OPTIONS
]
the_adhoc_type = {
adhoc_type: {
constants.TEMPLATE_TYPES_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]
):
Expand Down
6 changes: 6 additions & 0 deletions moban/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 18 additions & 0 deletions tests/mobanfile/test_targets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import uuid

from nose.tools import eq_

Expand Down Expand Up @@ -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)
ayan-b marked this conversation as resolved.
Show resolved Hide resolved
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)
2 changes: 2 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ def test_level_18_user_defined_template_types(self):
folder = "level-18-user-defined-template-types"
self._raw_moban(["moban"], folder, expected, "a.output")

_verify_content("b.output", "shijie\n")
ayan-b marked this conversation as resolved.
Show resolved Hide resolved

def test_misc_1(self):
expected = "test file\n"

Expand Down
11 changes: 11 additions & 0 deletions tests/test_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
ayan-b marked this conversation as resolved.
Show resolved Hide resolved