Skip to content

Commit

Permalink
Delete generated files by moban (#381)
Browse files Browse the repository at this point in the history
* 🔥 remove unwanted error message

* ✨ add delete support. resolves #167

* 📚 update change log

* 📰 add missing file
  • Loading branch information
chfw authored May 21, 2020
1 parent 10ba852 commit dec192f
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 61 deletions.
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: Added
details:
- "`#167`: reverse what moban have done: delete"
date: 21.5.2020
version: 0.7.5
- changes:
- action: Fixed
details:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Change log
================================================================================

0.7.5 - 21.5.2020
--------------------------------------------------------------------------------

**Added**

#. `#167 <https://github.com/moremoban/moban/issues/167>`_: reverse what moban
have done: delete

0.7.4 - 13.5.2020
--------------------------------------------------------------------------------

Expand Down
6 changes: 4 additions & 2 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ This section covers the use cases for moban. Please check them out individually.
#. `Select a group target to run`_
#. `Template files in a zip or tar`_
#. `Template copying from a zip to a zip`_
#. `Intermeidate targets`_
#. `Intermediate targets`_
#. `Mobanfile inheritance`_
#. `Files over http(s)`_
#. `Remove intermediate targets`_

.. _Jinja2 command line: level-1-jinja2-cli
.. _Template inheritance: level-2-template-inheritance
Expand All @@ -49,6 +50,7 @@ This section covers the use cases for moban. Please check them out individually.
.. _Select a group target to run: level-19-moban-a-sub-group-in-targets
.. _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
.. _Intermediate targets: level-22-intermediate-targets
.. _Mobanfile inheritance: level-23-inherit-organisational-moban-file
.. _Files over http(s): level-24-files-over-http
.. _Remove intermediate targets: level-25-delete-intermediate
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ examples folder.
level-22-intermediate-targets/README.rst
level-23-inherit-organisational-moban-file/README.rst
level-24-files-over-http/README.rst
level-25-delete-intermediate/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
11 changes: 11 additions & 0 deletions docs/level-25-delete-intermediate-targets/.moban.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
targets:
- intermediate.jj2: original.jj2
- intermediate2.jj2: original.jj2
- intermediate3.jj2: original.jj2
- final: intermediate.jj2
- output: what_ever_here_will_be_ignored
template: intermediate.jj2
template_type: delete
- output: ''
template: intermediate2.jj2
- delete!: intermediate3.jj2
42 changes: 42 additions & 0 deletions docs/level-25-delete-intermediate-targets/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Level 25: delete intermediate targets
================================================================================

Continue with level 22, we would like to delete intermediate files.

.. note::

What is intermediate targets? Simply they are the files moban generates
but in the end those files are not really used.


For safety reasons, we only delete intermediate targets. We are not allowing
moban to delete any files in template folders and staic folder.

Here is the short syntax::

targets:
- delete!: intermediate_file.jj2

Here are the full syntax::

targets:
- output: what_ever_here_will_be_ignored
template: intermediate.jj2
template_type: delete
- output: ''
template: intermediate2.jj2


Example mobanfile::

targets:
- intermediate.jj2: original.jj2
- intermediate2.jj2: original.jj2
- intermediate3.jj2: original.jj2
- output: x
template: intermediate.jj2
template_type: delete
- output: ''
template: intermediate2.jj2
- delete!: intermediate3.jj2
1 change: 1 addition & 0 deletions docs/level-25-delete-intermediate-targets/data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello: world
1 change: 1 addition & 0 deletions docs/level-25-delete-intermediate-targets/original.jj2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a {{hello}}
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Template type
TEMPLATE_JINJA2 = "jinja2"
TEMPLATE_COPY = "copy"
TEMPLATE_DELETE = "delete"

# Configurations
PROGRAM_NAME = "moban"
Expand Down
13 changes: 0 additions & 13 deletions moban/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,3 @@ def __repr__(self):
self.output,
self.template_type,
)


class Store:
def __init__(self):
self.init()

