def _process_includes(raw, filepath, error_name): if not raw: return [] source_dir = os.path.dirname(filepath) included_files = [] replace_value = None if hasattr(raw, 'items'): for key, value in raw.items(): if key == 'include#': include_path = os.path.expanduser( os.path.join(source_dir, value)) included_files.append(include_path) replace_value, includes = _load_file(include_path, error_name) included_files.extend(includes) elif hasattr(value, 'items') or isiterable(value): includes = _process_includes(value, filepath, error_name) included_files.extend(includes) elif isiterable(raw): for element in raw: if hasattr(element, 'items') or isiterable(element): includes = _process_includes(element, filepath, error_name) included_files.extend(includes) if replace_value is not None: del raw['include#'] for key, value in replace_value.items(): raw[key] = merge_config_values(value, raw.get(key, None)) return included_files
def set_value(self, obj, value=None, check_mandatory=True): if self.deprecated: if value is not None: msg = 'Depreciated parameter supplied for "{}" in "{}". The value will be ignored.' logger.warning(msg.format(self.name, obj.name)) return if value is None: if self.default is not None: value = self.kind(self.default) elif check_mandatory and self.mandatory: msg = 'No values specified for mandatory parameter "{}" in {}' raise ConfigError(msg.format(self.name, obj.name)) else: try: value = self.kind(value) except (ValueError, TypeError): typename = get_type_name(self.kind) msg = 'Bad value "{}" for {}; must be {} {}' article = get_article(typename) raise ConfigError(msg.format(value, self.name, article, typename)) if value is not None: self.validate_value(self.name, value) if self.merge and hasattr(obj, self.name): value = merge_config_values(getattr(obj, self.name), value) setattr(obj, self.name, value)
def _process_includes(raw, filepath, error_name): if not raw: return [] source_dir = os.path.dirname(filepath) included_files = [] replace_value = None if hasattr(raw, 'items'): for key, value in raw.items(): if key == 'include#': include_path = os.path.expanduser(os.path.join(source_dir, value)) included_files.append(include_path) replace_value, includes = _load_file(include_path, error_name) included_files.extend(includes) elif hasattr(value, 'items') or isiterable(value): includes = _process_includes(value, filepath, error_name) included_files.extend(includes) elif isiterable(raw): for element in raw: if hasattr(element, 'items') or isiterable(element): includes = _process_includes(element, filepath, error_name) included_files.extend(includes) if replace_value is not None: del raw['include#'] for key, value in replace_value.items(): raw[key] = merge_config_values(value, raw.get(key, None)) return included_files
def add_metric(self, name, value, units=None, lower_is_better=False, classifiers=None): if self.current_job: classifiers = merge_config_values(self.current_job.classifiers, classifiers) self.output.add_metric(name, value, units, lower_is_better, classifiers)
def test_merge_values(self): test_cases = [ # base, other, expected_result ('a', 3, 3), ('a', [1, 2], ['a', 1, 2]), ({1: 2}, [3, 4], [{1: 2}, 3, 4]), (set([2]), [1, 2, 3], [2, 1, 3]), ([1, 2, 3], set([2]), set([1, 2, 3])), ([1, 2], None, [1, 2]), (None, 'a', 'a'), ] for v1, v2, expected in test_cases: result = merge_config_values(v1, v2) assert_equal(result, expected) if v2 is not None: assert_equal(type(result), type(v2))
def set_value(self, obj, value=None, check_mandatory=True): if value is None: if self.default is not None: value = self.kind(self.default) elif check_mandatory and self.mandatory: msg = 'No values specified for mandatory parameter "{}" in {}' raise ConfigError(msg.format(self.name, obj.name)) else: try: value = self.kind(value) except (ValueError, TypeError): typename = get_type_name(self.kind) msg = 'Bad value "{}" for {}; must be {} {}' article = get_article(typename) raise ConfigError(msg.format(value, self.name, article, typename)) if value is not None: self.validate_value(self.name, value) if self.merge and hasattr(obj, self.name): value = merge_config_values(getattr(obj, self.name), value) setattr(obj, self.name, value)