X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=conf%2F__init__.py;h=83c5475f72218f325d1ee336dc1da96ec11a3bca;hb=0236fe345f85faf5e07fae683106021c97490720;hp=e24111dcb89a4252cc632511154beeb7e6612126;hpb=de6fc4b670fc42fc96f27f375fbcf7a099629434;p=vswitchperf.git diff --git a/conf/__init__.py b/conf/__init__.py index e24111dc..83c5475f 100644 --- a/conf/__init__.py +++ b/conf/__init__.py @@ -70,7 +70,7 @@ class Settings(object): except AttributeError: pass return param - elif isinstance(param, list) or isinstance(param, tuple): + elif isinstance(param, (list, tuple)): tmp_list = [] for item in param: tmp_list.append(self._eval_param(item)) @@ -93,7 +93,7 @@ class Settings(object): master_value = getattr(self, attr) # Check if parameter value was modified by CLI option cli_value = get_test_param(attr, None) - if cli_value: + if cli_value is not None: # TRAFFIC dictionary is not overridden by CLI option # but only updated by specified values if attr == 'TRAFFIC': @@ -124,6 +124,13 @@ class Settings(object): if name is not None and value is not None: super(Settings, self).__setattr__(name, value) + def resetValue(self, attr): + """If parameter was overridden by TEST_PARAMS, then it will + be set to its original value. + """ + if attr in self.__dict__['TEST_PARAMS']: + self.__dict__['TEST_PARAMS'].pop(attr) + def load_from_file(self, path): """Update ``settings`` with values found in module at ``path``. """ @@ -148,14 +155,15 @@ class Settings(object): :returns: None """ - regex = re.compile("^(?P[0-9]+).*.conf$") + regex = re.compile("^(?P[0-9]+)(?P[a-z]?)_.*.conf$") def get_prefix(filename): """ Provide a suitable function for sort's key arg """ match_object = regex.search(os.path.basename(filename)) - return int(match_object.group('digit_part')) + return [int(match_object.group('digit_part')), + match_object.group('alfa_part')] # get full file path to all files & dirs in dir_path file_paths = os.listdir(dir_path) @@ -166,7 +174,7 @@ class Settings(object): file_paths = [x for x in file_paths if os.path.isfile(x) and regex.search(os.path.basename(x))] - # sort ascending on the leading digits + # sort ascending on the leading digits and afla (e.g. 03_, 05a_) file_paths.sort(key=get_prefix) # load settings from each file in turn @@ -188,6 +196,18 @@ class Settings(object): else: setattr(self, key.upper(), conf[key]) + def restore_from_dict(self, conf): + """ + Restore ``settings`` with values found in ``conf``. + + Method will drop all configuration options and restore their + values from conf dictionary + """ + self.__dict__.clear() + tmp_conf = copy.deepcopy(conf) + for key in tmp_conf: + self.setValue(key, tmp_conf[key]) + def load_from_env(self): """ Update ``settings`` with values found in the environment. @@ -209,7 +229,7 @@ class Settings(object): if key not in self.__dict__ and key not in _EXTRA_TEST_PARAMS: unknown_keys.append(key) - if len(unknown_keys): + if unknown_keys: raise RuntimeError('Test parameters contain unknown configuration ' 'parameter(s): {}'.format(', '.join(unknown_keys))) @@ -250,7 +270,7 @@ class Settings(object): for vmindex in range(vm_number): value = master_value_str.replace('#VMINDEX', str(vmindex)) for macro, args, param, _, step in re.findall(_PARSE_PATTERN, value): - multi = int(step) if len(step) and int(step) else 1 + multi = int(step) if step and int(step) else 1 if macro == '#EVAL': # pylint: disable=eval-used tmp_result = str(eval(param)) @@ -305,12 +325,18 @@ class Settings(object): assert result == self.getValue(attr) return True - def validate_setValue(self, dummy_result, name, value): + def validate_setValue(self, _dummy_result, name, value): """Verifies, that value was correctly set """ assert value == self.__dict__[name] return True + def validate_resetValue(self, _dummy_result, attr): + """Verifies, that value was correctly reset + """ + return 'TEST_PARAMS' not in self.__dict__ or \ + attr not in self.__dict__['TEST_PARAMS'] + settings = Settings() def get_test_param(key, default=None):