-
Notifications
You must be signed in to change notification settings - Fork 9
/
stubs.py
118 lines (106 loc) · 3.48 KB
/
stubs.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
import os
import sys
import time
from inspect import signature
from textwrap import dedent
INCLUDED_PRIVATE_MEMBERS = ["__init__", "__call__", "_words"]
talon_modules = [
m
for m in sys.modules.keys()
if m.startswith("talon.") or m.startswith("talon_plugins.")
]
# talon_modules = ["talon.clip"]
# talon_plugin_modules = [m for m in sys.modules.keys() if m.startswith("talon_plugins.")]
# print(talon_plugin_modules)
def dump_stubs(mod, indent=""):
output = []
for identifier in dir(mod):
# print(mod, id)
if identifier.startswith("_") and identifier not in INCLUDED_PRIVATE_MEMBERS:
continue
# noinspection PyBroadException
try:
thing = getattr(mod, identifier)
except:
continue
if callable(thing):
output.append(stub_callable(identifier, thing, indent))
else:
type_name = type(thing).__name__
if type_name == "module" or identifier.startswith("_"):
continue
# noinspection PyBroadException
try:
output.append(f"{identifier}: {type_name} = ...")
except:
# print(e)
output.append(f"{identifier} = ...")
if len(output) == 0:
return "..."
return f"\n{indent}" + f"\n{indent}".join(
[
o.replace("<no value>", '"unknown value"')
.replace("NoneType", "Any")
.replace("<class ", "")
.replace("'>", "'")
for o in output
]
)
def stub_callable(identifier, thing, indent):
if callable(thing):
if not isinstance(thing, type):
# noinspection PyBroadException
try:
return f"def {identifier}{str(signature(thing))}: ..."
except:
return f"def {identifier}(*args, **kwargs) -> Any: ..."
else:
return f"class {identifier}: " + dump_stubs(thing, indent=indent + " ")
STUBS_DIR = os.path.expanduser("~/.talon/user")
def dump_all_stubs():
os.makedirs(STUBS_DIR, exist_ok=True)
super_import = (
"\n".join(
[
f"from {t} import *"
for t in talon_modules + ["talon.stubbed"]
if t != "talon.voice"
]
)
+ "\n"
)
for m in talon_modules:
name = ".".join(m.split("."))
if "." in name:
path_join = os.path.join(STUBS_DIR, "/".join(name.split(".")[:-1]))
os.makedirs(path_join, exist_ok=True)
output_path = os.path.join(STUBS_DIR, name.replace(".", "/"))
with open(output_path + ".pyi", "w") as fh:
stubs = dump_stubs(sys.modules[m])
fh.write("from typing import *\n")
# fh.write(super_import)
fh.write(stubs)
with open(os.path.join(STUBS_DIR, "talon", "stubbed.pyi"), "w") as fh:
stub = dedent("""\
from typing import *
CompiledLib: Any = ...
CompiledFFI: Any = ...
class _cffi_backend:
CData: Any = ...
getset_descriptor: Any = ...
""")
fh.write(
stub
)
with open(os.path.join(STUBS_DIR, "talon", "__init__.pyi"), "w") as fh:
fh.write(
dedent(
"""\
from typing import *
"""
)
)
print("Done stubbing.")
# if time.time() - os.path.getmtime(os.path.join(STUBS_DIR, "talon", "__init__.pyi")) > 3600:
# dump_all_stubs()
# dump_all_stubs()