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
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: Updated
details:
- "`#154`: introduce first ever positional argument for string base template."
date: 11-1-2019
version: 0.3.8
- changes:
- action: Updated
details:
Expand Down
6 changes: 3 additions & 3 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.3.7
current_version: 0.3.7
release: 0.3.7
version: 0.3.8
current_version: 0.3.8
release: 0.3.8
branch: master
command_line_interface: "moban"
entry_point: "moban.main:main"
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.3.8 - 11-1-2019
--------------------------------------------------------------------------------

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

#. `#154 <https://github.com/moremoban/moban/issues/154>`_: introduce first ever
positional argument for string base template.

0.3.7 - 6-1-2019
--------------------------------------------------------------------------------

Expand Down
29 changes: 26 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,24 @@ or clone it and install it:
Quick start
================================================================================

Here is a simple example:
.. code-block:: bash

$ export HELLO="world"
$ moban "{{HELLO}}"
Warning: Both data.yml and /.../.moban.cd/data.yml does not exist
Warning: Attempting to use environment vars as data...
Templating {{HELLO}}... to moban.output
Templated 1 file.
$ cat moban.output
world

Or simply
.. code-block:: bash

$ HELLO="world" moban "{{HELLO}}"


A bit formal example:

.. code-block:: bash

Expand All @@ -69,6 +86,8 @@ moban.output will contain::

world

Please note that data.yml will take precedence over environment variables.

`the tutorial`_ has more use cases.

.. _the tutorial: http://moban.readthedocs.org/en/latest/#tutorial
Expand All @@ -77,15 +96,19 @@ moban.output will contain::
Usage
================================================================================

::
code-block:: bash


usage: moban [-h] [-cd CONFIGURATION_DIR] [-c CONFIGURATION]
[-td [TEMPLATE_DIR [TEMPLATE_DIR ...]]] [-t TEMPLATE] [-o OUTPUT]
[-f] [-m MOBANFILE]

[template]

Yet another jinja2 cli command for static text generation

positional arguments:
template string templates

optional arguments:
-h, --help show this help message and exit
-cd CONFIGURATION_DIR, --configuration_dir CONFIGURATION_DIR
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
author = u'C. W.'

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


# -- General configuration ---------------------------------------------------
Expand Down
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.7"
__version__ = "0.3.8"
__author__ = "C. W."
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LABEL_CONFIG_DIR = "configuration_dir"
LABEL_PLUGIN_DIRS = "plugin_dir"
LABEL_TEMPLATE = "template"
POSITIONAL_LABEL_TEMPLATE = "template_in_string"
LABEL_TMPL_DIRS = "template_dir"
LABEL_OUTPUT = "output"
LABEL_TEMPLATE_TYPE = "template_type"
Expand Down
13 changes: 10 additions & 3 deletions moban/jinja2/engine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from jinja2 import Environment, FileSystemLoader
from jinja2 import Template, Environment, FileSystemLoader
from lml.loader import scan_plugins_regex
from lml.plugin import PluginInfo, PluginManager
from jinja2.exceptions import TemplateNotFound

import moban.constants as constants
from moban import constants, exceptions

