Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .azure-pipelines-steps-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ steps:
displayName: 'Use Python 3.x'
- script: |
python -m pip install --upgrade pip setuptools wheel
test ! -f rnd_requirements.txt || pip install -r rnd_requirements.txt
pip install -r requirements.txt
pip install -r tests/requirements.txt
displayName: 'Setup dependencies'
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ steps:
displayName: 'Use Python 3.x'
- script: |
python -m pip install --upgrade pip setuptools wheel
if exist rnd_requirements.txt pip install -r rnd_requirements.txt
pip install -r requirements.txt
pip install -r tests\requirements.txt
displayName: 'Setup dependencies'
Expand Down
7 changes: 7 additions & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: moban
organisation: moremoban
releases:
- changes:
- action: Updated
details:
- "`#90`: allow adding extra jinja2 extensions. `jinja2.ext.do`, `jinja2.ext.loopcontrols` are included by default. what's more, any other template enigne are eligible for extension additions."
- "`#158`: Empty file base_engine.py is finally removed"
date: 18-1-2019
version: 0.3.9
- changes:
- action: Updated
details:
Expand Down
8 changes: 4 additions & 4 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.3.8
current_version: 0.3.8
release: 0.3.8
version: 0.3.9
current_version: 0.3.9
release: 0.3.9
branch: master
command_line_interface: "moban"
entry_point: "moban.main:main"
company: Onni Software Ltd.
copyright_year: 2017-2018
copyright_year: 2017-2019
keywords:
- jinja2
- moban
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python:
- 3.7-dev
- 3.6
- 3.5
- 3.4
- 2.7
before_install:
- if [[ $TRAVIS_PYTHON_VERSION == "2.6" ]]; then pip install flake8==2.6.2; fi
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
Change log
================================================================================

0.3.9 - 18-1-2019
--------------------------------------------------------------------------------

Updated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#90 <https://github.com/moremoban/moban/issues/90>`_: allow adding extra
jinja2 extensions. `jinja2.ext.do`, `jinja2.ext.loopcontrols` are included by
default. what's more, any other template enigne are eligible for extension
additions.
#. `#158 <https://github.com/moremoban/moban/issues/158>`_: Empty file
base_engine.py is finally removed

0.3.8 - 12-1-2019
--------------------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
# -- Project information -----------------------------------------------------

project = u'moban'
copyright = u'2017-2018 Onni Software Ltd.'
copyright = u'2017-2019 Onni Software Ltd.'
author = u'C. W.'

# The short X.Y version
version = u'0.3.8'
version = u'0.3.9'
# The full version, including alpha/beta/rc tags
release = u'0.3.8'
release = u'0.3.9'


# -- General configuration ---------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions docs/level-12-use-template-engine-extensions/.moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
targets:
- a.output: a.template
- b.output: b.template
extensions:
jinja2:
- jinja2.ext.with_
36 changes: 36 additions & 0 deletions docs/level-12-use-template-engine-extensions/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Level 12: use template engine extensions
================================================================================

jinja2 comes with a lot of extensions. In order not to be the blocker in the
middle, **extensions** is allowed in moban file to initialize jinja2 engine
with desired extensions. Two extensions, expression-statement and loop-controls
are enabled by default.

The extensions syntax is::

extensions:
template_type:
- template.engine.specific.extension

For example::

extensions:
jinja2:
- jinja2.ext.i18n

Please also note that the following extensions are included by default:
`jinja2.ext.do`, `jinja2.ext.loopcontrols`


Evaluation
--------------------------------------------------------------------------------
Please go to `docs/level-12-use-template-engine-extensions` directory.

If you notice the file `a.template`, we are using a for loop control. This is
because moban comes with two default extensions loop-controls and
expression-statement.

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.
3 changes: 3 additions & 0 deletions docs/level-12-use-template-engine-extensions/a.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for _ in range(1,5) %}
{{ hello }}
{% endfor %}
9 changes: 9 additions & 0 deletions docs/level-12-use-template-engine-extensions/b.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% with %}
{% set foo = 142 %}
{{ foo }}
{% with %}
{% set foo = 42 %}
{{ foo }}
{% endwith %}
{{ foo }}
{% endwith %}
1 change: 1 addition & 0 deletions docs/level-12-use-template-engine-extensions/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello: world
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.3.8"
__version__ = "0.3.9"
__author__ = "C. W."
Empty file removed moban/base_engine.py
Empty file.
11 changes: 7 additions & 4 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
LABEL_TEMPLATE = "template"
POSITIONAL_LABEL_TEMPLATE = "template_in_string"
LABEL_TMPL_DIRS = "template_dir"
LABEL_EXIT_CODE = "exit-code"
LABEL_OUTPUT = "output"
LABEL_TEMPLATE_TYPE = "template_type"
LABEL_TARGETS = "targets"
LABEL_COPY = "copy"
LABEL_OVERRIDES = "overrides"
LABEL_MOBANFILE = "mobanfile"
LABEL_FORCE = "force"
LABEL_REQUIRES = "requires"
LABEL_EXIT_CODE = "exit-code"


DEFAULT_CONFIGURATION_DIRNAME = ".moban.cd"
DEFAULT_TEMPLATE_DIRNAME = ".moban.td"
Expand All @@ -52,6 +50,11 @@
DEFAULT_MOBAN_VERSION = "1.0"
MOBAN_DIR_NAME_UNDER_USER_HOME = ".moban"
MOBAN_REPOS_DIR_NAME = "repos"
# moban file configuration
LABEL_REQUIRES = "requires"
LABEL_TARGETS = "targets"
LABEL_COPY = "copy"
LABEL_EXTENSIONS = "extensions"

