Skip to content

Commit

Permalink
✨ Add low-setup usage
Browse files Browse the repository at this point in the history
This adds the ability to use moban in an ad-hoc manner
without a config file. This also adds environment variables
as a fallback data source if the default/specified data files
do not exist.

Closes moremoban#133
  • Loading branch information
CLiu13 committed Jan 3, 2019
1 parent e0880fb commit 5fb0d64
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 26 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: Updated
details:
- "`#146`: added a low-setup usage mode via environment variables to moban"
date: 3-1-2019
version: 0.3.7
- changes:
- action: Updated
details:
Expand Down
6 changes: 3 additions & 3 deletions .moban.cd/moban.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ organisation: moremoban
author: C. W.
contact: wangc_2011@hotmail.com
license: MIT
version: 0.3.6
current_version: 0.3.6
release: 0.3.6
version: 0.3.7
current_version: 0.3.7
release: 0.3.7
branch: master
command_line_interface: "moban"
entry_point: "moban.main:main"
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Change log
================================================================================

0.3.7 - 3-1-2019
--------------------------------------------------------------------------------

Updated
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#. `#146 <https://github.com/moremoban/moban/issues/146>`_: added a low-setup
usage mode via environment variables to moban

0.3.6 - 30-12-2018
--------------------------------------------------------------------------------

Expand Down
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ Usage
-cd CONFIGURATION_DIR, --configuration_dir CONFIGURATION_DIR
the directory for configuration file lookup
-c CONFIGURATION, --configuration CONFIGURATION
the dictionary file
the dictionary file. if not present, moban
will try to use environment vars as data
-td [TEMPLATE_DIR [TEMPLATE_DIR ...]], --template_dir [TEMPLATE_DIR [TEMPLATE_DIR ...]]
the directories for template file lookup
-t TEMPLATE, --template TEMPLATE
the template file
the template file. this overrides any targets
defined in a custom moban file
-o OUTPUT, --output OUTPUT
the output file
--template_type TEMPLATE_TYPE
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
author = u'C. W.'