JINJA2_LIBRARIES = "^moban_jinja2_.+$"
JINJA2_EXENSIONS = [
Expand Down Expand Up @@ -91,9 +92,15 @@ def get_template(self, template_file):
template file exists at:
'/User/moban-pro/my-template/templates/myfile.jj2'
"""
template = self.jj2_environment.get_template(template_file)
try:
template = self.jj2_environment.get_template(template_file)
except TemplateNotFound:
raise exceptions.FileNotFound("%s does not exist" % template_file)
return template

def get_template_from_string(self, string):
return Template(string)

def apply_template(self, template, data, output):
"""
It is not expected this function to write content to file system.
Expand Down
29 changes: 22 additions & 7 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def create_parser():
parser.add_argument(
"-m", "--%s" % constants.LABEL_MOBANFILE, help="custom moban file"
)
parser.add_argument(
constants.POSITIONAL_LABEL_TEMPLATE,
metavar="template",
type=str,
nargs="?",
help="string templates",
)
return parser


Expand Down Expand Up @@ -153,18 +160,26 @@ def handle_command_line(options):
act upon command options
"""
options = merge(options, constants.DEFAULT_OPTIONS)
if options[constants.LABEL_TEMPLATE] is None:
raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
engine = plugins.ENGINES.get_engine(
options[constants.LABEL_TEMPLATE_TYPE],
options[constants.LABEL_TMPL_DIRS],
options[constants.LABEL_CONFIG_DIR],
)
engine.render_to_file(
options[constants.LABEL_TEMPLATE],
options[constants.LABEL_CONFIG],
options[constants.LABEL_OUTPUT],
)
if options[constants.LABEL_TEMPLATE] is None:
if options[constants.POSITIONAL_LABEL_TEMPLATE] is None:
raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
else:
engine.render_string_to_file(
options[constants.POSITIONAL_LABEL_TEMPLATE],
options[constants.LABEL_CONFIG],
options[constants.LABEL_OUTPUT],
)
else:
engine.render_to_file(
options[constants.LABEL_TEMPLATE],
options[constants.LABEL_CONFIG],
options[constants.LABEL_OUTPUT],
)
engine.report()
HASH_STORE.save_hashes()
exit_code = reporter.convert_to_shell_exit_code(
Expand Down
33 changes: 27 additions & 6 deletions moban/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,49 @@ def render_to_file(self, template_file, data_file, output_file):
template_abs_path = utils.get_template_path(
self.template_dirs, template_file
)

flag = self.apply_template(
template_abs_path, template, data, output_file
)
if flag:
reporter.report_templating(template_file, output_file)
self.templated_count += 1

def render_string_to_file(
self, template_in_string, data_file, output_file
):
self.file_count = 1
template = self.engine.get_template_from_string(template_in_string)
template_abs_path = template_in_string[:10] + "..."
data = self.context.get_data(data_file)
flag = self.apply_template(
template_abs_path, template, data, output_file
)
if flag:
reporter.report_templating(template_abs_path, output_file)
self.templated_count += 1

def apply_template(self, template_abs_path, template, data, output_file):
rendered_content = self.engine.apply_template(
template, data, output_file
)
rendered_content = utils.strip_off_trailing_new_lines(rendered_content)
rendered_content = rendered_content.encode("utf-8")
flag = HASH_STORE.is_file_changed(
output_file, rendered_content, template_abs_path
)
if flag:
try:
flag = HASH_STORE.is_file_changed(
output_file, rendered_content, template_abs_path
)
if flag:
utils.write_file_out(
output_file, rendered_content, strip=False, encode=False
)
utils.file_permissions_copy(template_abs_path, output_file)
return flag
except exceptions.FileNotFound:
utils.write_file_out(
output_file, rendered_content, strip=False, encode=False
)
utils.file_permissions_copy(template_abs_path, output_file)
return flag
return True

def render_to_files(self, array_of_param_tuple):
sta = Strategy(array_of_param_tuple)
Expand Down
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.7'
VERSION = '0.3.8'
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.7.tar.gz' % URL
DOWNLOAD_URL = '%s/archive/0.3.8.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.7 ' +
"Find 0.3.7 in changelog for more details")
GS_COMMAND = ('gs moban v0.3.8 ' +
"Find 0.3.8 in changelog for more details")
NO_GS_MESSAGE = ('Automatic github release is disabled. ' +
'Please install gease to enable it.')
UPLOAD_FAILED_MSG = (
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_tests/test_command_line_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def test_default_options(self, fake_template_doer):
"a.jj2", "data.yml", "moban.output"
)

@patch("moban.plugins.BaseEngine.render_string_to_file")
def test_string_template(self, fake_template_doer):
string_template = "{{HELLO}}"
test_args = ["moban", string_template]
with patch.object(sys, "argv", test_args):
from moban.main import main

main()
fake_template_doer.assert_called_with(
string_template, "data.yml", "moban.output"
)

@raises(SystemExit)
def test_no_argments(self):
test_args = ["moban"]
Expand Down
13 changes: 12 additions & 1 deletion tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, template_dirs):
pass


@patch('moban.plugins.PluginManager.load_me_now', return_value=FakeEngine)
@patch("moban.plugins.PluginManager.load_me_now", return_value=FakeEngine)
def test_default_mako_type(_): # fake mako
engine = ENGINES.get_engine("fake", [], "")
assert engine.engine_cls.__name__ == "FakeEngine"
Expand Down Expand Up @@ -119,3 +119,14 @@ def test_environ_variables_as_data():
content = output_file.read()
eq_(content, "foo")
os.unlink(output)


def test_string_template():
output = "test.txt"
path = os.path.join("tests", "fixtures")
engine = BaseEngine([path], path, Engine)
engine.render_string_to_file("{{simple}}", "simple.yaml", output)
with open(output, "r") as output_file:
content = output_file.read()
eq_(content, "yaml")
os.unlink(output)
12 changes: 11 additions & 1 deletion tests/test_jinja2_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
from moban.jinja2.engine import Engine


def test_handlebars_template_not_found():
def test_jinja2_template():
path = os.path.join("tests", "fixtures", "jinja_tests")
engine = Engine([path])
template = engine.get_template("file_tests.template")
data = dict(test="here")
result = engine.apply_template(template, data, None)
expected = "yes\nhere"
eq_(expected, result)


def test_jinja2_template_string():
path = os.path.join("tests", "fixtures", "jinja_tests")
engine = Engine([path])
template = engine.get_template_from_string("{{test}}")
data = dict(test="here")
result = engine.apply_template(template, data, None)
expected = "here"
eq_(expected, result)