Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions pyaml/configuration/fileloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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]):
Expand Down
3 changes: 1 addition & 2 deletions tests/config/sr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/config/sr/correctors/SH1AC01-C02.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading