From fe05353060f2e1ba8cef25b5695b383eedea222c Mon Sep 17 00:00:00 2001 From: Claudio Satriano Date: Wed, 27 Mar 2024 15:44:40 +0100 Subject: [PATCH] `read_spectrum` always parse dictionaries as `AttributeDict` objects. --- sourcespec/spectrum.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/sourcespec/spectrum.py b/sourcespec/spectrum.py index 5bd2c404..c414c8b0 100644 --- a/sourcespec/spectrum.py +++ b/sourcespec/spectrum.py @@ -602,11 +602,24 @@ def _quoted_representer(dumper, data): return dumper.represent_scalar('tag:yaml.org,2002:str', data, style="'") +def _dict_constructor(loader, node): + """ + YAML constructor for dictionaries. + + :param loader: The YAML loader. + :param node: The node to construct. + :return: The dictionary constructed from the node. + """ + return AttributeDict(loader.construct_mapping(node)) + + # register the representers yaml.representer.SafeRepresenter.add_representer( None, _default_yaml_representer) _HDF5HeaderDumper.add_representer(str, _quoted_representer) _HDF5HeaderDumper.add_representer(None, _default_yaml_representer) +# register the constructor +yaml.SafeLoader.add_constructor('tag:yaml.org,2002:map', _dict_constructor) def _normalize_metadata_object(obj):