# The short X.Y version
version = u'0.3.6'
version = u'0.3.7'
# The full version, including alpha/beta/rc tags
release = u'0.3.6'
release = u'0.3.7'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion moban/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.6"
__version__ = "0.3.7"
__author__ = "C. W."
1 change: 0 additions & 1 deletion moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def handle_command_line(options):
"""
act upon command options
"""
options = merge(options, constants.DEFAULT_OPTIONS)
if options[constants.LABEL_TEMPLATE] is None:
raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
engine = plugins.ENGINES.get_engine(
Expand Down
12 changes: 8 additions & 4 deletions moban/mobanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def find_default_moban_file():

def handle_moban_file_v1(moban_file_configurations, command_line_options):
merged_options = None
target = extract_target(command_line_options)
if constants.LABEL_CONFIG in moban_file_configurations:
merged_options = merge(
command_line_options,
Expand All @@ -52,12 +51,17 @@ def handle_moban_file_v1(moban_file_configurations, command_line_options):
if requires:
handle_requires(requires)

target = extract_target(command_line_options)
targets = moban_file_configurations.get(constants.LABEL_TARGETS)
if targets:
if target:
# if command line option exists, append its template to targets
# issue 30
targets += target
template = target[0]['template']
if not any(template in t.values() for t in targets):
# Warn user if template not defined under targets in moban file
reporter.report_template_not_in_moban_file(template)
# Ignore any targets from moban file,
# only update template specified via CLI flag `-t`
targets = target
number_of_templated_files = handle_targets(merged_options, targets)
else:
number_of_templated_files = 0
Expand Down
30 changes: 27 additions & 3 deletions moban/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ def number_of_templated_files(self):
return self.templated_count

def render_to_file(self, template_file, data_file, output_file):
data = self.context.get_data(data_file)
try:
data = self.context.get_data(data_file)
except OSError as exception:
# If data file doesn't exist:
# 1. Alert the user of their (potential) mistake
# 2. Attempt to use environment vars as data
reporter.report_error_message(exception)
reporter.report_using_env_vars()
data = os.environ
template = self.engine.get_template(template_file)
template_abs_path = utils.get_template_path(
self.template_dirs, template_file
Expand Down Expand Up @@ -89,7 +97,15 @@ def _render_with_finding_template_first(self, template_file_index):
self.template_dirs, template_file
)
for (data_file, output) in data_output_pairs:
data = self.context.get_data(data_file)
try:
data = self.context.get_data(data_file)
except OSError as exception:
# If data file doesn't exist:
# 1. Alert the user of their (potential) mistake
# 2. Attempt to use environment vars as data
reporter.report_error_message(exception)
reporter.report_using_env_vars()
data = os.environ
flag = self.apply_template(
template_abs_path, template, data, output
)
Expand All @@ -100,7 +116,15 @@ def _render_with_finding_template_first(self, template_file_index):

def _render_with_finding_data_first(self, data_file_index):
for (data_file, template_output_pairs) in data_file_index.items():
data = self.context.get_data(data_file)
try:
data = self.context.get_data(data_file)
except OSError as exception:
# If data file doesn't exist:
# 1. Alert the user of their (potential) mistake
# 2. Attempt to use environment vars as data
reporter.report_error_message(exception)
reporter.report_using_env_vars()
data = os.environ
for (template_file, output) in template_output_pairs:
template = self.engine.get_template(template_file)
template_abs_path = utils.get_template_path(
Expand Down
11 changes: 11 additions & 0 deletions moban/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
MESSAGE_COPIED_ALL = "Copied {0} files."
MESSAGE_PULLING_REPO = "Updating {0}..."
MESSAGE_CLONING_REPO = "Cloning {0}..."
MESSAGE_USING_ENV_VARS = "Attempting to use environment vars as data..."
MESSAGE_TEMPLATE_NOT_IN_MOBAN_FILE = "Warning: {0} is not defined in your moban file!"


def report_templating(source_file, destination_file):
Expand Down Expand Up @@ -89,6 +91,15 @@ def report_git_clone(repo):
print(MESSAGE_CLONING_REPO.format(colored_repo))


def report_using_env_vars():
print(crayons.yellow(MESSAGE_USING_ENV_VARS, bold=True))


def report_template_not_in_moban_file(template):
message = MESSAGE_TEMPLATE_NOT_IN_MOBAN_FILE.format(template)
print(crayons.yellow(message, bold=True))


def _format_single(message, count):
if count == 1:
return message.replace("files", "file")
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

NAME = 'moban'
AUTHOR = 'C. W.'
VERSION = '0.3.6'
VERSION = '0.3.7'
EMAIL = 'wangc_2011@hotmail.com'
LICENSE = 'MIT'
ENTRY_POINTS = {
Expand All @@ -25,7 +25,7 @@
'Yet another jinja2 cli command for static text generation'
)
URL = 'https://github.com/moremoban/moban'
DOWNLOAD_URL = '%s/archive/0.3.6.tar.gz' % URL
DOWNLOAD_URL = '%s/archive/0.3.7.tar.gz' % URL
FILES = ['README.rst', 'CONTRIBUTORS.rst', 'CHANGELOG.rst']
KEYWORDS = [
'python',
Expand Down Expand Up @@ -60,8 +60,8 @@
# You do not need to read beyond this line
PUBLISH_COMMAND = '{0} setup.py sdist bdist_wheel upload -r pypi'.format(
sys.executable)
GS_COMMAND = ('gs moban v0.3.6 ' +
"Find 0.3.6 in changelog for more details")
GS_COMMAND = ('gs moban v0.3.7 ' +
"Find 0.3.7 in changelog for more details")
NO_GS_MESSAGE = ('Automatic github release is disabled. ' +
'Please install gease to enable it.')
UPLOAD_FAILED_MSG = (
Expand Down
7 changes: 1 addition & 6 deletions tests/integration_tests/test_command_line_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def tearDown(self):
os.unlink(self.config_file)


@raises(IOError)
@raises(OSError)
def test_missing_configuration():
test_args = ["moban", "-t", "a.jj2"]
with patch.object(sys, "argv", test_args):
Expand Down Expand Up @@ -146,8 +146,6 @@ def test_single_command_with_a_few_options(self, fake_template_doer):
eq_(
call_args,
[
("README.rst.jj2", "data.yaml", "README.rst"),
("setup.py.jj2", "data.yaml", "setup.py"),
("abc.jj2", "data.yaml", "xyz.output"),
],
)
Expand All @@ -171,14 +169,11 @@ def test_single_command_with_options(self, fake_template_doer):
eq_(
call_args,
[
("README.rst.jj2", "new.yml", "README.rst"),
("setup.py.jj2", "new.yml", "setup.py"),
("abc.jj2", "new.yml", "xyz.output"),
],
)

@raises(Exception)
@patch("moban.plugins.BaseEngine.render_to_files")
def test_single_command_without_output_option(self, fake_template_doer):
test_args = ["moban", "-t", "abc.jj2"]
with patch.object(sys, "argv", test_args):
Expand Down

0 comments on commit 5fb0d64

Please sign in to comment.