def add(self, target):
self.targets.append(target)
self.look_up_by_output[target.output] = target

def init(self):
self.targets = []
self.look_up_by_output = {}
27 changes: 17 additions & 10 deletions moban/core/moban_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,24 @@ def _render_with_finding_data_first(self, data_file_index):
data = self.context.get_data(data_file)
for (template_file, output) in template_output_pairs:
template = self.engine.get_template(template_file)
template_abs_path = self.template_fs.geturl(
template_file, purpose="fs"
)
flag = self.apply_template(
template_abs_path, template, data, output
)
if flag:
reporter.report_templating(
self.engine_action, template_file, output
if isinstance(template, bool):
if template:
reporter.report_templating(
self.engine_action, template_file, None
)
self.templated_count += 1
else:
template_abs_path = self.template_fs.geturl(
template_file, purpose="fs"
)
self.templated_count += 1
flag = self.apply_template(
template_abs_path, template, data, output
)
if flag:
reporter.report_templating(
self.engine_action, template_file, output
)
self.templated_count += 1
self.file_count += 1


Expand Down
14 changes: 13 additions & 1 deletion moban/core/mobanfile/store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
from moban.core.definitions import Store
class Store:
def __init__(self):
self.init()

def add(self, target):
self.targets.append(target)
self.look_up_by_output[target.output] = target

def init(self):
self.targets = []
self.look_up_by_output = {}
self.intermediate_targets = []


STORE = Store()
17 changes: 10 additions & 7 deletions moban/core/mobanfile/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ def parse_targets(options, targets):
STORE.add(template_target)
else:
for output, template_file in target.items():
if isinstance(template_file, str) is False:
if isinstance(template_file, str):
for template_target in _handle_implicit_target(
options, template_file, output
):
STORE.add(template_target)
else:
# grouping by template type feature
group_template_type = output
a_list_short_hand_targets = template_file
for template_target in _handle_group_target(
options, a_list_short_hand_targets, group_template_type
):
STORE.add(template_target)
else:
for template_target in _handle_implicit_target(
options, template_file, output
):
STORE.add(template_target)


def _handle_explicit_target(options, target):
Expand All @@ -78,7 +78,10 @@ def _handle_explicit_target(options, target):
)
data_file = target.get(constants.LABEL_CONFIG, common_data_file)
output = target[constants.LABEL_OUTPUT]
template_type = target.get(constants.LABEL_TEMPLATE_TYPE)
if output:
template_type = target.get(constants.LABEL_TEMPLATE_TYPE)
else:
template_type = constants.TEMPLATE_DELETE
if template_type and len(template_type) > 0:
if constants.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type:
reporter.report_file_extension_not_needed()
Expand Down
23 changes: 13 additions & 10 deletions moban/core/mobanfile/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ def handle_template(template_file, output, template_dirs):
fs, source_dir, output
)
else:
if STORE.look_up_by_output.get(template_file) is None:
reporter.report_error_message(
f"{template_file} cannot be found"
)
reporter.report_error_message(f"{template_file} cannot be found")
else:
_, fs = multi_fs.which(template_file)
if fs is None:
if STORE.look_up_by_output.get(template_file) is None:
if STORE.look_up_by_output.get(template_file) is None:
_, fs = multi_fs.which(template_file)
if fs is None:
reporter.report_error_message(
f"{template_file} cannot be found"
)
elif fs.isdir(template_file):
yield from _list_dir_files(fs, template_file, output)
else:
yield _create_a_single_target(template_file, output)
elif fs.isdir(template_file):
yield from _list_dir_files(fs, template_file, output)
else:
# when template_file is not found, it means
it_is_generated_by_moban = template_file
STORE.intermediate_targets.append(it_is_generated_by_moban)
yield _create_a_single_target(template_file, output)


Expand Down Expand Up @@ -66,7 +66,10 @@ def _listing_directory_files_recusively(fs, source, dest):


