Skip to content

Commit 561ff52

Browse files
authored
Merge pull request #147 from CLiu13/low-setup-usage
🔨 Code refactoring
2 parents bb1eb42 + 61889f3 commit 561ff52

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

moban/plugins.py

+21-36
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ def number_of_templated_files(self):
4646
return self.templated_count
4747

4848
def render_to_file(self, template_file, data_file, output_file):
49-
try:
50-
data = self.context.get_data(data_file)
51-
except Exception as exception:
52-
# If data file doesn't exist:
53-
# 1. Alert the user of their (potential) mistake
54-
# 2. Attempt to use environment vars as data
55-
reporter.report_error_message(str(exception))
56-
reporter.report_using_env_vars()
57-
data = os.environ
49+
data = self.context.get_data(data_file)
5850
template = self.engine.get_template(template_file)
5951
template_abs_path = utils.get_template_path(
6052
self.template_dirs, template_file
@@ -97,15 +89,7 @@ def _render_with_finding_template_first(self, template_file_index):
9789
self.template_dirs, template_file
9890
)
9991
for (data_file, output) in data_output_pairs:
100-
try:
101-
data = self.context.get_data(data_file)
102-
except Exception as exception:
103-
# If data file doesn't exist:
104-
# 1. Alert the user of their (potential) mistake
105-
# 2. Attempt to use environment vars as data
106-
reporter.report_error_message(exception)
107-
reporter.report_using_env_vars()
108-
data = os.environ
92+
data = self.context.get_data(data_file)
10993
flag = self.apply_template(
11094
template_abs_path, template, data, output
11195
)
@@ -116,15 +100,7 @@ def _render_with_finding_template_first(self, template_file_index):
116100

117101
def _render_with_finding_data_first(self, data_file_index):
118102
for (data_file, template_output_pairs) in data_file_index.items():
119-
try:
120-
data = self.context.get_data(data_file)
121-
except Exception as exception:
122-
# If data file doesn't exist:
123-
# 1. Alert the user of their (potential) mistake
124-
# 2. Attempt to use environment vars as data
125-
reporter.report_error_message(exception)
126-
reporter.report_using_env_vars()
127-
data = os.environ
103+
data = self.context.get_data(data_file)
128104
for (template_file, output) in template_output_pairs:
129105
template = self.engine.get_template(template_file)
130106
template_abs_path = utils.get_template_path(
@@ -207,15 +183,24 @@ def __init__(self, context_dirs):
207183
)
208184

209185
def get_data(self, file_name):
210-
file_extension = os.path.splitext(file_name)[1]
211-
if file_extension == ".json":
212-
data = utils.open_json(self.context_dirs, file_name)
213-
elif file_extension in [".yml", ".yaml"]:
214-
data = utils.open_yaml(self.context_dirs, file_name)
215-
utils.merge(data, self.__cached_environ_variables)
216-
else:
217-
raise exceptions.IncorrectDataInput
218-
return data
186+
try:
187+
file_extension = os.path.splitext(file_name)[1]
188+
if file_extension == ".json":
189+
data = utils.open_json(self.context_dirs, file_name)
190+
elif file_extension in [".yml", ".yaml"]:
191+
data = utils.open_yaml(self.context_dirs, file_name)
192+
utils.merge(data, self.__cached_environ_variables)
193+
else:
194+
raise exceptions.IncorrectDataInput
195+
return data
196+
except Exception as exception:
197+
# If data file doesn't exist:
198+
# 1. Alert the user of their (potential) mistake
199+
# 2. Attempt to use environment vars as data
200+
reporter.report_error_message(str(exception))
201+
reporter.report_using_env_vars()
202+
data = os.environ
203+
return data
219204

220205

221206
def make_sure_all_pkg_are_loaded():

0 commit comments

Comments
 (0)