mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-01-18 20:11:20 +00:00
configuration: Allow including multiple files into one mapping.
The key for 'include#' can now be either a scalar or a list. A scalar triggers the same behaviour as before. If the value is a list it must be a list of scalars (filepaths). The paths will be loaded and merged in order, and finally the resulting dict is included into the current scope.
This commit is contained in:
parent
b6ecc18763
commit
4839ab354f
@ -238,20 +238,47 @@ def _load_file(filepath, error_name):
|
|||||||
return raw, includes
|
return raw, includes
|
||||||
|
|
||||||
|
|
||||||
|
def _config_values_from_includes(filepath, include_path, error_name):
|
||||||
|
source_dir = os.path.dirname(filepath)
|
||||||
|
included_files = []
|
||||||
|
|
||||||
|
if isinstance(include_path, str):
|
||||||
|
include_path = os.path.expanduser(os.path.join(source_dir, include_path))
|
||||||
|
|
||||||
|
replace_value, includes = _load_file(include_path, error_name)
|
||||||
|
|
||||||
|
included_files.append(include_path)
|
||||||
|
included_files.extend(includes)
|
||||||
|
elif isinstance(include_path, list):
|
||||||
|
replace_value = {}
|
||||||
|
|
||||||
|
for path in include_path:
|
||||||
|
include_path = os.path.expanduser(os.path.join(source_dir, path))
|
||||||
|
|
||||||
|
sub_replace_value, includes = _load_file(include_path, error_name)
|
||||||
|
for key, val in sub_replace_value.items():
|
||||||
|
replace_value[key] = merge_config_values(val, replace_value.get(key, None))
|
||||||
|
|
||||||
|
included_files.append(include_path)
|
||||||
|
included_files.extend(includes)
|
||||||
|
else:
|
||||||
|
message = "{} does not contain a valid {} structure; value for 'include#' must be a string or a list"
|
||||||
|
raise ConfigError(message.format(filepath, error_name))
|
||||||
|
|
||||||
|
return replace_value, included_files
|
||||||
|
|
||||||
|
|
||||||
def _process_includes(raw, filepath, error_name):
|
def _process_includes(raw, filepath, error_name):
|
||||||
if not raw:
|
if not raw:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
source_dir = os.path.dirname(filepath)
|
|
||||||
included_files = []
|
included_files = []
|
||||||
replace_value = None
|
replace_value = None
|
||||||
|
|
||||||
if hasattr(raw, 'items'):
|
if hasattr(raw, 'items'):
|
||||||
for key, value in raw.items():
|
for key, value in raw.items():
|
||||||
if key == 'include#':
|
if key == 'include#':
|
||||||
include_path = os.path.expanduser(os.path.join(source_dir, value))
|
replace_value, includes = _config_values_from_includes(filepath, value, error_name)
|
||||||
included_files.append(include_path)
|
|
||||||
replace_value, includes = _load_file(include_path, error_name)
|
|
||||||
included_files.extend(includes)
|
included_files.extend(includes)
|
||||||
elif hasattr(value, 'items') or isiterable(value):
|
elif hasattr(value, 'items') or isiterable(value):
|
||||||
includes = _process_includes(value, filepath, error_name)
|
includes = _process_includes(value, filepath, error_name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user