def _create_a_single_target(template_file, output):
template_type = _get_template_type(template_file)
if output == constants.TEMPLATE_DELETE + "!":
template_type = constants.TEMPLATE_DELETE
else:
template_type = _get_template_type(template_file)
# output.jj2: source.jj2 means 'copy'
if template_type and output.endswith("." + template_type):
LOG.info(
Expand Down
1 change: 1 addition & 0 deletions moban/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"moban.plugins.yaml_loader",
"moban.plugins.json_loader",
"moban.plugins.copy",
"moban.plugins.delete",
]


Expand Down
3 changes: 0 additions & 3 deletions moban/externals/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ def wrapper(*args, **kwds):
try:
return function_in_this_module(*args, **kwds)
except fs.errors.CreateFailed:
from moban.externals import reporter

message = "Failed to open %s" % args[0]
LOG.debug(message)
reporter.report_error_message(message)
raise exceptions.FileNotFound(args[0])
except fs.opener.errors.UnsupportedProtocol as e:
LOG.exception(e)
Expand Down
17 changes: 11 additions & 6 deletions moban/externals/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
def report_templating(
action_in_present_continuous_tense, source_file, destination_file
):
do_print(
MESSAGE_TEMPLATING.format(
action_in_present_continuous_tense,
crayons.yellow(source_file),
crayons.green(destination_file),
if destination_file:
do_print(
MESSAGE_TEMPLATING.format(
action_in_present_continuous_tense,
crayons.yellow(source_file),
crayons.green(destination_file),
)
)
else:
do_print(
f"{action_in_present_continuous_tense} {crayons.yellow(source_file)}"
)
)


def report_no_action():
Expand Down
36 changes: 36 additions & 0 deletions moban/plugins/delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs
from lml.plugin import PluginInfo

from moban import constants
from moban.core.mobanfile.store import STORE


@PluginInfo(constants.TEMPLATE_ENGINE_EXTENSION, tags=["delete"])
class ContentForwardEngine(object):
"""
Does no templating but delete generated intermediate targets
"""

ACTION_IN_PRESENT_CONTINUOUS_TENSE = "Deleting"
ACTION_IN_PAST_TENSE = "Deleted"

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

def get_template(self, template_file):
if template_file in STORE.intermediate_targets:
with fs.open_fs(".") as the_fs:
if the_fs.exists(template_file):
the_fs.remove(template_file)
return True
else:
return False
else:
raise Exception(f"Cannot remove {template_file}")

def get_template_from_string(self, string):
raise NotImplementedError("Not sure what to do")

def apply_template(self, template, *_):
raise NotImplementedError("Not sure what to do")
10 changes: 1 addition & 9 deletions tests/test_definitions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from nose.tools import eq_

from moban.deprecated import GitRequire
from moban.core.definitions import Store, TemplateTarget
from moban.core.definitions import TemplateTarget


def test_git_require_repr():
Expand Down Expand Up @@ -43,11 +43,3 @@ def test_branch_params():
actual = require.clone_params()
expected = {"single_branch": True, "branch": "ghpages", "depth": 2}
eq_(expected, actual)


def test_store():
store = Store()
output = "output"
target = TemplateTarget("template_file", "data_file", output)
store.add(target)
eq_(target, store.look_up_by_output.get(output))
10 changes: 10 additions & 0 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ def test_level_22_intermediate_targets(self):

folder = "level-22-intermediate-targets"
self.run_moban(["moban"], folder, [("final", expected)])
assert os.path.exists("intermediate.jj2")

def test_level_25_delete_intermediate_targets(self):
expected = "a world\n"

folder = "level-25-delete-intermediate-targets"
self.run_moban(["moban"], folder, [("final", expected)])
assert not os.path.exists("intermediate.jj2")
assert not os.path.exists("intermediate2.jj2")
assert not os.path.exists("intermediate3.jj2")

def test_level_23_inherit_parent_moban_file(self):
folder = "level-23-inherit-organisational-moban-file"
Expand Down

0 comments on commit dec192f

Please sign in to comment.