-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·201 lines (174 loc) · 5.3 KB
/
main.py
File metadata and controls
executable file
·201 lines (174 loc) · 5.3 KB
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
#!/usr/bin/env python3
from typing import Set
from os import getcwd
from os.path import join, exists, isfile, abspath
import glob
import git
from git import Repo
import xml.etree.ElementTree as xml
GODOT_REPO_URL = "https://github.com/godotengine/godot.git"
GODOT_REPO_DIR = join(getcwd(), "godot")
# <https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#keywords>
KEYWORDS: Set[str] = {
"if",
"elif",
"else",
"for",
"while",
"match",
"when",
"break",
"continue",
"pass",
"return",
"class",
"class_name",
"extends",
"is",
"in",
"as",
"self",
"super",
"signal",
"func",
"static",
"const",
"enum",
"var",
"breakpoint",
"preload",
"await",
"yield",
"assert",
"void",
}
# <https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/gdscript_basics.html#built-in-types>
TYPES: Set[str] = {"null", "bool", "int", "float"}
METHODS: Set[str] = set()
FIELDS: Set[str] = set()
SIGNALS: Set[str] = set()
ANNOTATIONS: Set[str] = set()
CONSTANTS: Set[str] = set()
def clone_or_update_godot_repo() -> Repo | None:
repo_dir = GODOT_REPO_DIR
if exists(repo_dir):
try:
repo = Repo(repo_dir)
except git.InvalidGitRepositoryError:
print(f"[!] '{GODOT_REPO_DIR}' doesn't contain a valid repo")
return None
print(f"[-] Using existing repo in '{repo_dir}'")
if repo.active_branch.name != "master":
print("[-] Checking out the 'master' branch...")
head = repo.heads.master.checkout()
assert head.name == "master"
print("[+] Branch checked out")
print("[-] Updating repo...")
repo.remotes.origin.pull("master", depth=1)
print("[+] Repo updated")
else:
print(f"[-] Cloning repo into '{repo_dir}'...")
repo = Repo.clone_from(
GODOT_REPO_URL,
repo_dir,
depth=1,
branch="master",
)
print("[+] Repo cloned")
return repo
def parse_file(filepath: str) -> None:
tree = xml.parse(filepath)
root = tree.getroot()
class_name = root.get("name")
assert class_name is not None
class_name = class_name.strip("@")
TYPES.add(class_name)
methods = root.find("methods")
if methods is not None:
for m in methods.iter("method"):
name = m.get("name")
assert name is not None
METHODS.add(name.strip("_"))
# Members can be either fields or classes.
members = root.find("members")
if members is not None:
for m in members.iter("member"):
name = m.get("name")
type_name = m.get("type")
assert name is not None
assert type_name is not None
if name == type_name:
TYPES.add(type_name)
else:
FIELDS.update([w.strip("_") for w in name.split("/") if len(w) > 2])
enum = m.get("enum")
if enum is not None:
TYPES.update(enum.split("."))
getter = m.get("getter")
if getter is not None and len(getter) > 0:
METHODS.add(getter)
setter = m.get("setter")
if setter is not None and len(setter) > 0:
METHODS.add(setter)
signals = root.find("signals")
if signals is not None:
for s in signals.iter("signal"):
name = s.get("name")
assert name is not None
SIGNALS.add(name)
constants = root.find("constants")
if constants is not None:
for c in constants.iter("constant"):
name = c.get("name")
assert name is not None
CONSTANTS.add(name)
enum = c.get("enum")
if enum is not None:
TYPES.update(enum.split("."))
annotations = root.find("annotations")
if annotations is not None:
for a in annotations.iter("annotation"):
name = a.get("name")
assert name is not None
name = name.strip("@")
ANNOTATIONS.add(name)
def main():
repo = clone_or_update_godot_repo()
if repo is None:
return
files: Set[str] = {
abspath(f)
for f in [
*glob.glob("./godot/doc/classes/*.xml"),
*glob.glob("./godot/modules/**/doc_classes/*.xml", recursive=True),
]
if isfile(f)
}
print(f"[-] Found {len(files)} files to parse")
print("[-] Parsing files...")
for file in files:
parse_file(file)
print("[+] Files parsed")
print()
print(f"[-] Found {len(KEYWORDS)} keywords")
print(f"[-] Found {len(TYPES)} types")
print(f"[-] Found {len(METHODS)} methods")
print(f"[-] Found {len(FIELDS)} fields")
print(f"[-] Found {len(SIGNALS)} signals")
print(f"[-] Found {len(ANNOTATIONS)} annotations")
print(f"[-] Found {len(CONSTANTS)} constants")
words = set()
words.update(
KEYWORDS,
TYPES,
METHODS,
FIELDS,
SIGNALS,
ANNOTATIONS,
CONSTANTS,
)
with open("gdscript.txt", "w") as fd:
fd.write("# Automatically generated word list. Do NOT modify manually.\n")
fd.writelines([f"{word}\n" for word in sorted(words)])
if __name__ == "__main__":
main()