Commit 1cdd02cde6e1a3dcc8aaca35550e7878ee7704ab
Merge pull request #454 from bubanoid/master
Implement parameters passing through command line
Showing
2 changed files
with
30 additions
and
12 deletions
... | ... | @@ -68,7 +68,8 @@ Set Suite Variable With Default Value |
68 | 68 | |
69 | 69 | # Load brokers data |
70 | 70 | ${file_path}= Get Variable Value ${BROKERS_FILE} brokers.yaml |
71 | - ${BROKERS}= load_data_from ${file_path} mode=brokers | |
71 | + ${BROKERS_PARAMS}= Get Variable Value ${BROKERS_PARAMS} | |
72 | + ${BROKERS}= load_data_from ${file_path} mode=brokers external_params_name=BROKERS_PARAMS | |
72 | 73 | Log ${BROKERS} |
73 | 74 | Set Suite Variable ${BROKERS} |
74 | 75 | # List of currently used brokers |
... | ... | @@ -76,7 +77,8 @@ Set Suite Variable With Default Value |
76 | 77 | |
77 | 78 | # Load users data |
78 | 79 | ${file_path}= Get Variable Value ${USERS_FILE} users.yaml |
79 | - ${USERS}= load_data_from ${file_path} | |
80 | + ${USERS_PARAMS}= Get Variable Value ${USERS_PARAMS} | |
81 | + ${USERS}= load_data_from ${file_path} users.yaml external_params_name=USERS_PARAMS | |
80 | 82 | Log ${USERS.users} |
81 | 83 | Set Suite Variable ${USERS} |
82 | 84 | # List of currently used users | ... | ... |
... | ... | @@ -7,9 +7,9 @@ from dateutil.parser import parse |
7 | 7 | from dpath.util import new as xpathnew |
8 | 8 | from haversine import haversine |
9 | 9 | from iso8601 import parse_date |
10 | -from json import load | |
10 | +from json import load, loads | |
11 | 11 | from jsonpath_rw import parse as parse_path |
12 | -from munch import fromYAML, Munch, munchify | |
12 | +from munch import Munch, munchify | |
13 | 13 | from robot.errors import ExecutionFailed |
14 | 14 | from robot.libraries.BuiltIn import BuiltIn |
15 | 15 | from robot.output import LOGGER |
... | ... | @@ -200,22 +200,38 @@ def munch_to_object(data, format="yaml"): |
200 | 200 | return data.toYAML(allow_unicode=True, default_flow_style=False) |
201 | 201 | |
202 | 202 | |
203 | -def load_data_from(file_name, mode=None): | |
203 | +def load_data_from(file_name, mode=None, external_params_name=None): | |
204 | + """We assume that 'external_params' is a a valid json if passed | |
205 | + """ | |
206 | + | |
207 | + external_params = BuiltIn().\ | |
208 | + get_variable_value('${{{name}}}'.format(name=external_params_name)) | |
209 | + | |
204 | 210 | if not os.path.exists(file_name): |
205 | 211 | file_name = os.path.join(os.path.dirname(__file__), 'data', file_name) |
206 | 212 | with open(file_name) as file_obj: |
207 | - if file_name.endswith(".json"): | |
213 | + if file_name.endswith('.json'): | |
208 | 214 | file_data = Munch.fromDict(load(file_obj)) |
209 | - elif file_name.endswith(".yaml"): | |
210 | - file_data = fromYAML(file_obj) | |
211 | - if mode == "brokers": | |
215 | + elif file_name.endswith('.yaml'): | |
216 | + file_data = Munch.fromYAML(file_obj) | |
217 | + if mode == 'brokers': | |
212 | 218 | default = file_data.pop('Default') |
213 | 219 | brokers = {} |
214 | 220 | for k, v in file_data.iteritems(): |
215 | 221 | brokers[k] = merge_dicts(default, v) |
216 | - return brokers | |
217 | - else: | |
218 | - return file_data | |
222 | + file_data = brokers | |
223 | + | |
224 | + try: | |
225 | + ext_params_munch \ | |
226 | + = Munch.fromDict(loads(external_params)) \ | |
227 | + if external_params else Munch() | |
228 | + except ValueError: | |
229 | + raise ValueError( | |
230 | + 'Value {param} of command line parameter {name} is invalid'. | |
231 | + format(name=external_params_name, param=str(external_params)) | |
232 | + ) | |
233 | + | |
234 | + return merge_dicts(file_data, ext_params_munch) | |
219 | 235 | |
220 | 236 | |
221 | 237 | def compute_intrs(brokers_data, used_brokers): | ... | ... |
Please
register
or
login
to post a comment