Skip to content

Commit

Permalink
✨ moban file inheritance (#346)
Browse files Browse the repository at this point in the history
* ✨ allow .moban.yaml to inherit(overrides operator) any other yaml files

* 🔬 unit test the new feature

* 📚 🔬 update use case documentation and provide regression test suite

* 👕 make yamllint happy

* 🔥 remove debug print

* 📰 add missing moban file
  • Loading branch information
chfw authored Nov 9, 2019
1 parent 3b19f86 commit 0356213
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 5 deletions.
8 changes: 7 additions & 1 deletion .moban.cd/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ name: moban
organisation: moremoban
releases:
- changes:
- action: Updated
- action: Added
details:
- "support moban file inheritance. one base moban file and child repos can inherit and override"
date: tbd
version: 0.6.6
- changes:
- action: Added
details:
- "`#335`: support intermediate targets in moban file"
date: 13.10.2019
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
Change log
================================================================================

0.6.6 - tbd
--------------------------------------------------------------------------------

**Added**

#. support moban file inheritance. one base moban file and child repos can
inherit and override

0.6.5 - 13.10.2019
--------------------------------------------------------------------------------

**Updated**
**Added**

#. `#335 <https://github.com/moremoban/moban/issues/335>`_: support intermediate
targets in moban file
Expand Down
3 changes: 3 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This section covers the use cases for moban. Please check them out individually.
#. `Template files in a zip or tar`_
#. `Template copying from a zip to a zip`_
#. `Intermeidate targets`_
#. `Mobanfile inheritance`_

.. _Jinja2 command line: level-1-jinja2-cli
.. _Template inheritance: level-2-template-inheritance
Expand All @@ -48,3 +49,5 @@ This section covers the use cases for moban. Please check them out individually.
.. _Template files in a zip or tar: level-20-templates-configs-in-zip-or-tar
.. _Template copying from a zip to a zip: level-21-copy-templates-into-an-alien-file-system
.. _Intermeidate targets: level-22-intermediate-targets
.. _Mobanfile inheritance: level-23-inherit-organisational-moban-file

2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ examples folder.
level-20-templates-configs-in-zip-or-tar/README.rst
level-21-copy-templates-into-an-alien-file-system/README.rst
level-22-intermediate-targets/README.rst
level-23-inherit-organisational-moban-file/README.rst


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

Expand Down
3 changes: 3 additions & 0 deletions docs/level-23-inherit-organisational-moban-file/.moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
overrides: parent.moban.yaml
targets:
- output_a: template_a.jj2
10 changes: 10 additions & 0 deletions docs/level-23-inherit-organisational-moban-file/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Level 23: moban file inheritance
================================================================================

It is a bit tedious to repeat a few common configuration in moban file. Why not
create a parent moban file? Then allow child project to deviate from.

The answer is to use 'overrides' in `.moban.yaml`, so called moban file.

`overrides` could over ride any data file format in any location in theory. And
it support override a specific key set.
2 changes: 2 additions & 0 deletions docs/level-23-inherit-organisational-moban-file/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
template_a: I am template a
template_b: I am template b
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configuration:
configuration: data.yaml
targets:
- output_b: template_b.jj2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{template_a}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{template_b}}
9 changes: 8 additions & 1 deletion moban/core/data_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

from lml.plugin import PluginManager
from ruamel.yaml.comments import CommentedSeq

from moban import constants
from moban.externals import file_system
Expand Down Expand Up @@ -31,7 +32,7 @@ def load_data(base_dir, file_name):
data = LOADER.get_data(abs_file_path)
if data is not None:
parent_data = OrderedDict()
if base_dir and constants.LABEL_OVERRIDES in data:
if constants.LABEL_OVERRIDES in data:
overrides = data.pop(constants.LABEL_OVERRIDES)
if not isinstance(overrides, list):
overrides = [overrides]
Expand Down Expand Up @@ -69,6 +70,12 @@ def merge(left, right):
left[key] = value
else:
left[key] = merge(left[key], value)
else:
both_list_alike = (
isinstance(left, CommentedSeq) and isinstance(right, CommentedSeq)
) or (isinstance(left, list) and isinstance(right, list))
if both_list_alike:
left.extend(right)
return left


Expand Down
1 change: 0 additions & 1 deletion moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def handle_moban_file(moban_file, options):
act upon default moban file
"""
moban_file_configurations = data_loader.load_data(None, moban_file)
print(moban_file_configurations)
if moban_file_configurations is None:
raise exceptions.MobanfileGrammarException(
constants.ERROR_INVALID_MOBAN_FILE % moban_file
Expand Down
32 changes: 31 additions & 1 deletion tests/data_loaders/test_merge_dict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from moban.data_loaders.manager import merge
from nose.tools import eq_
from ruamel.yaml import YAML

from moban.core.data_loader import merge


def test_simple_union():
Expand Down Expand Up @@ -34,3 +37,30 @@ def test_three_level_conflict():
default = {"L1": {"L2": {"L3": "Hi"}}}
merged = merge(user, default)
assert merged == {"L1": {"L2": {"L3": "World"}}}


def test_merge_value_as_list():
user = {"L1": ["a", "b"]}
default = {"L1": ["c", "d"]}
merged = merge(user, default)
assert merged == {"L1": ["a", "b", "c", "d"]}


def test_merge_value_as_list_in_yaml():
yaml = YAML(typ="rt")
user = yaml.load(
"""
L1:
- a
- b
"""
)
default = yaml.load(
"""
L1:
- c
- d
"""
)
merged = merge(user, default)
eq_(merged, {"L1": ["a", "b", "c", "d"]})
8 changes: 8 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ def test_level_22_intermediate_targets(self):
folder = "level-22-intermediate-targets"
self.run_moban(["moban"], folder, [("final", expected)])

def test_level_23_inherit_parent_moban_file(self):
folder = "level-23-inherit-organisational-moban-file"
self.run_moban(
["moban"],
folder,
[("output_a", "I am template a"), ("output_b", "I am template b")],
)

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

Expand Down

0 comments on commit 0356213

Please sign in to comment.