diff --git a/pyaml/configuration/fileloader.py b/pyaml/configuration/fileloader.py index 6df60c1d..d4a8e81e 100644 --- a/pyaml/configuration/fileloader.py +++ b/pyaml/configuration/fileloader.py @@ -59,33 +59,26 @@ def __init__(self, error_filename: str, path_stack: list[Path]): self.error_filename = error_filename parent_file_stack = [parent_path.name for parent_path in path_stack] super().__init__( - f"Circular file inclusion of {error_filename}. " - f"File list before reaching it: {parent_file_stack}" + f"Circular file inclusion of {error_filename}. File list before reaching it: {parent_file_stack}" ) pass -def load( - filename: str, paths_stack: list = None, use_fast_loader: bool = False -) -> Union[dict, list]: +def load(filename: str, paths_stack: list = None, use_fast_loader: bool = False) -> Union[dict, list]: """Load recursively a configuration setup""" if filename.endswith(".yaml") or filename.endswith(".yml"): l = YAMLLoader(filename, paths_stack, use_fast_loader) elif filename.endswith(".json"): l = JSONLoader(filename, paths_stack, use_fast_loader) else: - raise PyAMLException( - f"{filename} File format not supported (only .yaml .yml or .json)" - ) + raise PyAMLException(f"{filename} File format not supported (only .yaml .yml or .json)") return l.load() # Expand condition def hasToLoad(value): - return isinstance(value, str) and any( - value.endswith(suffix) for suffix in accepted_suffixes - ) + return isinstance(value, str) and any(value.endswith(suffix) for suffix in accepted_suffixes) # Loader base class (nested files expansion) @@ -94,9 +87,7 @@ def __init__(self, filename: str, parent_path_stack: list[Path]): self.path: Path = get_path(filename) self.files_stack: list[Path] = [] if parent_path_stack: - if any( - self.path.samefile(parent_path) for parent_path in parent_path_stack - ): + if any(self.path.samefile(parent_path) for parent_path in parent_path_stack): raise PyAMLConfigCyclingException(filename, parent_path_stack) self.files_stack.extend(parent_path_stack) self.files_stack.append(self.path) @@ -125,17 +116,25 @@ def expand_dict(self, d: dict): file, line, col = location location_str = f" in {file} at line {line}, column {col}" raise PyAMLException( - "Circular file inclusion " - f"of {pyaml_ex.error_filename}{location_str}" + f"Circular file inclusion of {pyaml_ex.error_filename}{location_str}" ) from pyaml_ex # Recursively expand a list def expand_list(self, l: list): - for idx, value in enumerate(l): + idx = 0 + while idx < len(l): + value = l[idx] if hasToLoad(value): - l[idx] = load(value, self.files_stack) + obj = load(value, self.files_stack) + if isinstance(obj, list): + l[idx : idx + 1] = obj + idx += len(obj) + else: + l[idx] = obj + idx += 1 else: self.expand(value) + idx += 1 # Recursively expand an object def expand(self, obj: Union[dict, list]): diff --git a/tests/config/sr.yaml b/tests/config/sr.yaml index b223852e..e2f6ac2e 100644 --- a/tests/config/sr.yaml +++ b/tests/config/sr.yaml @@ -41,8 +41,7 @@ arrays: - SH1A-C02-H devices: - sr/quadrupoles/QF1AC01.yaml - - sr/correctors/SH1AC01.yaml - - sr/correctors/SH1AC02.yaml + - sr/correctors/SH1AC01-C02.yaml - type: pyaml.bpm.bpm name: BPM_C04-01 model: diff --git a/tests/config/sr/correctors/SH1AC01-C02.yaml b/tests/config/sr/correctors/SH1AC01-C02.yaml new file mode 100644 index 00000000..0bba2e4f --- /dev/null +++ b/tests/config/sr/correctors/SH1AC01-C02.yaml @@ -0,0 +1,22 @@ +- type: pyaml.magnet.cfm_magnet + name: SH1A-C01 #Name of the element in the lattice model + mapping: + # Multipole mapping for usage in families, in this example SH1-C01A-H is not + # a lattice element present in the model, it is just a name to use in + # PyAML families. When this 'virutal' element is set, it then applies + # the corresponding multipole on the parent element. + - [B0, SH1A-C01-H] + - [A0, SH1A-C01-V] + - [A1, SH1A-C01-SQ] + model: sr/magnet_models/SH1AC01.yaml +- type: pyaml.magnet.cfm_magnet + name: SH1A-C02 #Name of the element in the lattice model + mapping: + # Multipole mapping for usage in families, in this example SH1-C01A-H is not + # a lattice element present in the model, it is just a name to use in + # PyAML families. When this 'virutal' element is set, it then applies + # the corresponding multipole on the parent element. + - [B0, SH1A-C02-H] + - [A0, SH1A-C02-V] + - [A1, SH1A-C02-SQ] + model: sr/magnet_models/SH1AC02.yaml