2018-07-25 12:06:12 +00:00
|
|
|
from io import StringIO
|
2018-08-17 21:50:28 +00:00
|
|
|
from typing import ClassVar, TYPE_CHECKING
|
2018-07-24 11:28:53 +00:00
|
|
|
|
2018-07-27 04:33:24 +00:00
|
|
|
from ovgenpy.utils.keyword_dataclasses import dataclass, fields
|
2018-07-28 11:04:30 +00:00
|
|
|
# from dataclasses import dataclass, fields
|
2018-08-17 21:50:28 +00:00
|
|
|
from ruamel.yaml import yaml_object, YAML, Representer
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from enum import Enum
|
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-08-17 21:50:28 +00:00
|
|
|
_yaml_loadable = yaml_object(yaml)
|
2018-07-25 12:06:12 +00:00
|
|
|
|
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
def register_config(cls=None, *, always_dump: str = ''):
|
|
|
|
""" Marks class as @dataclass, and enables YAML dumping (excludes default fields).
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
dataclasses.dataclass is compatible with yaml.register_class.
|
|
|
|
typing.NamedTuple is incompatible.
|
|
|
|
"""
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
def decorator(cls: type):
|
|
|
|
cls.__getstate__ = _ConfigMixin.__getstate__
|
|
|
|
cls.__setstate__ = _ConfigMixin.__setstate__
|
|
|
|
cls.always_dump = always_dump
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
# https://stackoverflow.com/a/51497219/2683842
|
|
|
|
# YAML().register_class(cls) works... on versions more recent than 2018-07-12.
|
2018-08-17 21:50:28 +00:00
|
|
|
return _yaml_loadable(dataclass(cls))
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
if cls is not None:
|
|
|
|
return decorator(cls)
|
|
|
|
else:
|
|
|
|
return decorator
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
# __init__-less non-dataclasses are also compatible with yaml.register_class.
|
2018-07-24 11:28:53 +00:00
|
|
|
|
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
@dataclass()
|
|
|
|
class _ConfigMixin:
|
|
|
|
"""
|
|
|
|
Class is unused. __getstate__ and __setstate__ are assigned into other classes.
|
|
|
|
Ideally I'd use inheritance, but @yaml_object and @dataclass rely on decorators,
|
|
|
|
and I want @register_config to Just Work and not need inheritance.
|
|
|
|
"""
|
|
|
|
always_dump: ClassVar[str]
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
# SafeRepresenter.represent_yaml_object() uses __getstate__ to dump objects.
|
|
|
|
def __getstate__(self):
|
|
|
|
""" Returns all fields with non-default value, or appeear in
|
|
|
|
self.always_dump. """
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
always_dump = set(self.always_dump.split())
|
|
|
|
dump_all = ('*' in always_dump)
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
state = {}
|
|
|
|
cls = type(self)
|
2018-07-26 01:36:14 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
for field in fields(self):
|
|
|
|
name = field.name
|
|
|
|
value = getattr(self, name)
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
if dump_all or name in always_dump:
|
|
|
|
state[name] = value
|
|
|
|
continue
|
|
|
|
|
|
|
|
default = getattr(cls, name, object())
|
|
|
|
if value != default:
|
|
|
|
state[name] = value
|
|
|
|
|
|
|
|
return state
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-26 01:36:14 +00:00
|
|
|
# SafeConstructor.construct_yaml_object() uses __setstate__ to load objects.
|
2018-07-26 12:24:19 +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
|
2018-07-25 12:06:12 +00:00
|
|
|
|
2018-07-29 12:35:05 +00:00
|
|
|
# # FIXME crashes on generics, https://github.com/Stewori/pytypes ?
|
|
|
|
# if not isinstance(value, typ):
|
|
|
|
# name = type(self).__name__
|
|
|
|
# raise OvgenError(f'{name}.{key} was supplied {repr(value)}, should be of type {typ.__name__}')
|
2018-07-26 12:24:19 +00:00
|
|
|
|
|
|
|
if hasattr(self, '__post_init__'):
|
|
|
|
self.__post_init__()
|
|
|
|
|
|
|
|
|
2018-08-17 21:50:28 +00:00
|
|
|
def register_enum(cls: type):
|
|
|
|
cls.to_yaml = _EnumMixin.to_yaml
|
|
|
|
return _yaml_loadable(cls)
|
|
|
|
|
|
|
|
|
|
|
|
class _EnumMixin:
|
|
|
|
@classmethod
|
|
|
|
def to_yaml(cls, representer: Representer, node: 'Enum'):
|
|
|
|
return representer.represent_str(node._name_)
|
|
|
|
|
|
|
|
|
2018-07-26 12:24:19 +00:00
|
|
|
class OvgenError(Exception):
|
|
|
|
pass
|
2018-07-25 12:06:12 +00:00
|
|
|
|
|
|
|
|