forked from ontology-tools/py-horned-owl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_pyi.py
113 lines (83 loc) · 3.5 KB
/
gen_pyi.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
import typing
import pyhornedowl.pyhornedowl as pho
import os
import re
os.makedirs("pyhornedowl/model", exist_ok=True)
with open("pyhornedowl/__init__.py", "w") as f:
pyhornedowl_members = [k for k, v in pho.__dict__.items() if isinstance(v, type) or callable(v)]
f.write("from __future__ import annotations\n")
f.write("from .pyhornedowl import ")
f.write(", ".join(pyhornedowl_members))
f.write("\n\n")
f.write("__all__ = [")
f.write(", ".join([f'"{n}"' for n in pyhornedowl_members]))
f.write("]\n")
with open("pyhornedowl/__init__.pyi", "w") as f:
f.write("import typing\nfrom typing import *\n\n")
f.write("import model\n")
f.write("\n")
for name, entry in pho.__dict__.items():
if isinstance(entry, type):
f.write(f"class {name}:\n")
for member_name, member in entry.__dict__.items():
if member_name.startswith("_"):
continue
if hasattr(member, "__doc__"):
doc = member.__doc__
if doc is not None:
lines = doc.splitlines()
if len(lines) > 2:
sign = lines[0]
f.write(f" def {sign}:\n")
doc = "\n".join([f" {l}" for l in lines[2:]])
f.write(f' """\n{doc}\n """\n ...\n\n')
if callable(entry):
if hasattr(entry, "__doc__"):
doc = entry.__doc__
if doc is not None:
lines = doc.splitlines()
if len(lines) > 2:
sign = lines[0]
f.write(f"def {sign}:\n")
doc = "\n".join([f" {l}" for l in lines[2:]])
f.write(f' """\n{doc}\n """\n ..\n\n')
f.write("\n")
with open("pyhornedowl/py.typed", "w"):
pass
with open("pyhornedowl/model/__init__.py", "w") as f:
f.write("from ..pyhornedowl import model\n")
f.write("import typing\n\n")
al = []
for name, entry in pho.model.__dict__.items():
if not isinstance(entry, type) and not type(entry) == typing._UnionGenericAlias:
continue
f.write(f"{name} = model.{name}\n")
al.append(name)
# module_pyi = pyhornedowl.model.__pyi__()
# al += [l.split("=")[0].strip() for l in module_pyi.split("\n") if len(l.strip()) > 0]
# f.write(module_pyi)
f.write("\n")
f.write(f"__all__ = {al}")
with open("pyhornedowl/model/__init__.pyi", "w") as f:
f.write("import typing\n")
f.write("from typing import *\n\n")
for name, entry in pho.model.__dict__.items():
if isinstance(entry, type):
if "__pyi__" in entry.__dict__:
pyi: str = entry.__pyi__()
pyi = re.sub(r" from: .*$|from: .*?[,)]", "", pyi)
f.write(pyi)
else:
f.write(f"class {name}:\n")
for attr_name, attr in entry.__dict__.items():
if attr_name.startswith("_") or attr_name == "from":
continue
f.write(f" {attr_name}: Any\n")
f.write(" ...\n")
elif type(entry) == typing._UnionGenericAlias:
f.write(f"{name} = {str(entry).replace('pyhornedowl.model.', '')}")
else:
continue
f.write("\n")
# f.write(pyhornedowl.model.__pyi__())
f.write("\n")