Skip to content

Commit

Permalink
🔨 Code refactoring
Browse files Browse the repository at this point in the history
Related to moremoban#146
  • Loading branch information
CLiu13 committed Jan 4, 2019
1 parent d9e98b8 commit 068248c
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions moban/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ def report(self):

def number_of_templated_files(self):
return self.templated_count

def

def render_to_file(self, template_file, data_file, output_file):
try:
data = self.context.get_data(data_file)
except Exception 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(str(exception))
reporter.report_using_env_vars()
data = os.environ
data = self.context.get_data(data_file)
template = self.engine.get_template(template_file)
template_abs_path = utils.get_template_path(
self.template_dirs, template_file
Expand Down Expand Up @@ -97,15 +91,7 @@ def _render_with_finding_template_first(self, template_file_index):
self.template_dirs, template_file
)
for (data_file, output) in data_output_pairs:
try:
data = self.context.get_data(data_file)
except Exception 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
data = self.context.get_data(data_file)
flag = self.apply_template(
template_abs_path, template, data, output
)
Expand All @@ -116,15 +102,7 @@ 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():
try:
data = self.context.get_data(data_file)
except Exception 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
data = self.context.get_data(data_file)
for (template_file, output) in template_output_pairs:
template = self.engine.get_template(template_file)
template_abs_path = utils.get_template_path(
Expand Down Expand Up @@ -207,15 +185,24 @@ def __init__(self, context_dirs):
)

def get_data(self, file_name):
file_extension = os.path.splitext(file_name)[1]
if file_extension == ".json":
data = utils.open_json(self.context_dirs, file_name)
elif file_extension in [".yml", ".yaml"]:
data = utils.open_yaml(self.context_dirs, file_name)
utils.merge(data, self.__cached_environ_variables)
else:
raise exceptions.IncorrectDataInput
return data
try:
file_extension = os.path.splitext(file_name)[1]
if file_extension == ".json":
data = utils.open_json(self.context_dirs, file_name)
elif file_extension in [".yml", ".yaml"]:
data = utils.open_yaml(self.context_dirs, file_name)
utils.merge(data, self.__cached_environ_variables)
else:
raise exceptions.IncorrectDataInput
return data
except Exception 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(str(exception))
reporter.report_using_env_vars()
data = os.environ
return data


def make_sure_all_pkg_are_loaded():
Expand Down

0 comments on commit 068248c

Please sign in to comment.