-
Notifications
You must be signed in to change notification settings - Fork 11
/
__init__.py
791 lines (660 loc) · 22.4 KB
/
__init__.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
"""
Generate the type structure based on the Type class from the JSON schema file.
"""
import keyword
import random
import re
import textwrap
import unicodedata
from typing import Any, Optional, Union, cast
import yaml
from jsonschema_gentypes import jsonschema_draft_04, jsonschema_draft_2019_09_meta_data
def __pinyin(char: str) -> str:
try:
import pinyin # pylint: disable=import-outside-toplevel
except ModuleNotFoundError:
return char
char = pinyin.get(char, delimiter=" ")
return (
char
if len(char) == 1
else "".join([c for c in unicodedata.normalize("NFKD", f" {char} ") if not unicodedata.combining(c)])
)
def __romkan(char: str) -> str:
try:
import romkan # pylint: disable=import-outside-toplevel
except ModuleNotFoundError:
return char
return cast(str, romkan.to_roma(char))
def __greek(char: str) -> str:
try:
import romanize # pylint: disable=import-outside-toplevel
except ModuleNotFoundError:
return char
return cast(str, romanize.romanize(char))
def __char_range(char1: str, char2: str) -> list[str]:
"""
Generate the characters range from `char1` to `char2`, inclusive.
Parameter:
char1: the first char of the range
char2: the last char of the range
"""
return [chr(char) for char in range(ord(char1), ord(char2) + 1)]
AUTHORIZED_CHAR = __char_range("a", "z") + __char_range("A", "Z") + __char_range("0", "9")
def __convert_char(char: str) -> str:
if char in AUTHORIZED_CHAR:
return char
# Remove accents
if unicodedata.combining(char):
return ""
if char == "-":
return " "
category = unicodedata.category(char)
# All spaced => space
if category in ("Zs", "Cc"):
return " "
# Explicit sign
if category in ("So", "Po"):
name = unicodedata.name(char)
if category == "So":
name = name.replace(" SIGN", "")
return f" {name} "
# Greek characters
char = __greek(char)
if char in AUTHORIZED_CHAR or len(char) > 1:
return char
# Japanese characters
char = __romkan(char)
if char in AUTHORIZED_CHAR or len(char) > 1:
return char
# Chinese characters
char = __pinyin(char)
if char in AUTHORIZED_CHAR or len(char) > 1:
return char
return " "
def normalize(input_str: str) -> str:
"""Normalize the string to be a Python name."""
# Unaccent, ...
nfkd_form = unicodedata.normalize("NFKD", input_str)
name = "".join([__convert_char(c) for c in nfkd_form])
# No number at first position
if name[0] in __char_range("0", "9"):
name = f"num {name}"
# No python keyword
if name in keyword.kwlist:
name = f"{name} name"
return name
class Type:
"""
The base Type object.
"""
_comments: Optional[list[str]] = None
_depends_on: Optional[list["Type"]] = None
def __init__(self) -> None:
"""
Initialize the type.
"""
self._depends_on = []
def name(self) -> str:
"""
Return what we need to use the type.
"""
raise NotImplementedError
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
del python_version
return []
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Return the type declaration.
Parameter:
line_length: the maximum line length
"""
del line_length
return []
def depends_on(self) -> list["Type"]:
"""
Return the needed sub types.
"""
assert self._depends_on is not None
return self._depends_on
def add_depends_on(self, depends_on: "Type") -> None:
"""
Add a sub type.
"""
assert self._depends_on is not None
self._depends_on.append(depends_on)
def comments(self) -> list[str]:
"""
Additional comments shared by the type.
"""
if self._comments is None:
self._comments = []
return self._comments
def set_comments(self, comments: list[str]) -> None:
"""
Set comment on the type.
"""
self._comments = comments
class TypeProxy(Type):
"""
A proxy on a type that can be set later.
"""
_type: Optional[Type] = None
def name(self) -> str:
"""
Return what we need to use the type.
"""
assert self._type is not None
return self._type.name()
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
assert self._type is not None
return self._type.imports(python_version)
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Return the type declaration.
Parameter:
line_length: the maximum line length
"""
assert self._type is not None
return self._type.definition()
def depends_on(self) -> list["Type"]:
"""
Return the needed sub types.
"""
assert self._type is not None
return self._type.depends_on()
def add_depends_on(self, depends_on: "Type") -> None:
"""
Add a sub type.
"""
raise NotImplementedError
def comments(self) -> list[str]:
"""
Additional comments shared by the type.
"""
return self._type.comments() if self._type is not None else []
def set_comments(self, comments: list[str]) -> None:
"""
Set comment on the type.
"""
print(f"Warning: set_comments on a TypeProxy, lost comments: {comments}")
def set_type(self, type_: Type) -> None:
"""
Set the type.
"""
self._type = type_
class NamedType(Type):
"""
The based type of named type.
"""
def __init__(self, name: str) -> None:
"""
Init.
Parameter:
name: the type name
"""
super().__init__()
self._name = name
def postfix_name(self, postfix: str) -> None:
"""
Set a new name (Not available every time).
"""
self._name += postfix
def set_name(self, name: str) -> None:
"""
Set a new name (Not available every time).
"""
self._name = name
def unescape_name(self) -> str:
"""
Return the unescaped name.
"""
return self._name
def name(self) -> str:
"""
Return what we need to use the type.
"""
return f'"{self._name}"'
class LiteralType(Type):
"""
A literal type like: `Literal["text"]`.
"""
def __init__(self, const: Union[int, float, bool, str, None, dict[str, Any], list[Any]]) -> None:
"""
Init.
Parameter:
const: the constant
"""
super().__init__()
self.const = const
def name(self) -> str:
"""
Return what we need to use the type.
"""
return f"Literal[{repr(self.const)}]"
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
del python_version
return [("typing", "Literal")]
class BuiltinType(Type):
"""
Python builtin type, e.g.: str.
"""
def __init__(self, name: str) -> None:
"""
Init.
Parameter:
name: the type name
"""
super().__init__()
self._name = name
def name(self) -> str:
"""
Return what we need to use the type.
"""
return self._name
class NativeType(Type):
"""
Native Type that will essentially generates a Python import.
"""
def __init__(
self,
name: str,
package: str = "typing",
minimal_python_version: Optional[tuple[int, ...]] = None,
workaround_package: Optional[str] = None,
) -> None:
"""
Init.
Parameter:
name: the type name
package: the package of the type
minimal_python_version: the minimal Python version to use the type
workaround_package: the package to use if the minimal Python version is not met
"""
super().__init__()
self.package = package
self._name = name
self.minimal_python_version = minimal_python_version
self.workaround_package = workaround_package
def name(self) -> str:
"""
Return what we need to use the type.
"""
return self._name
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
if self.minimal_python_version is not None and python_version < self.minimal_python_version:
assert self.workaround_package is not None
return [(self.workaround_package, self._name)]
return [(self.package, self._name)]
def __repr__(self) -> str:
"""Get the representation of the object."""
return f"NativeType({self.package!r}.{self._name!r})"
class CombinedType(Type):
"""
A combined type.
e.g.: Union[str, int] is an Combined type of `str` and `int` with `Union` as base.
"""
def __init__(self, base: Type, sub_types: list[Type]) -> None:
"""
Init.
Parameter:
base: the base type (e.-g. for `Union[str, int]` the base type is `Union`)
sub_types: the sub types (e.-g. for `Union[str]` the sub types are `str` and `int`)
"""
super().__init__()
self.base = base
self.sub_types = sub_types
def name(self) -> str:
"""
Return what we need to use the type.
"""
assert isinstance(self.base, Type)
return f"{self.base.name()}[{', '.join([sub_type.name() for sub_type in self.sub_types])}]"
def depends_on(self) -> list[Type]:
"""
Return the needed sub types.
"""
return [self.base] + self.sub_types + super().depends_on()
class TypeAlias(NamedType):
"""
An alias on a type, essentially to add a description.
"""
def __init__(self, name: str, sub_type: Type, descriptions: Optional[list[str]] = None):
"""
Init.
Parameter:
name: the type name
sub_type: the type that should be aliased
descriptions: the type description
"""
super().__init__(name)
self.sub_type = sub_type
self._comments = [] if descriptions is None else descriptions
def depends_on(self) -> list[Type]:
"""
Return the needed sub types.
"""
return [self.sub_type] + super().depends_on()
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Return the type declaration.
"""
result = ["", ""]
_type = (
": TypeAlias" if isinstance(self.sub_type, BuiltinType) and self.sub_type.name() == "None" else ""
)
result.append(f"{self._name}{_type} = {self.sub_type.name()}")
comments = split_comment(self.comments(), line_length - 2 if line_length else None)
if len(comments) == 1:
result += [f'""" {comments[0]} """', ""]
elif comments:
result += ['"""', *comments, '"""', ""]
return result
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
del python_version
return (
[("typing", "TypeAlias")]
if isinstance(self.sub_type, BuiltinType) and self.sub_type.name() == "None"
else []
)
class TypeEnum(NamedType):
"""
The Type that represent an Enum in Python.
"""
def __init__(self, name: str, values: list[Union[int, float, bool, str, None]], descriptions: list[str]):
"""
Init.
Parameter:
name: the type name
values: the values of the enum
descriptions: the type description
"""
assert len(values) > 0
super().__init__(name)
self.values = values
self.value_names = {value: get_name({"title": f"{name} {value}"}, upper=True) for value in values}
self.descriptions = descriptions
self.sub_type: Type = CombinedType(NativeType("Union"), [LiteralType(value) for value in values])
def depends_on(self) -> list["Type"]:
"""
Return the needed sub types.
"""
return [self.sub_type] + super().depends_on()
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Return the type declaration.
"""
result = ["", ""]
comments = split_comment(self.descriptions, line_length - 2 if line_length else None)
result.append(f"{self._name} = {self.sub_type.name()}")
if len(comments) == 1:
result += [f'""" {comments[0]} """']
elif comments:
result += ['"""', *comments, '"""']
for value in self.values:
name = self.value_names[value]
formatted_value = f'"{value}"' if isinstance(value, str) else str(value)
result.append(f"{name}: {LiteralType(value).name()} = {formatted_value}")
name = self.descriptions[0] if self.descriptions else self._name
if name.endswith("."):
name = name[:-1]
result.append(f'"""The values for the \'{name}\' enum"""')
result.append("")
return result
class TypedDictType(NamedType):
"""
The Type that represent a TypedDict in Python.
"""
def __init__(
self,
name: str,
struct: dict[str, Type],
descriptions: list[str],
required: set[str],
):
"""
Init.
Parameter:
name: name of the type
struct: the struct of the subtypes
descriptions: the description
required: the required properties names
"""
super().__init__(name)
self.descriptions = descriptions
def get_required_type(prop_type: Type) -> Type:
result = CombinedType(NativeType("Required", "typing", (3, 11), "typing_extensions"), [prop_type])
if prop_type.comments():
result.set_comments([*prop_type.comments(), "", "Required property"])
else:
result.set_comments(["Required property"])
return result
self.struct = {
name: get_required_type(prop_type) if name in required else prop_type
for name, prop_type in struct.items()
}
def depends_on(self) -> list[Type]:
"""
Get the types that we requires to be valid.
"""
result: list[Type] = [NativeType("TypedDict")]
result += self.struct.values()
return result + super().depends_on()
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Get the definition based on a dict.
"""
supported_re = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
# Support to be a class
supported = True
for property_ in self.struct.keys():
if not supported_re.match(property_) or property_ in keyword.kwlist:
supported = False
break
result = ["", ""]
if supported:
result.append(f"class {self._name}(TypedDict, total=False):")
comments = split_comment(self.descriptions, line_length - 2 if line_length else None)
if len(comments) == 1:
result.append(f' """ {comments[0]} """')
result.append("")
elif comments:
result.append(' """')
result += [f" {d}" if d else "" for d in comments]
result.append(' """')
result.append("")
for property_, type_obj in self.struct.items():
result.append(f" {property_}: {type_obj.name()}")
comments = type_obj.comments()
if len(comments) == 1:
result.append(f' """ {comments[0]} """')
result.append("")
elif comments:
result.append(' """')
result += [f" {comment}" if comment else "" for comment in comments]
result.append(' """')
result.append("")
else:
result += [
"# | " + d for d in split_comment(self.descriptions, line_length - 2 if line_length else None)
]
result.append(f"{self._name} = TypedDict('{self._name}', " + "{")
for property_, type_obj in self.struct.items():
result += [f" # | {comment}" for comment in type_obj.comments()]
result.append(f" '{property_}': {type_obj.name()},")
result.append("}, total=False)")
return result
class Constant(NamedType):
"""
The Pseudo Type is used to add the default constants.
"""
def __init__(self, name: str, constant: Any, descriptions: list[str]):
"""
Init.
Parameter:
name: the type name
constant: the constant value
descriptions: the type description
"""
super().__init__(name)
self.constant = constant
self.descriptions = descriptions
def definition(self, line_length: Optional[int] = None) -> list[str]:
"""
Return the type declaration.
"""
result = ["", ""]
if isinstance(self.constant, dict) and not self.constant:
result.append(f"{self._name}: Dict[str, Any] = {repr(self.constant)}")
elif isinstance(self.constant, (dict, list)) and not self.constant:
result.append(f"{self._name}: List[Any] = {repr(self.constant)}")
else:
result.append(f"{self._name} = {repr(self.constant)}")
comments = split_comment(self.descriptions, line_length - 2 if line_length else None)
if len(comments) == 1:
result += [f'""" {comments[0]} """', ""]
elif comments:
result += ['"""', *comments, '"""', ""]
return result
def imports(self, python_version: tuple[int, ...]) -> list[tuple[str, str]]:
"""
Return the needed imports.
"""
del python_version
if isinstance(self.constant, dict) and not self.constant:
return [("typing", "Any"), ("typing", "Dict")]
elif isinstance(self.constant, list) and not self.constant:
return [("typing", "Any"), ("typing", "List")]
else:
return []
def split_comment(text: list[str], line_length: Optional[int]) -> list[str]:
"""
Split the text at line length.
Parameter:
text: the lines to split
line_length: the maximum line length
"""
if not line_length:
return text
result = []
for line in text:
# Don't remove empty lines
if line:
result += textwrap.wrap(line, width=line_length, break_long_words=False)
else:
result.append(line)
return result
def get_name(
schema: Optional[
Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
]
],
proposed_name: Optional[str] = None,
upper: bool = False,
get_name_properties: Optional[str] = None,
postfix: Optional[str] = None,
) -> str:
"""
Get the name for an element.
Parameter:
schema: the concerned schema
proposed_name: a name that we will use it the schema hasn't any title
upper: should we use an upper case (For constants)
"""
# Get the base name
has_title = isinstance(schema, dict) and "title" in schema
name = schema["title"] if has_title else proposed_name # type: ignore
assert name is not None
name = normalize(name)
prefix = "" if has_title else "_"
rand = str(random.randint(0, 9999)) if name != "Root" else "" # nosec
if upper:
# Upper case
name = name.upper()
# Remove spaces
output = prefix + "".join(["_" if char.isspace() else char for char in name])
elif get_name_properties == "UpperFirst":
# Change just the first letter to upper case
name = name[0].upper() + name[1:]
# Remove spaces
output = prefix + "".join([char for char in name if not char.isspace()])
else:
# Title case
name = name.title()
# Remove spaces
output = prefix + "".join([char for char in name if not char.isspace()])
if postfix:
output += postfix
if not get_name.__dict__.get("names"):
get_name.__dict__["names"] = set()
elif output in get_name.__dict__["names"]:
output += rand
get_name.__dict__["names"].add(output)
return output
def get_description(
schema: Union[
jsonschema_draft_04.JSONSchemaD4,
jsonschema_draft_2019_09_meta_data.JSONSchemaItemD2019,
]
) -> list[str]:
"""
Get the standard description for an element.
Parameter:
schema: the concerned schema
"""
result: list[str] = []
if "title" in schema:
result.append(f"{schema['title']}.")
schema.setdefault("used", set()).add("title") # type: ignore[typeddict-item]
if "description" in schema:
schema.setdefault("used", set()).add("description") # type: ignore[typeddict-item]
if result:
result.append("")
result += schema["description"].split("\n")
first = True
used = cast(set[str], schema.get("used", set()))
used = {
*used,
"$schema",
"$id",
"type",
"used",
"required",
"$defs",
"definitions",
"properties",
}
for key, value in schema.items():
if key not in used:
if not isinstance(value, (list, dict)):
if first:
if result:
result.append("")
first = False
result.append(f"{key}: {value}")
else:
if first:
if result:
result.append("")
first = False
result.append(f"{key}:")
lines = yaml.dump(value, Dumper=yaml.SafeDumper).split("\n")
result += [f" {line}" for line in lines if line]
return result