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 2, 2019
1 parent e0880fb commit 270f9c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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 270f9c2

Please sign in to comment.