Skip to content

Commit

Permalink
✨ support group targets with template type, #165. And it is related to
Browse files Browse the repository at this point in the history
…#62 with current restriction on template type as key. What will happen if template type as group key is unknown, atm it will be treated as jinja2 template type
  • Loading branch information
chfw committed Feb 14, 2019
1 parent 2aff81a commit 6f0a3ea
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 23 deletions.
9 changes: 9 additions & 0 deletions docs/level-16-group-targets-using-template-type/.moban.yml
@@ -0,0 +1,9 @@
configuration:
template_dir:
- template-sources
targets:
- copy:
- simple.file.copy: file-in-template-sources-folder.txt
- "misc-1-copying/can-create-folder/if-not-exists.txt": file-in-template-sources-folder.txt
- "test-dir": dir-for-copying
- "test-recursive-dir": dir-for-recusive-copying/**
26 changes: 26 additions & 0 deletions docs/level-16-group-targets-using-template-type/README.rst
@@ -0,0 +1,26 @@
Level 16: group targets by their template type
================================================================================

Since moban version 0.4.0, you can group your targets with their template type.
For example, with `copy` target, you can do the following things:


Here is example moban file for copying::
configuration:
template_dir:
- template-sources
targets:
- copy:
- simple.file.copy: file-in-template-sources-folder.txt
- "misc-1-copying/can-create-folder/if-not-exists.txt": file-in-template-sources-folder.txt
- "test-dir": dir-for-copying
- "test-recursive-dir": dir-for-recusive-copying/**

More information is documented in `misc-1-copying-template`.


template copy does:

#. copies any template inside pre-declared template directory to anywhere. moban will create directory if needed.
#. copies any directory to anywhere. If "**" is followed, moban attempts to do recursive copying.
@@ -0,0 +1 @@
test file
@@ -0,0 +1 @@
dir for copying
@@ -0,0 +1 @@
Please look at .moban.yml
@@ -0,0 +1 @@
everything is copied
@@ -0,0 +1 @@
dest_directory: source_directory/**
@@ -0,0 +1 @@
test file
4 changes: 2 additions & 2 deletions moban/copy/__init__.py
Expand Up @@ -7,8 +7,7 @@


@PluginInfo(
constants.TEMPLATE_ENGINE_EXTENSION,
tags=[constants.TEMPLATE_COPY]
constants.TEMPLATE_ENGINE_EXTENSION, tags=[constants.TEMPLATE_COPY]
)
class ContentForwardEngine(object):
"""
Expand All @@ -23,6 +22,7 @@ class ContentForwardEngine(object):
the source file. Everything else is taken care of by moban
templating mechanism.
"""

def __init__(self, template_dirs, extensions=None):
self.template_dirs = template_dirs

Expand Down
51 changes: 40 additions & 11 deletions moban/utils.py
Expand Up @@ -71,17 +71,46 @@ def parse_targets(options, targets):
)
else:
for output, template_file in target.items():
for src, dest, t_type in handle_template(
template_file, output, options[constants.LABEL_TMPL_DIRS]
):
if t_type:
yield TemplateTarget(
src, common_data_file, dest, t_type
)
else:
yield TemplateTarget(
src, common_data_file, dest, default_template_type
)
if isinstance(template_file, str) is False:
# grouping by template type feature
group_template_type = output
for _output, _template_file in iterate_list_of_dicts(
template_file
):
for src, dest, t_type in handle_template(
_template_file,
_output,
options[constants.LABEL_TMPL_DIRS],
):
yield TemplateTarget(
src,
common_data_file,
dest,
group_template_type,
)
else:
for src, dest, t_type in handle_template(
template_file,
output,
options[constants.LABEL_TMPL_DIRS],
):
if t_type:
yield TemplateTarget(
src, common_data_file, dest, t_type
)
else:
yield TemplateTarget(
src,
common_data_file,
dest,
default_template_type,
)


def iterate_list_of_dicts(list_of_dict):
for adict in list_of_dict:
for key, value in adict.items():
yield (key, value)


def get_template_type(template_file):
Expand Down
13 changes: 6 additions & 7 deletions tests/test_copy_engine.py
Expand Up @@ -6,21 +6,20 @@


class TestContentForwardEngine:

def setUp(self):
template_path = os.path.join('tests', 'fixtures')
template_path = os.path.join("tests", "fixtures")
self.engine = ContentForwardEngine([template_path])

def test_get_template(self):
template_content = self.engine.get_template('copier-test01.csv')
eq_('test 01\n', template_content)
template_content = self.engine.get_template("copier-test01.csv")
eq_("test 01\n", template_content)

def test_get_template_from_string(self):
test_content = 'simply forwarded'
test_content = "simply forwarded"
template_content = self.engine.get_template_from_string(test_content)
eq_(test_content, template_content)

def test_apply_template(self):
test_content = 'simply forwarded'
template_content = self.engine.apply_template(test_content, 'not used')
test_content = "simply forwarded"
template_content = self.engine.apply_template(test_content, "not used")
eq_(test_content, template_content)
6 changes: 3 additions & 3 deletions tests/test_definitions.py
Expand Up @@ -5,12 +5,12 @@

def test_git_require_repr():
require = GitRequire(git_url="http://github.com/some/repo")
eq_('http://github.com/some/repo,None,False', repr(require))
eq_("http://github.com/some/repo,None,False", repr(require))


def test_template_target_repr():
require = TemplateTarget('template_file', 'dat_file', 'output')
eq_('template_file,dat_file,output,jinja2', repr(require))
require = TemplateTarget("template_file", "dat_file", "output")
eq_("template_file,dat_file,output,jinja2", repr(require))


def test_clone_params():
Expand Down
6 changes: 6 additions & 0 deletions tests/test_docs.py
Expand Up @@ -210,6 +210,12 @@ def test_level_15_copy_templates_as_target(self):
folder = "level-15-copy-templates-as-target"
self._raw_moban(["moban"], folder, expected, "simple.file")

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

folder = "level-16-group-targets-using-template-type"
self._raw_moban(["moban"], folder, expected, "simple.file")

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

Expand Down

0 comments on commit 6f0a3ea

Please sign in to comment.