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 2, 2019
1 parent e0880fb commit b32bc8a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
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
13 changes: 9 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,18 @@ 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 any(template in t.values() for t in targets):
# If template from CLI is in targets from custom moban file,
# update only the template specifed by CLI
targets = [t for t in targets if template in t.values()]
else:
# Otherwise, append to targets (issue 30)
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
5 changes: 5 additions & 0 deletions moban/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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..."


def report_templating(source_file, destination_file):
Expand Down Expand Up @@ -89,6 +90,10 @@ 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 _format_single(message, count):
if count == 1:
return message.replace("files", "file")
Expand Down

0 comments on commit b32bc8a

Please sign in to comment.