Skip to content

Commit

Permalink
✨ blind copying. no touching on the file. simple copy files from temp…
Browse files Browse the repository at this point in the history
…late directories to target path. related to #16
  • Loading branch information
chfw committed May 14, 2018
1 parent bf13259 commit a60bd8d
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ organisation: moremoban
author: C. W.
contact: [email protected]
license: MIT
version: 0.1.3
current_version: 0.1.3
version: 0.1.4
current_version: 0.1.4
release: 0.1.3
branch: master
command_line_interface: "moban"
Expand Down
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
LABEL_OUTPUT = "output"
LABEL_TEMPLATE_TYPE = "template_type"
LABEL_TARGETS = "targets"
LABEL_COPY = "copy"
LABEL_OVERRIDES = "overrides"
LABEL_MOBANFILE = "mobanfile"
LABEL_FORCE = "force"
Expand Down
39 changes: 39 additions & 0 deletions moban/copier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import shutil

import moban.reporter as reporter


class Copier(object):
def __init__(self, template_dirs):
self.template_dirs = template_dirs
self._count = 0

def copy_files(self, file_list):
for dest_src_pair in file_list:
for dest, src in dest_src_pair.items():
src_path = self._get_src_file(src)
if src_path:
reporter.report_copying(src_path, dest)
shutil.copy(src_path, dest)
self._count = self._count + 1
else:
reporter.report_error_message(
"{0} cannot be found".format(src))

def number_of_copied_files(self):
return self._count

def report(self):
if self._count:
reporter.report_copying_summary(self._count)
else:
reporter.report_no_action()

def _get_src_file(self, src):
for folder in self.template_dirs:
path = os.path.join(folder, src)
if os.path.exists(path):
return path
else:
return None
15 changes: 14 additions & 1 deletion moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import moban.reporter as reporter
from moban.engine import EngineFactory
from moban.utils import merge, parse_targets
from moban.copier import Copier


def find_default_moban_file():
Expand All @@ -14,6 +15,13 @@ def find_default_moban_file():
return moban_file


def handle_copy(template_dirs, copy_config):
copier = Copier(template_dirs)
copier.copy_files(copy_config)
copier.report()
return copier.number_of_copied_files()


def handle_moban_file_v1(moban_file_configurations, command_line_options):
merged_options = None
if constants.LABEL_CONFIG in moban_file_configurations:
Expand All @@ -34,7 +42,12 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):
)
engine.render_to_files(list_of_templating_parameters)
engine.report()
number_of_copied_files = handle_copy(
merged_options[constants.LABEL_TMPL_DIRS],
moban_file_configurations[constants.LABEL_COPY]
)
exit_code = reporter.convert_to_shell_exit_code(
engine.number_of_templated_files()
engine.number_of_templated_files() + number_of_copied_files
)
reporter.report_up_to_date()
return exit_code
29 changes: 27 additions & 2 deletions moban/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import moban.constants as constants

MESSAGE_TEMPLATING = "Templating {0} to {1}"
MESSAGE_NO_ACTION = "Everything is up to date!"
MESSAGE_COPYING = "Copying {0} to {1}"
MESSAGE_UP_TO_DATE = "Everything is up to date!"
MESSAGE_NO_COPY = "No copying"
MESSAGE_NO_TEMPLATING = "No templating"
MESSAGE_REPORT = "Templated {0} out of {1} files."
MESSAGE_TEMPLATED_ALL = "Templated {0} files."
MESSAGE_COPIED_ALL = "Copied {0} files."


def report_templating(source_file, destination_file):
Expand All @@ -17,7 +21,7 @@ def report_templating(source_file, destination_file):


def report_no_action():
print(crayons.green(MESSAGE_NO_ACTION, bold=True))
print(crayons.yellow(MESSAGE_NO_TEMPLATING, bold=True))


def report_full_run(file_count):
Expand All @@ -35,9 +39,30 @@ def report_error_message(message):
print(crayons.white("Error: ", bold=True) + crayons.red(message))


