Skip to content

Commit 8f2c98d

Browse files
authored
Merge pull request #70 from moremoban/make-dir-while-copying
Make dir while copying
2 parents ee725fc + 7d0f67e commit 8f2c98d

File tree

11 files changed

+75
-6
lines changed

11 files changed

+75
-6
lines changed

.moban.cd/changelog.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name: moban
22
organisation: moremoban
33
releases:
44
- changes:
5+
- action: Added
6+
details:
7+
- "`#31`: create directory if missing during copying"
58
- action: Updated
69
details:
710
- "`#28`: if a template has been copied once before, it is skipped in the next moban call"

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Change log
44
0.2.2 - unreleased
55
--------------------------------------------------------------------------------
66

7+
Added
8+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
10+
#. `#31 <https://github.com/moremoban/moban/issues/31>`_: create directory if
11+
missing during copying
12+
713
Updated
814
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
915

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import shutil
33

4+
import moban.utils as utils
45
import moban.reporter as reporter
56
from moban.hashstore import HASH_STORE
67

@@ -9,17 +10,22 @@ class Copier(object):
910

1011
def __init__(self, template_dirs):
1112
self.template_dirs = template_dirs
13+
self._file_count = 0
1214
self._count = 0
1315

1416
def copy_files(self, file_list):
1517
for dest_src_pair in file_list:
1618
for dest, src in dest_src_pair.items():
19+
self._file_count += 1
1720
src_path = self._get_src_file(src)
1821
if src_path is None:
1922
reporter.report_error_message(
2023
"{0} cannot be found".format(src)
2124
)
2225
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)
2329
reporter.report_copying(src_path, dest)
2430
shutil.copy(src_path, dest)
2531
self._count = self._count + 1
@@ -29,7 +35,7 @@ def number_of_copied_files(self):
2935

3036
def report(self):
3137
if self._count:
32-
reporter.report_copying_summary(self._count)
38+
reporter.report_copying_summary(self._file_count, self._count)
3339
else:
3440
reporter.report_no_copying()
3541

moban/reporter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
MESSAGE_NO_TEMPLATING = "No templating"
1010
MESSAGE_REPORT = "Templated {0} out of {1} files."
1111
MESSAGE_TEMPLATED_ALL = "Templated {0} files."
12+
MESSAGE_COPY_REPORT = "Copied {0} out of {1} files."
1213
MESSAGE_COPIED_ALL = "Copied {0} files."
1314

1415

@@ -69,10 +70,16 @@ def report_no_copying_done():
6970
print(crayons.red(MESSAGE_NO_COPY, bold=True))
7071

7172

72-
def report_copying_summary(file_count):
73-
figure = crayons.green(str(file_count), bold=True)
74-
message = MESSAGE_COPIED_ALL.format(figure)
75-
print(_format_single(message, file_count))
73+
def report_copying_summary(total, copies):
74+
if total == copies:
75+
figure = crayons.green(str(total), bold=True)
76+
message = MESSAGE_COPIED_ALL.format(figure)
77+
print(_format_single(message, total))
78+
else:
79+
figure = crayons.green(str(copies), bold=True)
80+
total_figure = crayons.yellow(str(total), bold=True)
81+
message = MESSAGE_COPY_REPORT.format(figure, total_figure)
82+
print(_format_single(message, total))
7683

7784

7885
def _format_single(message, count):

moban/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import re
33
import stat
4+
import errno
45

56
import yaml
67

@@ -103,3 +104,13 @@ def write_file_out(filename, content, strip=True, encode=True):
103104
if encode:
104105
content = content.encode("utf-8")
105106
out.write(content)
107+
108+
109+
def mkdir_p(path):
110+
try:
111+
os.makedirs(path)
112+
except OSError as exc: # Python >2.5
113+
if exc.errno == errno.EEXIST and os.path.isdir(path):
114+
pass
115+
else:
116+
raise

tests/test_docs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_level_7(self):
8787
folder = "level-7-use-custom-jinja2-filter-test-n-global"
8888
self._raw_moban(["moban"], folder, expected, "test.output")
8989

90+
def test_misc_1(self):
91+
expected = "test file\n"
92+
93+
folder = "misc-1-copying-templates"
94+
self._raw_moban(["moban"], folder, expected, "simple.file.copy")
95+
9096
def _moban(self, folder, expected):
9197
args = ["moban", "-c", "data.yml", "-t", "a.template"]
9298
self._raw_moban(args, folder, expected, "moban.output")

0 commit comments

Comments
 (0)