Skip to content

Commit b9be0a5

Browse files
authored
Merge pull request #71 from moremoban/dev
release 0.2.2
2 parents fbf2854 + c126716 commit b9be0a5

23 files changed

+194
-40
lines changed

.moban.cd/changelog.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
name: moban
22
organisation: moremoban
33
releases:
4+
- changes:
5+
- action: Added
6+
details:
7+
- "`#31`: create directory if missing during copying"
8+
- action: Updated
9+
details:
10+
- "`#28`: if a template has been copied once before, it is skipped in the next moban call"
11+
date: unreleased
12+
version: 0.2.2
413
- changes:
514
- action: Updated
615
details:

.moban.cd/moban.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ organisation: moremoban
33
author: C. W.
44
55
license: MIT
6-
version: 0.2.1
7-
current_version: 0.2.1
8-
release: 0.2.1
6+
version: 0.2.2
7+
current_version: 0.2.2
8+
release: 0.2.2
99
branch: master
1010
command_line_interface: "moban"
1111
entry_point: "moban.main:main"
@@ -18,4 +18,4 @@ dependencies:
1818
- lml==0.0.3
1919
- crayons
2020
description: Yet another jinja2 cli command for static text generation
21-
scm_host: github.com
21+
scm_host: github.com

CHANGELOG.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
Change log
22
================================================================================
33

4+
0.2.2 - unreleased
5+
--------------------------------------------------------------------------------
6+
7+
Added
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
#. `#31 <https://github.com/moremoban/moban/issues/31>`_: create directory if
11+
missing during copying
12+
13+
Updated
14+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
#. `#28 <https://github.com/moremoban/moban/issues/28>`_: if a template has been
17+
copied once before, it is skipped in the next moban call
18+
419
0.2.1 - 13-06-2018
520
--------------------------------------------------------------------------------
621

docs/index.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ examples folder.
2323
level-6-complex-configuration/README.rst
2424
level-7-use-custom-jinja2-filter-test-n-global/README.rst
2525

26+
In pratice, the following use cases were found interesting to go along with.
27+
28+
.. toctree::
29+
:maxdepth: 1
30+
31+
misc-1-copying-templates
32+
33+
2634
For more complex use case, please look at `its usage in pyexcel project <http://pyexcel.readthedocs.io/en/latest/guide.html>`_
2735

2836
Developer Guide
@@ -40,4 +48,3 @@ Indices and tables
4048
* :ref:`genindex`
4149
* :ref:`modindex`
4250
* :ref:`search`
43-
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
configuration:
2+
template_dir:
3+
- template-sources
4+
copy:
5+
- simple.file.copy: file-in-template-sources-folder.txt
6+
- "misc-1-copying/can-create-folder/if-not-exists.txt": file-in-template-sources-folder.txt
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Misc 1: copying templates
2+
================================================================================
3+
4+
With `.moban.yml`, you can copy templates to your destination.
5+
6+
7+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test file

moban/copier.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
import os
22
import shutil
33

4+
import moban.utils as utils
45
import moban.reporter as reporter
6+
from moban.hashstore import HASH_STORE
57

68

79
class Copier(object):
810

911
def __init__(self, template_dirs):
1012
self.template_dirs = template_dirs
13+
self._file_count = 0
1114
self._count = 0
1215

1316
def copy_files(self, file_list):
1417
for dest_src_pair in file_list:
1518
for dest, src in dest_src_pair.items():
19+
self._file_count += 1
1620
src_path = self._get_src_file(src)
17-
if src_path:
18-
reporter.report_copying(src_path, dest)
19-
shutil.copy(src_path, dest)
20-
self._count = self._count + 1
21-
else:
21+
if src_path is None:
2222
reporter.report_error_message(
2323
"{0} cannot be found".format(src)
2424
)
25+
elif HASH_STORE.are_two_file_different(src_path, dest):
26+
dest_folder = os.path.dirname(dest)
27+
if dest_folder:
28+
utils.mkdir_p(dest_folder)
29+
reporter.report_copying(src_path, dest)
30+
shutil.copy(src_path, dest)
31+
self._count = self._count + 1
2532

2633
def number_of_copied_files(self):
2734
return self._count
2835

2936
def report(self):
3037
if self._count:
31-
reporter.report_copying_summary(self._count)
38+
reporter.report_copying_summary(self._file_count, self._count)
3239
else:
33-
reporter.report_no_action()
40+
reporter.report_no_copying()
3441

3542
def _get_src_file(self, src):
3643
for folder in self.template_dirs:

moban/engine.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from lml.plugin import PluginManager, PluginInfo
77
from lml.loader import scan_plugins
88

9-
from moban.hashstore import HashStore
9+
from moban.hashstore import HASH_STORE
1010
from moban.extensions import JinjaFilterManager, JinjaTestManager
1111
from moban.extensions import JinjaGlobalsManager
1212
import moban.utils as utils
@@ -74,7 +74,6 @@ def __init__(self, template_dirs, context_dirs):
7474

7575
self.context = Context(context_dirs)
7676
self.template_dirs = template_dirs
77-
self.hash_store = HashStore()
7877
self.__file_count = 0
7978
self.__templated_count = 0
8079

@@ -95,7 +94,6 @@ def render_to_files(self, array_of_param_tuple):
9594
self._render_with_finding_data_first(sta.data_file_index)
9695
else:
9796
self._render_with_finding_template_first(sta.template_file_index)
98-
self.hash_store.close()
9997

10098
def report(self):
10199
if self.__templated_count == 0:
@@ -136,7 +134,7 @@ def _apply_template(self, template, data, output):
136134
rendered_content = template.render(**data)
137135
rendered_content = utils.strip_off_trailing_new_lines(rendered_content)
138136
rendered_content = rendered_content.encode("utf-8")
139-
flag = self.hash_store.is_file_changed(
137+
flag = HASH_STORE.is_file_changed(
140138
output, rendered_content, template.filename
141139
)
142140
if flag:

moban/hashstore.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ def __init__(self):
2121
else:
2222
self.hashes = {}
2323

24+
def are_two_file_different(self, source_file, dest_file):
25+
different = True
26+
source_hash = get_file_hash(source_file)
27+
28+
previous_source_hash = self.hashes.get("copy:" + source_file)
29+
if previous_source_hash is None:
30+
self.hashes["copy:" + source_file] = source_hash
31+
32+
if source_hash == previous_source_hash:
33+
different = False
34+
35+
if not different:
36+
if os.path.exists(dest_file):
37+
dest_hash = get_file_hash(dest_file)
38+
if source_hash == dest_hash:
39+
different = False
40+
else:
41+
different = True
42+
else:
43+
different = True
44+
45+
return different
46+
2447
def is_file_changed(self, file_name, file_content, source_template):
2548
changed = self._is_source_updated(
2649
file_name, file_content, source_template
@@ -49,11 +72,14 @@ def _is_source_updated(self, file_name, file_content, source_template):
4972

5073
return changed
5174

52-
def close(self):
75+
def save_hashes(self):
5376
with open(self.cache_file, "w") as f:
5477
json.dump(self.hashes, f)
5578

5679

80+
HASH_STORE = HashStore()
81+
82+
5783
def get_file_hash(afile):
5884
with open(afile, "rb") as handle:
5985
content = handle.read()

0 commit comments

Comments
 (0)