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, like `moban foo.jj2`. 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 1, 2019
1 parent e0880fb commit db3f2ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions moban/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
LABEL_CONFIG = "configuration"
LABEL_CONFIG_DIR = "configuration_dir"
LABEL_PLUGIN_DIRS = "plugin_dir"
LABEL_SIMPLE_TEMPLATE = "simple_template"
LABEL_TEMPLATE = "template"
LABEL_TMPL_DIRS = "template_dir"
LABEL_OUTPUT = "output"
Expand Down
6 changes: 6 additions & 0 deletions moban/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def create_parser():
parser = argparse.ArgumentParser(
prog=constants.PROGRAM_NAME, description=constants.PROGRAM_DESCRIPTION
)
parser.add_argument(
constants.LABEL_SIMPLE_TEMPLATE, metavar=constants.LABEL_TEMPLATE,
type=str, nargs='?', help='the template file (low-setup mode)'
)
parser.add_argument(
"-cd",
"--%s" % constants.LABEL_CONFIG_DIR,
Expand Down Expand Up @@ -153,6 +157,8 @@ def handle_command_line(options):
act upon command options
"""
options = merge(options, constants.DEFAULT_OPTIONS)
if options[constants.LABEL_SIMPLE_TEMPLATE]:
options[constants.LABEL_TEMPLATE] = options[constants.LABEL_SIMPLE_TEMPLATE]
if options[constants.LABEL_TEMPLATE] is None:
raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
engine = plugins.ENGINES.get_engine(
Expand Down
10 changes: 9 additions & 1 deletion 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
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 db3f2ec

Please sign in to comment.