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 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
|
"""
Generates a Python (dataclass) schema, given a JSON input. The entry point for
this module is the `gen-schema` subcommand.
This JSON to Dataclass conversion tool was inspired by the following projects:
* https://github.com/mischareitsma/json2dataclass
* https://github.com/russbiggs/json2dataclass
* https://github.com/mholt/json-to-go
The parser supports the full JSON spec, so both `list` and `dict` as the
root type are properly handled as expected.
A few important notes on the behavior of JSON parsing:
* Lists with multiple dictionaries will have all the keys and type
definitions merged into a single model dataclass, as the dictionary
objects are considered homogenous in this case.
* Nested lists within the above structure (e.g. list -> dict -> list)
should similarly merge all list elements with the list for that same key
in each sibling `dict` object. For example, assuming the below input::
... [{"d1": [1, {"k": "v"}]}, {"d1": [{"k": 2}, {"k2": "v2"}, True]}]
This should result in a single, merged type definition for "d1"::
... List[Union[int, dataclass(k: Union[str, int], k2: str), bool]]
* Any nested dictionaries within lists will have their Model class name
generated with the singular form of the key containing the model
definition -- for example, {"Items":[{"key":"value"}]} will result in a
model class named `Item`. In the case a dictionary is nested within a
list, it will have the class name auto-incremented with a common
prefix -- for example, `Data1`, `Data2`, etc.
The implementation below uses regex code in the `rules.english` module from
the library Python-Inflector (https://github.com/bermi/Python-Inflector).
This library is available under the BSD license, which can be
obtained from https://opensource.org/licenses.
The library Python-Inflector contains the following attribution notices:
Copyright (c) 2006 Bermi Ferrer Martinez
bermi a-t bermilabs - com
See the end of this file for the original BSD-style license from this library.
"""
__all__ = [
'PyCodeGenerator'
]
import json
import re
import textwrap
from collections import defaultdict
from collections import deque
from collections.abc import Iterable
from dataclasses import dataclass, field, InitVar
from datetime import date, datetime, time
from enum import Enum
from pathlib import Path
from typing import Callable, Any, Optional, TypeVar, Type, ClassVar
from typing import DefaultDict, Set, List
from typing import (
Union, Dict, Sequence
)
from .. import property_wizard
from ..constants import PACKAGE_NAME
from ..class_helper import get_class_name
from ..type_def import PyDeque, JSONList, JSONObject, JSONValue, T
from ..utils.string_conv import to_snake_case, to_pascal_case
# noinspection PyProtectedMember
from ..utils.type_conv import TRUTHY_VALUES
from ..utils.type_conv import as_datetime, as_date, as_time
# Some unconstrained type variables. These are used by the container types.
# (These are not for export.)
_S = TypeVar('_S')
# Merge both the "truthy" and "falsy" values, so we can determine the criteria
# under which a string can be considered as a boolean value.
_FALSY_VALUES = {'false', 'f', 'no', 'n', 'off', '0'}
_BOOL_VALUES = TRUTHY_VALUES | _FALSY_VALUES
# Valid types for JSON contents; this can be either a list of any type,
# or a dictionary with `string` keys and values of any type.
JSONBlobType = Union[JSONList, JSONObject]
PyDataTypeOrSeq = Union['PyDataType', Sequence['PyDataType']]
TypeContainerElements = Union[PyDataTypeOrSeq,
'PyDataclassGenerator', 'PyListGenerator']
@dataclass
class PyCodeGenerator:
"""
This is the main class responsible for generating Python code that
leverages dataclasses, given a JSON object as an input data.
"""
# Either the file name (ex. file1.json) or the file contents as a string
# can be passed in as an input to the constructor method.
file_name: InitVar[str] = None
file_contents: InitVar[str] = None
# Should we force-resolve inferred types for strings? For example, a value
# of "TRUE" will appear as a `Union[str, bool]` type by default.
force_strings: InitVar[bool] = None
# Enable experimental features via a `__future__` import, which allows
# PEP-585 and PEP-604 style annotations in Python 3.7+
experimental: InitVar[bool] = None
# The rest of these fields are just for internal use.
parser: 'JSONRootParser' = field(init=False)
data: JSONBlobType = field(init=False)
_py_code_lines: List[str] = field(default=None, init=False)
def __post_init__(self, file_name: str, file_contents: str,
force_strings: bool, experimental: bool):
# Set global flags
global Globals
Globals = _Globals(force_strings=force_strings,
experimental=experimental)
# https://stackoverflow.com/a/62940588/10237506
if file_name:
file_path = Path(file_name)
file_contents = file_path.read_bytes()
self.data = json.loads(file_contents)
self.parser = JSONRootParser(self.data)
@property
def py_code(self) -> str:
if self._py_code_lines is None:
# Generate Python code for the dataclass(es)
dataclass_code: str = repr(self.parser)
# Add any imports used at the top of the code
self._py_code_lines = ModuleImporter.imports
if self._py_code_lines:
self._py_code_lines.append('')
# Generate final Python code - imports + dataclass(es)
self._py_code_lines.append(dataclass_code)
return '\n'.join(self._py_code_lines)
# Global flags (generally passed in via command-line) which are shared by
# classes and functions.
Globals: '_Globals | None' = None
@dataclass
class _Globals:
# Should we force-resolve inferred types for strings? For example, a value
# of "TRUE" will appear as a `Union[str, bool]` type by default.
force_strings: bool = False
# Enable experimental features via a `__future__` import, which allows
# PEP-585 and PEP-604 style annotations in Python 3.7+
experimental: bool = False
# Should we insert auto-generated comments under each dataclass.
insert_comments: bool = True
# Should we include a newline after the comments block mentioned above.
newline_after_class_def: bool = True
# Credits: https://github.com/bermi/Python-Inflector
class English:
"""
Inflector for pluralize and singularize English nouns.
This is the default Inflector for the Inflector obj
"""
@staticmethod
def humanize(word):
"""
Returns a human-readable string from word, by replacing
underscores with a space, and by upper-casing the initial
character by default.
"""
return to_snake_case(word).replace('_', ' ').title()
@staticmethod
def singularize(word):
"""Singularizes English nouns."""
rules = [
['(?i)(quiz)zes$', '\\1'],
['(?i)(matr)ices$', '\\1ix'],
['(?i)(vert|ind)ices$', '\\1ex'],
['(?i)^(ox)en', '\\1'],
['(?i)(alias|status)es$', '\\1'],
['(?i)([octop|vir])i$', '\\1us'],
['(?i)(cris|ax|test)es$', '\\1is'],
['(?i)(shoe)s$', '\\1'],
['(?i)(o)es$', '\\1'],
['(?i)(bus)es$', '\\1'],
['(?i)([m|l])ice$', '\\1ouse'],
['(?i)(x|ch|ss|sh)es$', '\\1'],
['(?i)(m)ovies$', '\\1ovie'],
['(?i)(s)eries$', '\\1eries'],
['(?i)([^aeiouy]|qu)ies$', '\\1y'],
['(?i)([lr])ves$', '\\1f'],
['(?i)(tive)s$', '\\1'],
['(?i)(hive)s$', '\\1'],
['(?i)([^f])ves$', '\\1fe'],
['(?i)(^analy)ses$', '\\1sis'],
['(?i)(^analysis)$', '\\1'],
['(?i)((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$', '\\1\\2sis'],
# I don't want 'Data' replaced with 'Datum', however
['(?i)(^data)$', '\\1'],
['(?i)([ti])a$', '\\1um'],
['(?i)(n)ews$', '\\1ews'],
['(?i)s$', ''],
]
uncountable_words = ['equipment', 'information', 'rice', 'money',
'species', 'series', 'fish', 'sheep', 'sms']
irregular_words = {
'people': 'person',
'men': 'man',
'children': 'child',
'sexes': 'sex',
'moves': 'move'
}
lower_cased_word = word.lower()
for uncountable_word in uncountable_words:
if lower_cased_word[-1 * len(uncountable_word):] == uncountable_word:
return word
for irregular in irregular_words.keys():
match = re.search('(' + irregular + ')$', word, re.IGNORECASE)
if match:
return re.sub(
'(?i)' + irregular + '$',
match.expand('\\1')[0] + irregular_words[irregular][1:],
word)
for rule in range(len(rules)):
match = re.search(rules[rule][0], word, re.IGNORECASE)
if match:
groups = match.groups()
for k in range(0, len(groups)):
if groups[k] == None:
rules[rule][1] = rules[
rule][1].replace('\\' + str(k + 1), '')
return re.sub(rules[rule][0], rules[rule][1], word)
return word
# noinspection SpellCheckingInspection, PyPep8Naming
class classproperty:
"""
Decorator that converts a method with a single cls argument into a
property that can be accessed directly from the class.
Credits:
- https://stackoverflow.com/a/57055258/10237506
- https://docs.djangoproject.com/en/3.1/ref/utils/#django.utils.functional.classproperty
"""
def __init__(self, method: Callable[[Any], T]) -> None:
self.f = method
def __get__(
self, instance: Optional[_S], cls: Optional[Type[_S]] = None) -> T:
return self.f(cls)
def getter(self, method):
self.f = method
return self
def is_float(s: str) -> bool:
"""
Check if a string is a :class:`float` value
ex. '1.23'
"""
try:
_ = float(s)
return True
except ValueError:
return False
def can_be_bool(o: str) -> bool:
"""
Check if a string can be a :class:`bool` value. Note this doesn't mean
that the string can or should be converted to bool, only that it *appears*
to be one.
"""
return o.lower() in _BOOL_VALUES
class PyDataType(Enum):
"""
Enum representing a Python Data Type
"""
STRING = str
FLOAT = float
INT = int
BOOL = bool
LIST = list
DICT = dict
DATE = date
DATETIME = datetime
TIME = time
NULL = None
def __str__(self) -> str:
"""
Returns the string representation of an Enum member's value.
"""
return getattr(
self.value, '__name__', str(self.value))
class ModuleImporter:
"""
Helper class responsible for constructing import statements in the
generated Python code.
"""
# Import level (e.g. stdlib or 3rd party) -> Module Name -> Module Imports
_MOD_IMPORTS: DefaultDict[int, DefaultDict[str, Set[str]]] = defaultdict(
lambda: defaultdict(set)
)
# noinspection PyMethodParameters
@classproperty
def imports(cls: Type[T]) -> List[str]:
"""
Returns a list of generated import statements based on the modules
currently used in the code.
"""
lines = []
for lvl in sorted(cls._MOD_IMPORTS):
modules = cls._MOD_IMPORTS[lvl]
for mod in sorted(modules):
imported = sorted(modules[mod])
lines.append(f'from {mod} import {", ".join(imported)}')
lines.append('')
return lines
@classmethod
def wrap_string_with_import(cls, string: str,
imported: object,
wrap_chars='[]',
register_import=True,
level=1) -> str:
"""
Wraps `string` so it is contained within `imported`. The `wrap_chars`
parameter determines the enclosing characters to use -- defaults to
braces by default, as subscripted type Generics often appear in this
form.
If `register_import` is true (default), an import statement will also
be generated for the `imported` usage, if one needs to be added.
Examples::
>>> ModuleImporter.wrap_string_with_import('int', List)
'List[int]'
"""
module = imported.__module__
name = cls._get_import_name(imported)
start, end = wrap_chars
if register_import:
cls.register_import_by_name(module, name, level)
return f'{name}{start}{string}{end}'
# noinspection PyUnresolvedReferences
@classmethod
def wrap_with_import(cls, deck: PyDeque[str],
imported: object,
wrap_chars='[]',
register_import=True,
level=1) -> None:
"""
Same as :meth:`wrap_string_with_import` above, except this accepts
a list (deque) of strings to be wrapped instead.
"""
module = imported.__module__
name = cls._get_import_name(imported)
start, end = wrap_chars
if register_import:
cls.register_import_by_name(module, name, level)
deck.appendleft(start)
deck.appendleft(name)
deck.append(end)
@classmethod
def register_import(cls, imported: object, level=1) -> None:
"""
Registers a new import for the given object.
Examples::
>>> ModuleImporter.register_import(datetime)
"""
module = imported.__module__
name = cls._get_import_name(imported)
cls.register_import_by_name(module, name, level)
@classmethod
def register_import_by_name(cls, module: str, name: str, level: int) -> None:
"""
Registers a new import for a module and the imported name.
Note: any built-in's like "int" or "min" should be skipped by
default.
"""
# Skip any built-in helper functions
# if name in __builtins__.__dict__:
if module == 'builtins':
return
cls._MOD_IMPORTS[level][module].add(name)
@classmethod
def register_future_import(cls, name: str) -> None:
"""
Registers a top-level `__future__` import for a module, which is
required to be the first import defined at the top of the file.
"""
cls._MOD_IMPORTS[0]['__future__'].add(name)
@classmethod
def clear_imports(cls):
"""
Clears all the module imports currently in the cache.
"""
cls._MOD_IMPORTS.clear()
@classmethod
def _get_import_name(cls, imported: Any) -> str:
"""Retrieves the name of an imported object."""
return cls._safe_get_class_name(imported)
@staticmethod
def _safe_get_class_name(cls: Any):
"""
Retrieves the class name of the specified object or class.
Note: the `_name` attribute is specific to most Generic types in
the `typing` module.
"""
try:
return cls._name
except AttributeError:
# Useful to strip underscores from the start, for example
# in Python 3.6 which doesn't have a `_name` attribute for the
# `Union` type, and the class name is returned as `_Union`.
return get_class_name(cls).lstrip('_')
@dataclass(repr=False)
class TypeContainer(List[TypeContainerElements]):
"""
Custom list class which functions as a container for Python data types.
"""
# This keeps track of whether we've seen a `null` type before.
is_optional = False
def append(self, o: TypeContainerElements):
"""
Appends an object (or a sequence of objects) to the
:class:`TypeContainer` instance.
"""
if isinstance(o, Iterable):
for elem in o:
self.append(elem)
return
if o is PyDataType.NULL:
self.is_optional = True
return
if o in self:
return
if isinstance(o, PyDataType):
# Register the types in case they are not standard imports.
# For example, `uuid` and `datetime` objects.
ModuleImporter.register_import(o.value)
super(TypeContainer, self).append(o)
def __or__(self, other):
"""
Performs logical OR, to merge instances of :class:`TypeContainer`
"""
if not isinstance(other, TypeContainer):
raise TypeError(
f'TypeContainer: incorrect type for __add__: {type(other)}')
# Remember to carry over the `is_optional` flag
self.is_optional |= other.is_optional
if len(self) == 1 and len(other) == 1:
self_item = self[0]
other_item = other[0]
for typ in PyDataclassGenerator, PyListGenerator:
if isinstance(self_item, typ) and isinstance(other_item, typ):
# We call `__or__` to merge the lists or dataclasses
# together.
self_item |= other_item
return self
for elem in other:
self.append(elem)
return self
def __repr__(self):
"""
Iteratively calls the `repr` method of all our model collection types.
"""
lines = []
for typ in self:
if isinstance(typ, (PyDataclassGenerator, PyListGenerator)):
lines.append(repr(typ))
return '\n'.join(lines)
def __str__(self):
...
def _default_str(self):
"""
Return the string representation of the resolved type -
ex.`Optional[Union[str, int]]`
"""
# I'm using `deque`s here to avoid doing `list.insert(0, x)` or later
# iterating over `reversed(list)`, as this might be a bit faster.
# noinspection PyUnresolvedReferences
typing_imports: PyDeque[object] = deque()
# noinspection PyUnresolvedReferences
parts: PyDeque[str]
if not self:
# This is the case when the only value encountered for a field is
# a `null` - hence, we're unable to determine the type.
typing_imports.appendleft(Any)
elif self.is_optional:
typing_imports.appendleft(Optional)
if len(self) > 1:
# Else, if we have more than one type for a field, then the
# resolved type should be a `Union` of all the seen types.
typing_imports.appendleft(Union)
parts = deque(', '.join(str(typ) for typ in self))
for tp in typing_imports:
ModuleImporter.wrap_with_import(parts, tp)
return ''.join(parts).replace('[]', '')
def _experimental_features_str(self):
if not self:
# This is the case when the only value encountered for a field is
# a `null` - hence, we're unable to determine the type.
ModuleImporter.register_import(Any)
return 'Any'
parts = [str(typ) for typ in self]
if self.is_optional:
parts.append('None')
return ' | '.join(parts)
def possible_types_for_string_value(string: str) -> PyDataTypeOrSeq:
"""
Returns possible types for a JSON field with a :class:`string` value,
depending on what that value appears to be.
If `Globals.force_strings` is true and there is more than one possible
type, we simply return the inferred type, instead of the
`Union[T..., str]` syntax.
"""
exc_types = TypeError, ValueError
try:
_ = as_date(string)
return PyDataType.DATE
except exc_types:
pass
# I want to eliminate false positives so this seems the easiest
# way to do that. Otherwise strings like "24" seem to get parsed
# as a :class:`Time` object, which might not be expected.
if ':' not in string:
possible_types = []
if string.isnumeric():
possible_types.append(PyDataType.INT)
elif is_float(string):
possible_types.append(PyDataType.FLOAT)
elif can_be_bool(string):
possible_types.append(PyDataType.BOOL)
# If force-resolve is enabled, just return the inferred type if one
# was determined.
# noinspection PyUnresolvedReferences
if Globals.force_strings and possible_types:
return possible_types[0]
possible_types.append(PyDataType.STRING)
return possible_types
try:
_ = as_time(string)
return PyDataType.TIME
except exc_types:
pass
try:
_ = as_datetime(string)
return PyDataType.DATETIME
except exc_types:
pass
return PyDataType.STRING
def json_to_python_type(o: JSONValue) -> PyDataTypeOrSeq:
"""
Convert a JSON object to a Python Data Type, or a Union of Python Data
Types.
"""
if o is None:
return PyDataType.NULL
if isinstance(o, str):
return possible_types_for_string_value(o)
# `bool` needs to come before `int`, as it's a subclass of `int`
if isinstance(o, bool):
return PyDataType.BOOL
if isinstance(o, int):
return PyDataType.INT
if isinstance(o, float):
return PyDataType.FLOAT
if isinstance(o, list):
return PyDataType.LIST
if isinstance(o, dict):
return PyDataType.DICT
@dataclass
class JSONRootParser:
data: JSONBlobType
model: Union['PyListGenerator',
'PyDataclassGenerator'] = field(init=False)
def __post_init__(self):
# Clear imports from last run
ModuleImporter.clear_imports()
str_method_prefix = 'default'
# Check if experimental features are enabled
if Globals.experimental:
# Add the required `__future__` import
ModuleImporter.register_future_import('annotations')
# Update how annotations are resolved
str_method_prefix = 'experimental_features'
# Set the `__str__` method to use for classes
str_method_name = f'_{str_method_prefix}_str'
for typ in TypeContainer, PyListGenerator, PyDataclassGenerator:
typ.__str__ = getattr(typ, str_method_name)
# We'll need an import for the @dataclass decorator, at a minimum
ModuleImporter.register_import(dataclass)
if isinstance(self.data, list):
self.model = PyListGenerator(self.data,
is_root=True)
elif isinstance(self.data, dict):
self.model = PyDataclassGenerator(self.data,
is_root=True)
else:
raise TypeError(
'Incorrect type, expected a JSON `list` or `dict`. '
f'actual_type={type(self.data)!r}, data={self.data!r}')
def __repr__(self):
return repr(self.model) + '\n'
@dataclass
class PyDataclassGenerator(metaclass=property_wizard):
data: InitVar[JSONObject]
_name: str = 'data'
indent: str = ' ' * 4
is_root: bool = False
nested_lvl: InitVar[int] = 0
parsed_types: DefaultDict[str, TypeContainer] = field(
init=False,
default_factory=lambda: defaultdict(TypeContainer)
)
@property
def name(self):
return self._name
@name.setter
def name(self, name: str):
"""Title case the name"""
self._name = to_pascal_case(name)
@classmethod
def load_parsed(
cls: Type[T],
parsed_types: Dict[str,
Union[PyDataType, 'PyDataclassGenerator']],
**constructor_kwargs
) -> T:
obj = cls({}, **constructor_kwargs)
for k, typ in parsed_types.items():
underscored_field = to_snake_case(k)
obj.parsed_types[underscored_field].append(typ)
return obj
def __post_init__(self, data: JSONObject, nested_lvl: int):
for k, v in data.items():
underscored_field = to_snake_case(k)
typ = json_to_python_type(v)
if typ is PyDataType.DICT:
typ = PyDataclassGenerator(
v, k,
nested_lvl=nested_lvl,
)
elif typ is PyDataType.LIST:
nested_lvl += 1
typ = PyListGenerator(
v, k, k,
nested_lvl=nested_lvl,
)
self.parsed_types[underscored_field].append(typ)
def __or__(self, other):
if not isinstance(other, PyDataclassGenerator):
raise TypeError(
f'{self.__class__.__name__}: Incorrect type for `__or__`. '
f'actual_type: {type(other)}, object={other}')
for k, v in other.parsed_types.items():
if k in self.parsed_types:
self.parsed_types[k] |= v
else:
self.parsed_types[k] = v
return self
def get_lines(self) -> List[str]:
if self.is_root:
ModuleImporter.register_import_by_name(
PACKAGE_NAME, 'JSONWizard', level=2)
class_name = f'class {self.name}(JSONWizard):'
else:
class_name = f'class {self.name}:'
class_parts = ['@dataclass',
class_name]
parts = []
nested_parts = []
# noinspection PyUnresolvedReferences
if Globals.insert_comments:
class_parts.append(
textwrap.indent('"""', self.indent))
class_parts.append(
textwrap.indent(f'{self.name} dataclass', self.indent))
# noinspection PyUnresolvedReferences
if Globals.newline_after_class_def:
class_parts.append('')
class_parts.append(textwrap.indent(
'"""', self.indent))
for k, v in self.parsed_types.items():
line = f'{k}: {v}'
wrapped_line = textwrap.indent(line, self.indent)
parts.append(wrapped_line)
nested_part = repr(v)
if nested_part:
nested_parts.append(nested_part)
for part in nested_parts:
parts.append('\n')
parts.append(part)
if not parts:
parts = [textwrap.indent('pass', self.indent)]
class_parts.extend(parts)
return class_parts
def __str__(self):
...
def _default_str(self):
return f"'{self.name}'"
def _experimental_features_str(self):
return self.name
def __repr__(self):
"""
Returns the Python `dataclasses` representation of the object.
"""
return '\n'.join(self.get_lines())
@dataclass(repr=False)
class PyListGenerator(metaclass=property_wizard):
"""
Parse a list in a JSON object to a Python list, based on the following
rules:
* If the JSON list contains *only* simple types, for example int,
str, or bool, then invoking ``str()`` on this object should return
a Union representation of those types, for example
`Union[int, str, bool]`.
* If the JSON list contains *any* complex type, like a dict, then
all `dict`s should have their keys and values merged together.
Optional and Union should be included if needed.
Additionally, if `is_root` is true, then calling ``str()`` will
effectively ignore any simple types,
"""
# Default name for model class if none is provided.
default_name: ClassVar[str] = 'data'
data: JSONList
container_name: str = 'container'
_name: str = None
indent: str = ' ' * 4
is_root: InitVar[bool] = False
nested_lvl: InitVar[int] = 0
root: PyDataclassGenerator = field(init=False, default=None)
parsed_types: TypeContainer = field(init=False,
default_factory=TypeContainer)
# Model is our model dataclass object, which may or may not be present
# in the list. If there are multiple models (i.e. dicts), their keys
# and the associated type defs should be merged into one model.
model: PyDataclassGenerator = field(init=False, default=None)
@property
def name(self):
return self._name
@name.setter
def name(self, name: Optional[str]):
"""Title case and singularize the name."""
if name:
name = English.humanize(name)
name = English.singularize(name).replace(' ', '')
self._name = name
def __post_init__(self, is_root: bool, nested_lvl: int):
if not self.name:
# Increment the suffix if needed
if nested_lvl:
self.name = f'{self.default_name}{nested_lvl}'
else:
self.name = self.default_name
# Temp data dictionary object
data_list = []
for elem in self.data:
typ = json_to_python_type(elem)
if typ is PyDataType.DICT:
typ = PyDataclassGenerator(elem, self.name,
nested_lvl=nested_lvl,
is_root=is_root)
if self.model:
self.model |= typ
continue
self.model = typ
else:
# Nested lists.
if typ is PyDataType.LIST:
nested_lvl += 1
typ = PyListGenerator(elem, nested_lvl=nested_lvl)
data_list.append(typ)
self.parsed_types.append(typ)
if is_root:
# We want to start off by adding the nested `dataclass` field
# first, so it shows up at the top of the container `dataclass`.
data_dict = {self.name: self.model} if self.model else {}
data_dict.update({
f'field_{i + 1}': elem
for i, elem in enumerate(data_list)
})
self.root = PyDataclassGenerator.load_parsed(
data_dict,
nested_lvl=nested_lvl
)
self.root.name = self.container_name
def __or__(self, other):
"""Merge two lists together."""
if not isinstance(other, PyListGenerator):
raise TypeError(
f'{self.__class__.__name__}: Incorrect type for `__or__`. '
f'actual_type: {type(other)}, object={other}')
# To merge lists with equal number of elements, that's easy enough:
# [{"key": "v1"}] | [{"key2": 2}] = [{"key": "v1", "key2": 2}]
#
# But... what happens when it's something like this?
# [1, {"key": "v1"}] | [{"key2": "2}, "testing", 1, 2, 3]
#
# Solution is to merge the model in the other list class with our
# model -- note that both ours and the other instance end up with only
# one model after `__post_init__` runs. However, easiest way is to
# iterate over the nested types in the other list and check for the
# model explicitly. For the rest of the types in the other list
# (including nested lists), we just add them to our current list.
for t in other.parsed_types:
if isinstance(t, PyDataclassGenerator):
if self.model:
self.model |= t
continue
self.model = t
self.parsed_types.append(t)
return self
def get_lines(self) -> List[str]:
lines = []
if self.root:
lines.append(repr(self.root))
else:
if self.model:
lines.append(repr(self.model))
for t in self.parsed_types:
if isinstance(t, PyListGenerator):
code = repr(t)
if code:
# Only if our list already has a dataclass, append
# a newline. This should add the proper number of
# spaces, in a case like below.
# [{"another_Key": "value"}, [{"key": "value"}]]
if self.model:
lines.append('\n')
lines.append(code)
return lines
def __str__(self):
...
def _default_str(self):
if len(self.parsed_types) == 0:
# We could also wrap it with 'Optional' here, since we see it's
# an empty list, but it's probably better to not not do that, as
# 'Optional' generally means the value can be an explicit "null".
#
# return ModuleImporter.wrap_string_with_import('list', Optional)
return ModuleImporter.wrap_string_with_import('', List)
return ModuleImporter.wrap_string_with_import(
str(self.parsed_types), List)
def _experimental_features_str(self):
if len(self.parsed_types) == 0:
return 'list'
return ModuleImporter.wrap_string_with_import(
str(self.parsed_types), list)
def __repr__(self):
"""
Returns the Python `dataclasses` representation of the object.
"""
return '\n'.join(self.get_lines())
if __name__ == '__main__':
loader = PyCodeGenerator('../../tests/testdata/test1.json')
print(loader.py_code)
# Copyright (c) 2006 Bermi Ferrer Martinez
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software to deal in this software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of this software, and to permit
# persons to whom this software is furnished to do so, subject to the following
# condition:
#
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THIS SOFTWARE.
|