def load_yaml(cls, filename: str):
"""
Load a hierarchy of sparse objects from a YAML file.
:param filename: The filename to open.
:return: The configuration object hierarchy
"""
def unpack(mapping, parent=None):
"""
Recursively create Configuration objects with the parent
correctly set, returning the top-most parent.
"""
if mapping is None:
return None
children = config = None
if not isinstance(mapping, cls):
children = mapping.pop('children', None)
config = cls(**cls._coerce_types(mapping), parent=parent)
if children is not None and len(children) > 0:
for child in children:
unpack(child, parent=config)
return config
if filename in cls._yaml_cache:
return cls._yaml_cache[filename]
data = None
with open(filename, 'r') as yaml_file:
data = unpack(yaml.round_trip_load(yaml_file.read()))
if data is not None:
cls._yaml_cache[filename] = data
return data
评论列表
文章目录