-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubgen.py
146 lines (110 loc) · 4.13 KB
/
stubgen.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
from typing import (
Any, Type, Optional,
ForwardRef, get_args
)
from dataclasses import dataclass
import re
class StubGeneratorException(Exception):
pass
@dataclass
class ClassRoot:
code: str
depends: list[str]
class StubGenerator:
def __init__(self):
self.definitions: dict[str, ClassRoot] = {}
self.imports: set[str] = set(['typing'])
def generate_root(self, root: dict[str, Any], first: bool = True) -> None:
name: Optional[str]
if not first:
name = root.pop('__name__')
requires: list[str] = []
output: list[str] = []
for key, sroot in root.items():
if isinstance(sroot, dict):
self.generate_root(sroot, False)
continue
output.append(self.generate_param(
key, sroot, requires))
if not first:
self.definitions[name] = ClassRoot(
'\n'.join(output), requires)
def _genparam_routine(self, params: list[str | Type], requires: list[str]):
for param in params:
if isinstance(param, list | tuple):
self._genparam_routine(param, requires)
continue
if isinstance(param, ForwardRef):
param = param.__forward_arg__
if isinstance(param, str):
requires.append(param)
continue
if str(param).startswith('typing.'):
self._genparam_routine(get_args(param), requires)
continue
if hasattr(param, '__module__'):
self.imports.add(param.__module__)
def generate_param(
self, key: str, param: str | Type | ForwardRef,
requires: list[str]
) -> str:
if isinstance(param, ForwardRef):
param = param.__forward_arg__
if isinstance(param, str):
requires.append(param)
return f'{key}: {param}'
if str(param).startswith('<'):
return f'{key}: {param.__name__}'
if str(param).startswith('typing.'):
self._genparam_routine(get_args(param), requires)
else:
self.imports.add(param)
return f'{key}: {param}'
def _tocode_routine(
self, name: str, defi: ClassRoot, code: list[str],
imported: list[str], importing: list[str]
) -> str:
importing.append(name)
for dep in defi.depends:
if dep.startswith('stub.'):
dep = dep[5:]
if dep in imported:
continue
if dep in importing:
raise StubGeneratorException(
'Cyclical reference detected when generating stub`s')
self._tocode_routine(
dep, self.definitions[dep],
code, imported, importing)
code.append(f'class {name}:\n\t{"\n\t".join(defi.code.split("\n"))}')
importing.remove(name)
imported.append(name)
def to_code(self) -> str:
code: list[str] = []
imported: list[str] = []
importing: list[str] = []
for k, defi in self.definitions.items():
if k not in imported:
self._tocode_routine(
k, defi, code,
imported, importing)
out: str
if not self.imports:
out = '\n\n'.join(code) + '\n'
else:
out = '\n'.join([f'import {x}' for x in self.imports]) + \
'\n\n' + '\n\n'.join(code) + '\n'
m: re.Match = re.search(r'ForwardRef\(\'\w*\'\)', out)
while m:
sp = m.span()
piece: str = out[sp[0] + 12:sp[1] - 2]
if piece.startswith('stub.'):
piece = piece[5:]
out = out[:sp[0]] + piece + out[sp[1]:]
m = re.search(r'ForwardRef\(\'[\w.]*\'\)', out)
return out
def generate(root: dict[str, Any]) -> str:
stub: StubGenerator = StubGenerator()
stub.generate_root(root)
return 'from stubs.client import MetaClient, MetaModule, MainException, SettingsCollision, InvalidSettings\n' + \
stub.to_code().replace('NoneType', 'None')