def report_up_to_date():
print(crayons.green(MESSAGE_UP_TO_DATE, bold=True))


def convert_to_shell_exit_code(number_of_templated_files):
return (
constants.HAS_CHANGES
if number_of_templated_files > 0
else constants.NO_CHANGES
)


def report_copying(source_file, destination_file):
print(
MESSAGE_COPYING.format(
crayons.yellow(source_file), crayons.green(destination_file)
)
)


def report_no_copying_done():
print(crayons.red(MESSAGE_NO_COPY, bold=True))


def report_copying_summary(file_count):
figure = crayons.green(str(file_count), bold=True)
print(MESSAGE_COPIED_ALL.format(figure))
2 changes: 1 addition & 1 deletion mobanfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ targets:
- MANIFEST.in: MANIFEST.in.jj2
- output: CHANGELOG.rst
configuration: changelog.yml
template: CHANGELOG.rst.jj2
template: CHANGELOG.rst.jj2
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Template by setupmobans
#!/usr/bin/env python3

# Template by pypi-mobans
import os
import sys
import codecs
Expand All @@ -9,7 +11,7 @@

NAME = 'moban'
AUTHOR = 'C. W.'
VERSION = '0.1.3'
VERSION = '0.1.4'
EMAIL = '[email protected]'
LICENSE = 'MIT'
ENTRY_POINTS = {
Expand All @@ -20,7 +22,7 @@
DESCRIPTION = (
'Yet another jinja2 cli command for static text generation'
)
URL = 'https://github.com/moremoban/moban'
URL = 'https:///moremoban/moban'
DOWNLOAD_URL = '%s/archive/0.1.3.tar.gz' % URL
FILES = ['README.rst', 'CHANGELOG.rst']
KEYWORDS = [
Expand All @@ -30,8 +32,6 @@
]

CLASSIFIERS = [
'Topic :: Office/Business',
'Topic :: Utilities',
'Topic :: Software Development :: Libraries',
'Programming Language :: Python',
'Intended Audience :: Developers',
Expand Down Expand Up @@ -134,7 +134,8 @@ def read_files(*files):

def read(afile):
"""Read a file into setup"""
with codecs.open(afile, 'r', 'utf-8') as opened_file:
the_relative_file = os.path.join(HERE, afile)
with codecs.open(the_relative_file, 'r', 'utf-8') as opened_file:
content = filter_out_test_code(opened_file)
content = "".join(list(content))
return content
Expand Down
Empty file.
50 changes: 50 additions & 0 deletions tests/test_copier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
from moban.copier import Copier
from moban.mobanfile import handle_copy
from nose.tools import eq_
from mock import patch


class TestCopier:

def setUp(self):
self.patcher = patch("shutil.copy")
self.fake_copy = self.patcher.start()

def tearDown(self):
self.patcher.stop()

@patch("moban.reporter.report_copying")
def test_copy_files(self, reporter):
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{
"/tmp/test": "copier-test01.csv"
}]
copier.copy_files(file_list)
self.fake_copy.assert_called()

@patch("moban.reporter.report_error_message")
def test_copy_files_file_not_found(self, reporter):
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{
"/tmp/test": "copier-test-not-found.csv"
}]
copier.copy_files(file_list)
reporter.assert_called_with(
"copier-test-not-found.csv cannot be found")

def test_number_of_files(self):
copier = Copier([os.path.join("tests", "fixtures")])
file_list = [{
"/tmp/test": "copier-test01.csv"
}]
copier.copy_files(file_list)
eq_(copier.number_of_copied_files(), 1)

def test_handle_copy(self):
tmpl_dirs = [os.path.join("tests", "fixtures")]
copy_config = [{
"/tmp/test": "copier-test01.csv"
}]
count = handle_copy(tmpl_dirs, copy_config)
eq_(count, 1)

0 comments on commit a60bd8d

Please sign in to comment.