2018-07-25 12:06:12 +00:00
|
|
|
from io import StringIO
|
2018-07-24 11:28:53 +00:00
|
|
|
|
2018-07-25 12:06:12 +00:00
|
|
|
from dataclasses import dataclass, fields
|
|
|
|
from ruamel.yaml import yaml_object, YAML
|
2018-07-24 11:28:53 +00:00
|
|
|
|
|
|
|
|
2018-07-25 12:06:12 +00:00
|
|
|
class MyYAML(YAML):
|
|
|
|
def dump(self, data, stream=None, **kw):
|
|
|
|
inefficient = False
|
|
|
|
if stream is None:
|
|
|
|
inefficient = True
|
|
|
|
stream = StringIO()
|
|
|
|
YAML.dump(self, data, stream, **kw)
|
|
|
|
if inefficient:
|
|
|
|
return stream.getvalue()
|
2018-07-24 11:28:53 +00:00
|
|
|
|
|
|
|
|
2018-07-25 12:06:12 +00:00
|
|
|
# https://yaml.readthedocs.io/en/latest/dumpcls.html
|
|
|
|
# >Only yaml = YAML(typ='unsafe') loads and dumps Python objects out-of-the-box. And
|
|
|
|
# >since it loads any Python object, this can be unsafe.
|
|
|
|
# I assume roundtrip is safe.
|
|
|
|
yaml = MyYAML()
|
|
|
|
|
|
|
|
|
2018-07-26 01:36:14 +00:00
|
|
|
class OvgenError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-07-25 12:06:12 +00:00
|
|
|
def __getstate__(self):
|
2018-07-26 01:36:14 +00:00
|
|
|
""" Returns all non-default fields. """
|
2018-07-25 12:06:12 +00:00
|
|
|
state = {}
|
|
|
|
cls = type(self)
|
|
|
|
|
|
|
|
for field in fields(self):
|
|
|
|
name = field.name
|
|
|
|
value = getattr(self, name)
|
|
|
|
default = getattr(cls, name, object())
|
|
|
|
|
|
|
|
if value != default:
|
|
|
|
state[name] = value
|
|
|
|
|
|
|
|
return state
|
2018-07-24 11:28:53 +00:00
|
|
|
|
|
|
|
|
2018-07-26 01:36:14 +00:00
|
|
|
def __setstate__(self, state):
|
|
|
|
""" Checks that all fields match their correct types. """
|
|
|
|
self.__dict__.update(state)
|
|
|
|
for field in fields(self):
|
|
|
|
key = field.name
|
|
|
|
value = getattr(self, key)
|
|
|
|
typ = field.type
|
|
|
|
|
|
|
|
if not isinstance(value, typ):
|
|
|
|
name = type(self).__name__
|
|
|
|
raise OvgenError(f'{name}.{key} was supplied {repr(value)}, should be of type {typ.__name__}')
|
|
|
|
|
|
|
|
if hasattr(self, '__post_init__'):
|
|
|
|
self.__post_init__()
|
|
|
|
|
|
|
|
|
2018-07-25 12:01:58 +00:00
|
|
|
def register_config(cls):
|
2018-07-25 12:06:12 +00:00
|
|
|
""" Marks class as @dataclass, and enables YAML dumping (excludes default fields).
|
|
|
|
|
|
|
|
dataclasses.dataclass is compatible with yaml.register_class.
|
|
|
|
typing.NamedTuple is incompatible.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# SafeRepresenter.represent_yaml_object() uses __getstate__ to dump objects.
|
|
|
|
cls.__getstate__ = __getstate__
|
2018-07-26 01:36:14 +00:00
|
|
|
# SafeConstructor.construct_yaml_object() uses __setstate__ to load objects.
|
|
|
|
cls.__setstate__ = __setstate__
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-24 11:28:53 +00:00
|
|
|
# https://stackoverflow.com/a/51497219/2683842
|
2018-07-25 12:06:12 +00:00
|
|
|
# YAML().register_class(cls) works... on versions more recent than 2018-07-12.
|
2018-07-24 11:28:53 +00:00
|
|
|
return yaml_object(yaml)(
|
|
|
|
dataclass(cls)
|
|
|
|
)
|
2018-07-25 12:06:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# __init__-less non-dataclasses are also compatible with yaml.register_class.
|