Skip to content

11 yaml loader and factory cycle detection reporting#18

Merged
JeanLucPons merged 3 commits intomainfrom
11-yaml-loader-and-factory-cycle-detection-reporting
Aug 5, 2025
Merged

11 yaml loader and factory cycle detection reporting#18
JeanLucPons merged 3 commits intomainfrom
11-yaml-loader-and-factory-cycle-detection-reporting

Conversation

@gupichon
Copy link
Contributor

@gupichon gupichon commented Aug 5, 2025

No description provided.

@JeanLucPons
Copy link
Contributor

The detection works, however I would expect a more comprehensive error, such as:
/home/esrf/pons/pyaml/tests/config/sr/magnet_models/QF1AC01.yaml: Circular file inclusion of sr/quadrupoles/QF1AC01.yaml at line 4, colum 7

I modified tests/config/sr/magnet_models/QF1AC01.yaml and for the curve field, i put:
curve: sr/quadrupoles/QF1AC01.yaml
I get this error:
pyaml.exception.PyAMLException: A cycle has been detected: [PosixPath('/home/esrf/pons/pyaml/tests/config/sr.yaml'), PosixPath('/home/esrf/pons/pyaml/tests/config/sr/quadrupoles/QF1AC01.yaml'), PosixPath('/home/esrf/pons/pyaml/tests/config/sr/magnet_models/QF1AC01.yaml')]

@JeanLucPons
Copy link
Contributor

I propose the following patch which produces the error:

pyaml.exception.PyAMLException: /home/esrf/pons/pyaml/tests/config/sr/magnet_models/QF1AC01.yaml: Circular inclusion of sr/quadrupoles/QF1AC01.yaml

Still need to implemented access to line number mapping...

     def __init__(self, filename:str, parent_path_stack:list):
        self.path:Path = get_root_folder() / filename
        self.files_stack = parent_path_stack if parent_path_stack is not None else []
        self.files_stack.append(self.path)

    # Expand condition
    def hasToExpand(self,value):
        expand = isinstance(value,str) and any(value.endswith(suffix) for suffix in accepted_suffixes)
        if expand:
            expandedPath:Path = get_root_folder() / value
            if any(expandedPath.samefile(p) for p in self.files_stack):
                raise PyAMLException(f"{self.path}: Circular inclusion: {value}")
        return expand

    # Recursively expand a dict
    def expand_dict(self,d:dict):
        for key, value in d.items():
            if self.hasToExpand(value):
                d[key] = load(value, self.files_stack)
            else:
                self.expand(value)

@gupichon
Copy link
Contributor Author

gupichon commented Aug 5, 2025

@JeanLucPons, have another look, I have updated tests and set a better exception message.

@JeanLucPons
Copy link
Contributor

Thanks it is much better, however, same problem with list.
The location point to the root field.
This could be the purpose of an other pull request to improve error detection in list.

@JeanLucPons
Copy link
Contributor

Note: no reviewer is assigned to this pull request.

@gupichon gupichon requested a review from JeanLucPons August 5, 2025 15:30
@JeanLucPons JeanLucPons merged commit 8d85636 into main Aug 5, 2025
2 checks passed
@JeanLucPons JeanLucPons deleted the 11-yaml-loader-and-factory-cycle-detection-reporting branch August 5, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

yaml loader and factory error management: cycle detection, line mapping, reporting

3 participants