-
Notifications
You must be signed in to change notification settings - Fork 0
/
classyjson.py
818 lines (672 loc) · 24.1 KB
/
classyjson.py
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
""" General class objects for writing the parameters out. Does not have specific
logic for a particular parameters schema.
"""
# flake8: noqa: E501
# ############################################################################ #
# Imports
# ############################################################################ #
# standard
import logging
from typing import ( # pylint: disable=no-name-in-module
Any,
Tuple,
Dict,
List,
Union,
Optional,
Type,
TypeVar,
IO,
Protocol,
)
import json
import io
import os
# external
try:
import jsonschema
_jsonschema_validate = jsonschema.validate
except ImportError:
jsonschema = None
_jsonschema_validate = lambda x: x
logger = logging.getLogger(__name__)
__all__ = [
# dotdict
"DotDict",
# schema classes
"StrSchema",
"IntSchema",
"NumberSchema",
"BoolSchema",
"NullSchema",
"ObjectSchema",
"ArraySchema",
# classy json classes
"ClassyJson",
"ClassyObject",
"ClassyArray",
# load/dump functions
"load",
"loads",
"dump",
"dumps",
# constants
"JSON_TYPE_STR",
"JSON_TYPE_NUMBER",
"JSON_TYPE_INTEGER",
"JSON_TYPE_OBJECT",
"JSON_TYPE_ARRAY",
"JSON_TYPE_BOOL",
"JSON_TYPE_NULL",
]
JSON_TYPE_STR = "string"
JSON_TYPE_NUMBER = "number"
JSON_TYPE_INTEGER = "integer"
JSON_TYPE_OBJECT = "object"
JSON_TYPE_ARRAY = "array"
JSON_TYPE_BOOL = "boolean"
JSON_TYPE_NULL = "null"
# JSON_TYPE_DATETIME = "datetime"
ALL_JSON_TYPES = [
JSON_TYPE_STR,
JSON_TYPE_NUMBER,
JSON_TYPE_INTEGER,
JSON_TYPE_OBJECT,
JSON_TYPE_ARRAY,
JSON_TYPE_BOOL,
JSON_TYPE_NULL,
]
# ############################################################################ #
# Types
# ############################################################################ #
KT = TypeVar("KT")
VT = TypeVar("VT")
class TJsonArray(Protocol):
"""JSON Array Type
https://github.com/python/typing/issues/182#issuecomment-893657366
"""
__class__: Type[List["TJson"]] # type: ignore
class TJsonObject(Protocol):
"""JSON Object Type
https://github.com/python/typing/issues/182#issuecomment-893657366
"""
__class__: Type[Dict[str, "TJson"]] # type: ignore
# all json base types
TJson = Union[None, float, str, int, TJsonArray, TJsonObject]
# BaseSchema type
TBaseSchemaType = Type["BaseSchema"]
# ClassyJson type
TClassyJsonType = Type["ClassyJson"]
# ############################################################################ #
# DotDict
# ############################################################################ #
class DotDict(dict):
"""dot.notation access to dictionary keys"""
# do you overwrite existing attributes? e.g. self.items or self.keys
# if True then they are overwritten with the value
_overwrite_attrs = False
def __init__(self, *args, **kws):
super().__init__()
self._dictclass = self.__class__
self.update(*args, **kws)
def __repr__(self):
return f"{self.__class__.__name__}({super().__repr__()})"
def __getitem__(self, name: KT):
try:
return super().__getitem__(name)
except KeyError as error:
names = list(self.keys())
error.args = (f"'{name}' not in {names}",)
raise error
def _convert_value_type(self, value: VT) -> VT:
# modify type on set
value_type = type(value)
if value_type == dict: # pylint: disable=unidiomatic-typecheck
return self._dictclass(value)
elif isinstance(value, (list, tuple)):
gen = map(self._convert_value_type, value)
if isinstance(value, ClassyArray):
return value.__class__(list(gen), validate=False)
if value_type == list:
return list(gen)
if value_type == tuple:
return tuple(gen)
return value.__class__(gen)
else:
return value
def _setitem_setattr(self, name: str, value: VT):
if self._overwrite_attrs:
self.__dict__[name] = value
elif not hasattr(self, name):
self.__dict__[name] = value
def __setitem__(self, name: KT, value: VT):
value_dotdict = self._convert_value_type(value)
if isinstance(name, str):
self._setitem_setattr(name, value_dotdict)
return super().__setitem__(name, value_dotdict)
def setdefault(self, key: KT, default: VT = None) -> VT:
"""Set default"""
if key not in self:
self[key] = default
return self[key]
def update(self, *args, **kws):
for arg in args:
kws.update(arg)
for key, value in kws.items():
self[key] = value
return self
def pop(self, key: KT, default: VT = None) -> VT:
self.__dict__.pop(key, None)
return super().pop(key, default)
def __delitem__(self, name: str):
del self.__dict__[name]
return super().__delitem__(name)
def __setattr__(self, name: str, value: VT):
"""Use __setitem__"""
if name.startswith("_"):
super().__setattr__(name, value)
else:
self.__setitem__(name, value)
def __getattr__(self, name: str):
"""Use __getitem__"""
if name.startswith("_"):
return super().__getattribute__(name)
try:
return self.__getitem__(name)
except KeyError as error:
raise AttributeError(
f"{name} not in {tuple(self.__dict__.keys())}"
) from error
def __delattr__(self, name: str):
"""Use __delitem__"""
return self.__delitem__(name)
# ############################################################################ #
# Schema Classes
# ############################################################################ #
def _get_jsonschema(schema: Union[TJson, TClassyJsonType, "BaseSchema"]) -> TJson:
"""Get the jsonschema"""
if isinstance(schema, list):
return [_get_jsonschema(value) for value in schema]
if isinstance(schema, dict):
return {key: _get_jsonschema(value) for key, value in schema.items()}
if isinstance(schema, (float, str, int)):
return schema
raise TypeError(type(schema)) # pragma: no cover
class BaseSchema(dict):
"""Base jsonschema"""
_schema_type: Union[str, list] = ""
@property
def schema_type(self) -> Union[str, list]:
"""Return the type"""
return self["type"]
def get_jsonschema(self) -> TJson:
"""Get the jsonschema for this"""
return _get_jsonschema(dict(self))
def validate(self, instance: TJson, **kws):
"""Validate instance against schema"""
_jsonschema_validate(instance, self.get_jsonschema(), **kws)
def load(self, instance: TJson, validate: bool = True) -> Any:
"""Parse into objects"""
if validate:
self.validate(instance)
return instance
def __repr__(self):
return f"{self.__class__.__name__}({super().__repr__()})"
def __add__(self, other):
self_types = (
self.schema_type
if isinstance(self.schema_type, list)
else [self.schema_type]
)
other_types = (
other.schema_type
if isinstance(other.schema_type, list)
else [other.schema_type]
)
types = list(set(self_types + other_types))
props = self.copy()
props.update(other)
props["type"] = types
return BaseSchema(props)
def __init__(self, schema: Dict[str, TJson] = None, **kws):
schema_ = schema or {}
for key, value in kws.items():
if value is not None:
schema_[key] = value
schema_.setdefault("type", self._schema_type)
super().__init__(**schema_)
schema_type = self.schema_type
if isinstance(schema_type, str):
if schema_type not in ALL_JSON_TYPES:
raise LookupError(
f"Unknown json type '{schema_type}' not in {ALL_JSON_TYPES}"
)
elif isinstance(schema_type, list):
unknown_types = set(schema_type) - set(ALL_JSON_TYPES)
if len(unknown_types):
raise LookupError(
f"Unknown json types '{unknown_types}' not in {ALL_JSON_TYPES}"
)
else:
raise TypeError(f"Unknown schema_type {type(schema_type)}")
class StrSchema(BaseSchema):
"""type=string
https://json-schema.org/understanding-json-schema/reference/string.html
"""
_schema_type: str = JSON_TYPE_STR
def __init__( # pylint: disable=too-many-arguments
self,
schema: Dict[str, TJson] = None,
minLength: int = None,
maxLength: int = None,
pattern: str = None,
format: str = None, # pylint: disable=redefined-builtin
**kws,
):
"""String json type
Parameters
----------
schema: can provide entire schema
maxLength: The length of a string can be constrained using the minLength and maxLength keywords.
minLength: The length of a string can be constrained using the minLength and maxLength keywords.
pattern: The pattern keyword is used to restrict a string to a particular regular expression.
format: The format keyword allows for basic semantic identification of certain kinds of string values that are commonly used.
**kws: any other keywords
"""
super().__init__(
# as argument
schema=schema,
# specific
minLength=minLength,
maxLength=maxLength,
pattern=pattern,
format=format,
# any other
**kws,
)
class NumberSchema(BaseSchema):
"""type=number
https://json-schema.org/understanding-json-schema/reference/numeric.html
"""
_schema_type: str = JSON_TYPE_NUMBER
def __init__( # pylint: disable=too-many-arguments
self,
schema: Dict[str, TJson] = None,
multipleOf: int = None,
minimum: float = None,
exclusiveMinimum: Union[float, bool] = None,
maximum: float = None,
exclusiveMaximum: Union[float, bool] = None,
**kws,
):
"""Number json type
Parameters
----------
schema: can provide entire schema
multipleOf: Numbers can be restricted to a multiple of a given number, using the multipleOf keyword.
minimum: Range for numbers.
exclusiveMinimum: Range for numbers.
maximum: Range for numbers.
exclusiveMaximum: Range for numbers.
**kws: any other keywords
"""
super().__init__(
# as argument
schema=schema,
# specific
multipleOf=multipleOf,
minimum=minimum,
exclusiveMinimum=exclusiveMinimum,
maximum=maximum,
exclusiveMaximum=exclusiveMaximum,
# any other
**kws,
)
class IntSchema(BaseSchema):
"""type=int
https://json-schema.org/understanding-json-schema/reference/numeric.html
"""
_schema_type: str = JSON_TYPE_INTEGER
class BoolSchema(BaseSchema):
"""type=bool
https://json-schema.org/understanding-json-schema/reference/boolean.html
"""
_schema_type: str = JSON_TYPE_BOOL
class NullSchema(BaseSchema):
"""type=null
https://json-schema.org/understanding-json-schema/reference/null.html
"""
_schema_type: str = JSON_TYPE_NULL
class ObjectSchema(BaseSchema):
"""Object json schema
https://json-schema.org/understanding-json-schema/reference/object.html
"""
_schema_type: str = JSON_TYPE_OBJECT
def get_jsonschema(self) -> TJson:
"""Generate the full jsonschema"""
schema = self.copy()
if self.schema_properties:
properties = {}
for key, prop in self.schema_properties.items():
if isinstance(prop, BaseSchema):
prop_schema = prop.get_jsonschema()
elif isinstance(prop, type) and issubclass(prop, ClassyJson):
prop_schema = prop.schema.get_jsonschema()
else:
prop_schema = prop
properties[key] = prop_schema
schema["properties"] = properties
return schema
@property
def schema_properties(self) -> Optional[Dict[str, TJson]]:
"""properties"""
return self.get("properties")
@property
def schema_additional_properties(self) -> Optional[bool]:
"""additionalProperties"""
return self.get("additionalProperties")
@staticmethod
def _load_prop(
property_schema: Union[TJsonObject, TClassyJsonType], value: TJson = None
) -> Union[TJson, "ClassyJson"]:
if value is None:
return None
if isinstance(property_schema, type) and issubclass(
property_schema, ClassyJson
):
classy = property_schema
return classy(value, validate=False)
return value
def _load_known_properties(self, instance: TJson) -> Dict:
properties = self.schema_properties or {}
data = {}
for key, prop_schema in properties.items():
if key in instance:
data[key] = self._load_prop(prop_schema, instance[key])
elif "default" in prop_schema:
default = prop_schema["default"]
if isinstance(default, type) and issubclass(default, ClassyJson):
value = default()
else:
value = default
data[key] = value
return data
def _load_additional_properties(self, instance: TJson) -> Dict:
properties = self.schema_properties or {}
if self.schema_additional_properties is None:
additional_properties = self.schema_properties is None
else:
additional_properties = self.schema_additional_properties
data = {}
if additional_properties:
for key in instance:
if key not in properties:
data[key] = self._load_prop(None, instance[key])
return data
# TODO: fix overload types so ObjectSchema return TJsonObject
def load(self, instance: TJson, validate: bool = True) -> Any:
"""Load object"""
instance = super().load(instance, validate=validate)
if not isinstance(instance, dict):
raise TypeError(f"Wrong instance base type: {type(instance)}")
data = self._load_known_properties(instance)
data.update(self._load_additional_properties(instance))
return data
def __init__( # pylint: disable=too-many-arguments
self,
schema: Dict[str, TJson] = None,
properties: Dict[str, TJson] = None,
patternProperties: Dict[str, str] = None,
required: List[str] = None,
propertyNames: str = None,
minProperties: int = None,
maxProperties: int = None,
additionalProperties: bool = None,
**kws,
):
"""Object json type
Parameters
----------
schema: can provide entire schema
properties:
patternProperties:
required:
propertyNames:
minProperties:
maxProperties:
additionalProperties:
**kws: any other keywords
"""
super().__init__(
# as argument
schema=schema,
# specific
properties=properties,
patternProperties=patternProperties,
required=required,
propertyNames=propertyNames,
minProperties=minProperties,
maxProperties=maxProperties,
additionalProperties=additionalProperties,
# any other
**kws,
)
class ArraySchema(BaseSchema):
"""Schema Array Type
https://json-schema.org/understanding-json-schema/reference/array.html
"""
_schema_type: str = JSON_TYPE_ARRAY
@property
def schema_items(self) -> Optional[Union[Dict[str, TJson], List[TJson]]]:
"""items"""
return self.get("items")
def get_jsonschema(self) -> TJson:
"""Get the jsonschema for this"""
schema = self.copy()
items = self.schema_items
if items is None:
return schema
elif isinstance(items, BaseSchema):
schema["items"] = items.get_jsonschema()
elif isinstance(items, type) and issubclass(items, ClassyJson):
schema["items"] = items.schema.get_jsonschema()
elif isinstance(items, dict):
schema["items"] = items.copy()
elif isinstance(items, list):
items_schema = []
for item in items:
if isinstance(item, BaseSchema):
items_schema.append(item.get_jsonschema())
elif isinstance(item, type) and issubclass(item, ClassyJson):
items_schema.append(item.schema.get_jsonschema())
else:
items_schema.append(dict(item))
schema["items"] = items_schema
else:
raise TypeError(f"Unknown type {type(items)}")
return schema
def load(self, instance: TJson, validate: bool = True) -> Any:
"""Parse into objects"""
instance = super().load(instance, validate=validate)
if not isinstance(instance, list):
raise TypeError(f"Instance must be of base type list not {type(instance)}")
def _inf_item_generator(item):
"""Return this item"""
while True:
yield item
schema_items = self.schema_items
if isinstance(schema_items, dict):
schema_items_iter = _inf_item_generator(schema_items)
elif isinstance(schema_items, list):
schema_items_iter = schema_items
else:
schema_items_iter = _inf_item_generator(schema_items)
items = []
for schema_item, inst in zip(schema_items_iter, instance):
if schema_item is None:
items.append(inst)
else:
item_type = schema_item["type"]
if isinstance(item_type, type) and issubclass(item_type, ClassyJson):
classy = item_type
value = classy(inst, validate=False)
items.append(value)
else:
items.append(inst)
return items
def __init__(
self,
schema: Dict[str, TJson] = None,
items: Union[TJsonObject, TJsonArray] = None,
additionalItems: bool = None,
contains: Dict[str, str] = None,
minItems: int = None,
maxItems: int = None,
uniqueItems: bool = None,
**kws,
):
"""Array type
Parameters
----------
schema: can provide entire schema
items: List or Tuple validation.
additionalItems:
The additionalItems keyword controls whether it’s valid to have
additional items in a tuple beyond what is defined in items.
contains:
While the items schema must be valid for every item in the array,
the contains schema only needs to validate against one or more
items in the array.
minItems: The length of the array.
maxItems: The length of the array.
uniqueItems: Each of the items in an array is unique.
**kws: any other keywords
"""
super().__init__(
# as argument
schema=schema,
# specific
items=items,
additionalItems=additionalItems,
contains=contains,
minItems=minItems,
maxItems=maxItems,
uniqueItems=uniqueItems,
# any other
**kws,
)
# ############################################################################ #
# Classy Classes
# ############################################################################ #
class ClassyJson: # pylint: disable=too-few-public-methods
"""Python JSON Schema class object"""
_schema_class: TBaseSchemaType = BaseSchema
_schema_raw: Dict[str, Union[TJson, TClassyJsonType]] = {}
schema: BaseSchema
def __init_subclass__(cls) -> None:
schema_class = cls._schema_class
schema = getattr(cls, "schema", {}) or {}
if not isinstance(schema, BaseSchema):
cls._schema_raw = schema.copy()
cls.schema = schema_class(**schema)
def __init__(self):
"""self, instance: TJson, validate: bool = True"""
if not isinstance(self.schema, BaseSchema):
self.schema = self._schema_class(self.schema)
def __repr__(self):
return f"{self.__class__.__name__}({super().__repr__()})"
class ClassyObject(ClassyJson, DotDict):
"""Json Schema type 'object'"""
_schema_class: TBaseSchemaType = ObjectSchema
def __init__(self, instance: TJson = None, validate: bool = True):
super().__init__()
self._dictclass = DotDict
data = self.schema.load(instance or {}, validate=validate)
self.update(data)
def __repr__(self):
return f"{self.__class__.__name__}({dict.__repr__(self)})"
class ClassyArray(ClassyJson, list):
"""Json Schema type 'array'"""
_schema_class: TBaseSchemaType = ArraySchema
def __init__(self, instance: TJson = None, validate: bool = True):
super().__init__()
items = self.schema.load(instance or [], validate=validate)
self.extend(items)
def __repr__(self):
return f"{self.__class__.__name__}({list.__repr__(self)})"
# ############################################################################ #
# load/dump functions
# ############################################################################ #
def _load_json(
json_data: Union[str, Dict, IO[str]],
**kws,
) -> TJson:
"""Wrapper around json.load which handles overloaded json types"""
if isinstance(json_data, dict):
return json_data
if isinstance(json_data, str):
if os.path.exists(json_data):
with open(json_data) as buffer:
json_loaded = json.load(buffer, **kws)
else:
json_loaded = json.loads(json_data, **kws)
return json_loaded
if isinstance(json_data, io.BufferedReader):
return json.load(json_data, **kws)
raise TypeError(f"Invalid type {type(json_data)}")
def load(
json_data: Union[str, Dict, IO[str]],
classy: TClassyJsonType = None,
classy_options: Tuple[str, Dict[str, TClassyJsonType]] = None,
**kws,
) -> Union[ClassyJson, TJson]:
"""Load generic."""
json_loaded = _load_json(json_data, **kws)
if classy_options is not None:
keyword, options = classy_options
if not isinstance(json_loaded, dict):
raise TypeError(
f"classy_options can only be used for dict, not {type(json_loaded)}"
)
name = json_loaded[keyword]
try:
classy = options[name]
except KeyError as error:
raise KeyError(f"{name} not in {options.keys()}") from error
if classy is None: # no schema
return json_loaded
elif isinstance(classy, type):
return classy(json_loaded)
else:
return classy.__class__(json_loaded)
def loads(
json_data: str,
classy: TClassyJsonType = None,
classy_options: Tuple[str, Dict[str, TClassyJsonType]] = None,
) -> Union[ClassyJson, TJson]:
"""Load from string"""
return load(
json_data,
classy=classy,
classy_options=classy_options,
)
def dump(
obj: Union[ClassyJson, TJson],
fp: Union[str, IO[str]] = None,
**kws,
) -> Optional[str]:
"""Serialize to json."""
if fp is None:
return json.dumps(obj, **kws)
if isinstance(fp, str):
with open(fp, "w") as buffer:
json.dump(obj, buffer, **kws)
return None
if isinstance(fp, io.BufferedWriter):
json.dump(obj, fp, **kws)
return None
raise TypeError(f"Invalid type {type(fp)}")
def dumps(obj: Union[ClassyJson, TJson], **kws) -> Optional[str]:
"""Serialize to string."""
kws["fp"] = None
return dump(obj, **kws)