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

Moban extension #61

Merged
merged 23 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
98f546e
:sparkles: make internal jinja2 filters as a pluginable filter. relat…
chfw May 30, 2018
1debcf3
:sparkles: take jinja filters from plugin_dir. #23
chfw May 30, 2018
9c4e3bc
:sparkles: support for jinja2 tests and globals. verification require…
chfw May 30, 2018
77e4131
:fire: remove tests because the custom filters have been relocated to…
chfw May 30, 2018
9a671ab
:sparkles: provide repr filter, fix #26
chfw May 30, 2018
ffe2343
:bug: fix format call for python 2.6
chfw Jun 1, 2018
6843e81
:sparkles: deliver exists test and more. fix #18. related to #23
chfw Jun 1, 2018
0fd443c
bring back the custom filters so as to avoid big impact of existing m…
chfw Jun 4, 2018
df8a0c9
:bug: minor fixes
chfw Jun 4, 2018
c4ea295
:bug: make plugin managers global in engine file so that each instanc…
chfw Jun 6, 2018
42488eb
:sparkles: verfied jinja globals plugin
chfw Jun 6, 2018
6e02d05
:newspaper: add test fixtures
chfw Jun 6, 2018
26772c4
:green_heart: make unit test pass. note: lml does not really support …
chfw Jun 6, 2018
79dd753
:books: document custom jinja extensions
chfw Jun 6, 2018
35f0ad3
:art: treat plugin directory as a python module so as to avoid duplic…
chfw Jun 8, 2018
dbdd8e2
:fire: remove debug log
chfw Jun 8, 2018
ed44151
:books: document jinja plugin feature
chfw Jun 8, 2018
277c0b6
:shirt: pump up the version number
chfw Jun 8, 2018
bd8467b
:handshake: Merge branch 'master' into moban-extension
chfw Jun 8, 2018
f12440f
:fire: sync with latest pypi-mobans
chfw Jun 8, 2018
0889731
:bug: fix filter for python 3
chfw Jun 8, 2018
16d2283
:books: update change log
chfw Jun 8, 2018
4462f8f
:shirt: whitening the code
chfw Jun 8, 2018
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: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# moban hashes
.moban.hashes


# Extra rules from https://github.com/github/gitignore/
# Python rules
# Byte-compiled / optimized / DLL files
Expand Down
8 changes: 8 additions & 0 deletions .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: moban
organisation: moremoban
releases:
- changes:
- action: Updated
details:
- '`#18`: file exists test'
- "`#23`: custom jinja plugins"
- '`#26`: repr filter'
date: unreleased
version: 0.2.0
- changes:
- action: Updated
details:
Expand Down
5 changes: 3 additions & 2 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ organisation: moremoban
author: C. W.
contact: [email protected]
license: MIT
version: 0.1.4
current_version: 0.1.4
version: 0.2.0
current_version: 0.2.0
release: 0.1.4
branch: master
command_line_interface: "moban"
Expand All @@ -15,6 +15,7 @@ keywords:
dependencies:
- pyyaml>=3.11
- jinja2>=2.7.1
- lml==0.0.2
- crayons
description: Yet another jinja2 cli command for static text generation
scm_host: github.com
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Change log
================================================================================

0.2.0 - unreleased
--------------------------------------------------------------------------------

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

#. `#18 <https://github.com/moremoban/moban/issues/18>`_: file exists test
#. `#23 <https://github.com/moremoban/moban/issues/23>`_: custom jinja plugins
#. `#26 <https://github.com/moremoban/moban/issues/26>`_: repr filter

0.1.4 - 29-May-2018
--------------------------------------------------------------------------------

Expand Down
32 changes: 32 additions & 0 deletions docs/extension.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Development guide
=======================

Jinja2 extensions for Moban
------------------------------

Since version 0.2, mobanfile supports an extra field `plugin_dir`, along with
`template_dir`. When you put your own jinja2 filters, tests and globals in
your moban repo, you can let moban know about them via this keyword.

Importantly, you have to have `__init__.py` file in your `plugin_dir`. Otherwise,
your plugins will NOT be loaded.

Jinja2 Filter
*******************

.. literalinclude:: ../../moban/filters/repr.py


Jinja2 Test
*******************

.. literalinclude:: ../../moban/tests/files.py

Jinja2 Globals
*******************

.. literalinclude:: ../../tests/test_engine.py
:lines: 49-61

It is possible to write an installable package including your own jinja2
filters, tests and globals. Please email me for more details.
8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ examples folder.
level-4-single-command/README.rst
level-5-custom-configuration/README.rst
level-6-complex-configuration/README.rst
level-7-use-custom-jinja2-filter-test-n-global/README.rst

For more complex use case, please look at `its usage in pyexcel project <http://pyexcel.readthedocs.io/en/latest/guide.html>`_

Developer Guide
================================================================================

.. toctree::

extension

.. include:: ../CHANGELOG.rst

Indices and tables
Expand Down
10 changes: 10 additions & 0 deletions docs/level-7-use-custom-jinja2-filter-test-n-global/.moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
configuration:
template_dir:
- my-templates
plugin_dir:
- custom-jj2-plugin
configuration: data.yml
targets:
- filter.output: filter.jj2
- test.output: test.jj2
- global.output: global.jj2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eol at eof

45 changes: 45 additions & 0 deletions docs/level-7-use-custom-jinja2-filter-test-n-global/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Level 7: Custom jinja filters, tests and globals
================================================================================

Level 7 example demonstrates advanced plugin capabilities of moban. The following
moban file had `plugin_dir` specified::