# error messages
ERROR_DATA_FILE_NOT_FOUND = "Both %s and %s does not exist"
Expand Down
20 changes: 18 additions & 2 deletions moban/jinja2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"moban.jinja2.filters.text",
"moban.jinja2.tests.files",
]
JINJA2_THIRD_PARTY_EXTENSIONS = ["jinja2.ext.do", "jinja2.ext.loopcontrols"]


class PluginMixin:
Expand Down Expand Up @@ -50,7 +51,7 @@ def __init__(self):
constants.TEMPLATE_ENGINE_EXTENSION, tags=["jinja2", "jinja", "jj2", "j2"]
)
class Engine(object):
def __init__(self, template_dirs):
def __init__(self, template_dirs, extensions=None):
"""
Contruct a jinja2 template engine

Expand All @@ -61,12 +62,19 @@ def __init__(self, template_dirs):
load_jinja2_extensions()
self.template_dirs = template_dirs
template_loader = FileSystemLoader(template_dirs)
self.jj2_environment = Environment(
env_params = dict(
loader=template_loader,
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
extensions=[
extension for extension in JINJA2_THIRD_PARTY_EXTENSIONS
], # get a copy of this global variable
)
if is_extension_list_valid(extensions):
# because it is modified here
env_params["extensions"] += extensions
self.jj2_environment = Environment(**env_params)
for filter_name, filter_function in FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function

Expand Down Expand Up @@ -120,3 +128,11 @@ def apply_template(self, template, data, output):

def load_jinja2_extensions():
scan_plugins_regex(JINJA2_LIBRARIES, "moban", None, JINJA2_EXENSIONS)


def is_extension_list_valid(extensions):
return (
extensions is not None
and isinstance(extensions, list)
and len(extensions) > 0
)
4 changes: 4 additions & 0 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):
if requires:
handle_requires(requires)

extensions = moban_file_configurations.get(constants.LABEL_EXTENSIONS)
if extensions:
plugins.ENGINES.register_extensions(extensions)

if targets:
# If template specified via CLI flag `-t:
# 1. Only update the specified template
Expand Down
15 changes: 12 additions & 3 deletions moban/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ def resource_path_of(self, library_name):


class BaseEngine(object):
def __init__(self, template_dirs, context_dirs, engine_cls):
def __init__(
self, template_dirs, context_dirs, engine_cls, engine_extensions=None
):
# pypi-moban-pkg cannot be found if removed
make_sure_all_pkg_are_loaded()
template_dirs = list(expand_template_directories(template_dirs))
verify_the_existence_of_directories(template_dirs)
context_dirs = expand_template_directory(context_dirs)
self.context = Context(context_dirs)
self.template_dirs = template_dirs
self.engine = engine_cls(self.template_dirs)
self.engine = engine_cls(self.template_dirs, engine_extensions)
self.engine_cls = engine_cls
self.templated_count = 0
self.file_count = 0
Expand Down Expand Up @@ -143,10 +145,17 @@ def __init__(self):
super(EngineFactory, self).__init__(
constants.TEMPLATE_ENGINE_EXTENSION
)
self.extensions = {}

def register_extensions(self, extensions):
self.extensions.update(extensions)

def get_engine(self, template_type, template_dirs, context_dirs):
engine_cls = self.load_me_now(template_type)
return BaseEngine(template_dirs, context_dirs, engine_cls)
engine_extensions = self.extensions.get(template_type)
return BaseEngine(
template_dirs, context_dirs, engine_cls, engine_extensions
)

def all_types(self):
return list(self.registry.keys())
Expand Down
1 change: 1 addition & 0 deletions rnd_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/moremoban/moban-handlebars/archive/dev.zip
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

NAME = 'moban'
AUTHOR = 'C. W.'
VERSION = '0.3.8'
VERSION = '0.3.9'
EMAIL = 'wangc_2011@hotmail.com'
LICENSE = 'MIT'
ENTRY_POINTS = {
Expand All @@ -25,7 +25,7 @@
'Yet another jinja2 cli command for static text generation'
)
URL = 'https://github.com/moremoban/moban'
DOWNLOAD_URL = '%s/archive/0.3.8.tar.gz' % URL
DOWNLOAD_URL = '%s/archive/0.3.9.tar.gz' % URL
FILES = ['README.rst', 'CONTRIBUTORS.rst', 'CHANGELOG.rst']
KEYWORDS = [
'python',
Expand Down Expand Up @@ -60,8 +60,8 @@
# You do not need to read beyond this line
PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format(
sys.executable)
GS_COMMAND = ('gs moban v0.3.8 ' +
"Find 0.3.8 in changelog for more details")
GS_COMMAND = ('gs moban v0.3.9 ' +
"Find 0.3.9 in changelog for more details")
NO_GS_MESSAGE = ('Automatic github release is disabled. ' +
'Please install gease to enable it.')
UPLOAD_FAILED_MSG = (
Expand Down
17 changes: 17 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ def test_level_11(self):
folder = "level-11-use-handlebars"
self._raw_moban(["moban"], folder, expected, "a.output")

def test_level_12_a(self):
expected_a = """world
world
world
world
"""
folder = "level-12-use-template-engine-extensions"
self._raw_moban(["moban"], folder, expected_a, "a.output")

def test_level_12_b(self):
expected_b = """142
42
142
"""
folder = "level-12-use-template-engine-extensions"
self._raw_moban(["moban"], folder, expected_b, "b.output")

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

Expand Down
Loading