From db3f2ec5ed1cf617e7ef1cbf15ad24618e56651e Mon Sep 17 00:00:00 2001 From: CLiu13 Date: Tue, 1 Jan 2019 00:52:18 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20low-setup=20usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/moremoban/moban/issues/133 --- moban/constants.py | 1 + moban/main.py | 6 ++++++ moban/plugins.py | 10 +++++++++- moban/reporter.py | 5 +++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/moban/constants.py b/moban/constants.py index ae11d2f9..2c70f39c 100644 --- a/moban/constants.py +++ b/moban/constants.py @@ -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" diff --git a/moban/main.py b/moban/main.py index a453e19b..03cae0fc 100644 --- a/moban/main.py +++ b/moban/main.py @@ -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, @@ -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( diff --git a/moban/plugins.py b/moban/plugins.py index 544dc2a4..bb22bc4f 100644 --- a/moban/plugins.py +++ b/moban/plugins.py @@ -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 diff --git a/moban/reporter.py b/moban/reporter.py index bdb5bc42..7de1f6d9 100644 --- a/moban/reporter.py +++ b/moban/reporter.py @@ -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): @@ -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")