configuration:
template_dir:
- my-templates
plugin_dir:
- custom-jj2-plugin
configuration: data.yml
targets:
- filter.output: filter.jj2
- test.output: test.jj2

Where `custom-jj2-plugin` is a directory holding all jinja2 filters, tests
and globals. Under it, there are 4 files::

__init__.py filter.py test.py global.py

It is very important to have `__init__.py`, otherwise, it will NOT work. Other three
files are named to show case the feature. You can choose whichever name you prefer,
as long as you and your team could make sense of the names.


Evaluation
--------------------------------------------------------------------------------

Please go to `docs/level-7-use-custom-jinja2-filter-test-n-global` directory,

Here is the command to launch it:

.. code-block:: bash

$ moban
Templating filter.jj2 to filter.output
Templating test.jj2 to test.output
Templating global.jj2 to global.output
Templated 3 files.
Everything is up to date!

Please examine individual template and its associated plugin for more details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary trailing blank line?

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
import base64
from moban.extensions import JinjaFilter


@JinjaFilter('base64encode')
def base64_encode(string):
if sys.version_info[0] > 2:
content = base64.b64encode(string.encode('utf-8'))
content = content.decode('utf-8')
else:
content = base64.b64encode(string)
return content
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from moban.extensions import jinja_global


jinja_global('global', dict(hello='world'))
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from moban.extensions import JinjaTest


@JinjaTest('level7')
def test_level7(value):
return value == 'level7'
2 changes: 2 additions & 0 deletions docs/level-7-use-custom-jinja2-filter-test-n-global/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
level: level7
level8: level8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YWJj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'abc' | base64encode }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ global.hello }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% if level is level7%}
Hello, you are in level 7 example
{% else %}
Hello, you are not in {{level}}
{% endif %}

{% if level8 is level7%}
Hello, you are in level 7 example
{% else %}
Hello, you are not in level 7
{% endif %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eol at eof

7 changes: 7 additions & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Command line options
LABEL_CONFIG = "configuration"
LABEL_CONFIG_DIR = "configuration_dir"
LABEL_PLUGIN_DIRS = "plugin_dir"
LABEL_TEMPLATE = "template"
LABEL_TMPL_DIRS = "template_dir"
LABEL_OUTPUT = "output"
Expand Down Expand Up @@ -68,3 +69,9 @@
HAS_CHANGES = 1
ERROR = 2
NO_CHANGES = 0


# Extension
JINJA_FILTER_EXTENSION = "jinja_filter"
JINJA_TEST_EXTENSION = "jinja_test"
JINJA_GLOBALS_EXTENSION = "jinja_globals"
4 changes: 3 additions & 1 deletion moban/copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


class Copier(object):

def __init__(self, template_dirs):
self.template_dirs = template_dirs
self._count = 0
Expand All @@ -19,7 +20,8 @@ def copy_files(self, file_list):
self._count = self._count + 1
else:
reporter.report_error_message(
"{0} cannot be found".format(src))
"{0} cannot be found".format(src)
)

def number_of_copied_files(self):
return self._count
Expand Down
41 changes: 36 additions & 5 deletions moban/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,30 @@
from collections import defaultdict
from jinja2 import Environment, FileSystemLoader

from lml.loader import scan_plugins

from moban.hashstore import HashStore
from moban.filters.text import split_length
from moban.filters.github import github_expand
from moban.extensions import JinjaFilterManager, JinjaTestManager
from moban.extensions import JinjaGlobalsManager
import moban.utils as utils
import moban.constants as constants
import moban.exceptions as exceptions
import moban.reporter as reporter


BUILTIN_EXENSIONS = [
"moban.filters.repr",
"moban.filters.github",
"moban.filters.text",
"moban.tests.files",
]

_FILTERS = JinjaFilterManager()
_TESTS = JinjaTestManager()
_GLOBALS = JinjaGlobalsManager()
scan_plugins("moban_", "moban", None, BUILTIN_EXENSIONS)


class EngineFactory(object):

@staticmethod
Expand Down Expand Up @@ -39,9 +54,17 @@ def __init__(self, template_dirs, context_dirs):
trim_blocks=True,
lstrip_blocks=True,
)
self.jj2_environment.filters["split_length"] = split_length
self.jj2_environment.filters["github_expand"] = github_expand
for filter_name, filter_function in _FILTERS.get_all():
self.jj2_environment.filters[filter_name] = filter_function

for test_name, test_function in _TESTS.get_all():
self.jj2_environment.tests[test_name] = test_function

for global_name, helper_obj in _GLOBALS.get_all():
self.jj2_environment.globals[global_name] = helper_obj.payload

self.context = Context(context_dirs)
self.template_dirs = template_dirs
self.hash_store = HashStore()
self.__file_count = 0
self.__templated_count = 0
Expand All @@ -53,7 +76,7 @@ def render_to_file(self, template_file, data_file, output_file):

rendered_content = template.render(**data)
utils.write_file_out(output_file, rendered_content)
utils.file_permissions_copy(template_file, output_file)
self._file_permissions_copy(template_file, output_file)

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
Expand Down Expand Up @@ -114,6 +137,14 @@ def _apply_template(self, template, data, output):
utils.file_permissions_copy(template.filename, output)
return flag

def _file_permissions_copy(self, template_file, output_file):
true_template_file = template_file
for a_template_dir in self.template_dirs:
true_template_file = os.path.join(a_template_dir, template_file)
if os.path.exists(true_template_file):
break
utils.file_permissions_copy(true_template_file, output_file)


class Context(object):

Expand Down
Loading