Commit fc98fa0715fa829e1396142a58501dfea05bf2b6

Authored by godsdog
1 parent f9a5a0a6

Implement parameters passing through command line

Parameters defined in brokers.yaml and users.yaml can be overridden
through command line. Example of usage:
-v BROKERS_PARAMS:'{"Quinta": {"intervals": {"default":
{"enquiry": [0, 0], "tender": [0, 5.3]}}}}'
-v USERS_PARAMS:'{"users": {"Tender_Owner": {"api_key": "a"}}}'
@@ -68,7 +68,8 @@ Set Suite Variable With Default Value @@ -68,7 +68,8 @@ Set Suite Variable With Default Value
68 68
69 # Load brokers data 69 # Load brokers data
70 ${file_path}= Get Variable Value ${BROKERS_FILE} brokers.yaml 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 Log ${BROKERS} 73 Log ${BROKERS}
73 Set Suite Variable ${BROKERS} 74 Set Suite Variable ${BROKERS}
74 # List of currently used brokers 75 # List of currently used brokers
@@ -76,7 +77,8 @@ Set Suite Variable With Default Value @@ -76,7 +77,8 @@ Set Suite Variable With Default Value
76 77
77 # Load users data 78 # Load users data
78 ${file_path}= Get Variable Value ${USERS_FILE} users.yaml 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 Log ${USERS.users} 82 Log ${USERS.users}
81 Set Suite Variable ${USERS} 83 Set Suite Variable ${USERS}
82 # List of currently used users 84 # List of currently used users
@@ -7,9 +7,9 @@ from dateutil.parser import parse @@ -7,9 +7,9 @@ from dateutil.parser import parse
7 from dpath.util import new as xpathnew 7 from dpath.util import new as xpathnew
8 from haversine import haversine 8 from haversine import haversine
9 from iso8601 import parse_date 9 from iso8601 import parse_date
10 -from json import load 10 +from json import load, loads
11 from jsonpath_rw import parse as parse_path 11 from jsonpath_rw import parse as parse_path
12 -from munch import fromYAML, Munch, munchify 12 +from munch import Munch, munchify
13 from robot.errors import ExecutionFailed 13 from robot.errors import ExecutionFailed
14 from robot.libraries.BuiltIn import BuiltIn 14 from robot.libraries.BuiltIn import BuiltIn
15 from robot.output import LOGGER 15 from robot.output import LOGGER
@@ -200,22 +200,38 @@ def munch_to_object(data, format="yaml"): @@ -200,22 +200,38 @@ def munch_to_object(data, format="yaml"):
200 return data.toYAML(allow_unicode=True, default_flow_style=False) 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 if not os.path.exists(file_name): 210 if not os.path.exists(file_name):
205 file_name = os.path.join(os.path.dirname(__file__), 'data', file_name) 211 file_name = os.path.join(os.path.dirname(__file__), 'data', file_name)
206 with open(file_name) as file_obj: 212 with open(file_name) as file_obj:
207 - if file_name.endswith(".json"): 213 + if file_name.endswith('.json'):
208 file_data = Munch.fromDict(load(file_obj)) 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 default = file_data.pop('Default') 218 default = file_data.pop('Default')
213 brokers = {} 219 brokers = {}
214 for k, v in file_data.iteritems(): 220 for k, v in file_data.iteritems():
215 brokers[k] = merge_dicts(default, v) 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 def compute_intrs(brokers_data, used_brokers): 237 def compute_intrs(brokers_data, used_brokers):
Please register or login to post a comment