1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
import json
import logging
from dataclasses import dataclass, MISSING
from .abstractions import AbstractJSONWizard
from .bases_meta import BaseJSONWizardMeta, LoadMeta, DumpMeta, register_type
from .class_helper import call_meta_initializer_if_needed
from .constants import PACKAGE_NAME
from .loader_selection import asdict, fromdict, fromlist
from .log import enable_library_debug_logging
from .type_def import dataclass_transform
# noinspection PyProtectedMember
from .utils.dataclass_compat import (_create_fn,
_dataclass_needs_refresh,
_set_new_attribute)
def _str_fn():
return _create_fn('__str__',
('self',),
['return self.to_json(indent=2)'])
def _first_declared_attr_in_mro(cls, name: str):
"""First `name` found in MRO (excluding cls); else None."""
for base in cls.__mro__[1:]:
attr = base.__dict__.get(name, MISSING)
if attr is not MISSING:
return attr
return None
def _set_from_dict_and_to_dict_if_needed(cls):
"""
Pin default dispatchers on subclasses.
Codegen is lazy; if a base later gets a specialised
`from_dict` / `to_dict`, subclasses would inherit it.
Defining defaults in `cls.__dict__` blocks that.
"""
if 'from_dict' not in cls.__dict__:
inherited = _first_declared_attr_in_mro(cls, 'from_dict')
if getattr(inherited, '__func__', None) is fromdict:
cls.from_dict = classmethod(fromdict)
if 'to_dict' not in cls.__dict__:
inherited = _first_declared_attr_in_mro(cls, 'to_dict')
if inherited is asdict:
cls.to_dict = asdict
# noinspection PyShadowingBuiltins
def _configure_wizard_class(cls,
str=True,
debug=False,
case=None,
dump_case=None,
load_case=None,
_key_transform=None,
_v1_default=False):
load_meta_kwargs = {}
if case is not None:
_v1_default = True
load_meta_kwargs['v1_case'] = case
if dump_case is not None:
_v1_default = True
load_meta_kwargs['v1_dump_case'] = dump_case
if load_case is not None:
_v1_default = True
load_meta_kwargs['v1_load_case'] = load_case
if _v1_default:
load_meta_kwargs['v1'] = True
if _key_transform is not None:
DumpMeta(key_transform=_key_transform).bind_to(cls)
if debug:
# minimum logging level for logs by this library
lvl = logging.DEBUG if isinstance(debug, bool) else debug
# enable library logging
enable_library_debug_logging(lvl)
# set `v1_debug` flag for the class's Meta
load_meta_kwargs['v1_debug'] = lvl
if load_meta_kwargs:
LoadMeta(**load_meta_kwargs).bind_to(cls)
# Calls the Meta initializer when inner :class:`Meta` is sub-classed.
call_meta_initializer_if_needed(cls)
# Add a `__str__` method to the subclass, if needed
if str:
_set_new_attribute(cls, '__str__', _str_fn())
# Add `from_dict` and `to_dict` methods to the subclass, if needed
_set_from_dict_and_to_dict_if_needed(cls)
@dataclass_transform()
class DataclassWizard(AbstractJSONWizard):
__slots__ = ()
class Meta(BaseJSONWizardMeta):
__slots__ = ()
__is_inner_meta__ = True
def __init_subclass__(cls):
return cls._init_subclass()
register_type = classmethod(register_type)
@classmethod
def from_json(cls, string, *,
decoder=json.loads,
**decoder_kwargs):
o = decoder(string, **decoder_kwargs)
return fromdict(cls, o) if isinstance(o, dict) else fromlist(cls, o)
from_list = classmethod(fromlist)
from_dict = classmethod(fromdict)
to_dict = asdict
def to_json(self, *,
encoder=json.dumps,
**encoder_kwargs):
return encoder(asdict(self), **encoder_kwargs)
@classmethod
def list_to_json(cls,
instances,
encoder=json.dumps,
**encoder_kwargs):
list_of_dict = [asdict(o, cls=cls) for o in instances]
return encoder(list_of_dict, **encoder_kwargs)
# noinspection PyShadowingBuiltins
def __init_subclass__(cls,
str=False,
debug=False,
case=None,
dump_case=None,
load_case=None,
_key_transform=None,
_v1_default=True,
_apply_dataclass=True,
**dc_kwargs):
super().__init_subclass__()
# skip classes provided by this library.
if cls.__module__.startswith(f'{PACKAGE_NAME}.'):
return
# Apply the @dataclass decorator.
if _apply_dataclass and _dataclass_needs_refresh(cls):
# noinspection PyArgumentList
dataclass(cls, **dc_kwargs)
_configure_wizard_class(cls, str, debug, case, dump_case, load_case,
_key_transform, _v1_default)
# noinspection PyAbstractClass
class JSONSerializable(DataclassWizard):
__slots__ = ()
# noinspection PyShadowingBuiltins
def __init_subclass__(cls,
str=True,
debug=False,
case=None,
dump_case=None,
load_case=None,
_key_transform=None,
_v1_default=False,
_apply_dataclass=False,
**_):
super().__init_subclass__(str, debug, case, dump_case, load_case,
_key_transform, _v1_default, _apply_dataclass)
def _str_pprint_fn():
from pprint import pformat
def __str__(self):
return pformat(self, width=70)
return __str__
# A handy alias in case it comes in useful to anyone :)
JSONWizard = JSONSerializable
class JSONPyWizard(JSONWizard):
"""Helper for JSONWizard that ensures dumping to JSON keeps keys as-is."""
# noinspection PyShadowingBuiltins
def __init_subclass__(cls,
str=True,
debug=False,
case=None,
dump_case=None,
load_case=None,
_key_transform=None,
_v1_default=False,
_apply_dataclass=False,
**_):
"""Bind child class to DumpMeta with no key transformation."""
# Call JSONSerializable.__init_subclass__()
# set `key_transform_with_dump` for the class's Meta
super().__init_subclass__(False, debug, case, dump_case, load_case, 'NONE',
_v1_default, _apply_dataclass)
# Add a `__str__` method to the subclass, if needed
if str:
_set_new_attribute(cls, '__str__', _str_pprint_fn())
|