summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authordherrada <dylan.herrada@gmail.com>2020-05-14 18:22:07 -0400
committerdherrada <dylan.herrada@gmail.com>2020-05-14 18:22:07 -0400
commita16edbc45ca00b3181bfe68a30a3fd7681393a7f (patch)
tree798c30c9fba2268acd843df9ccbab216560d8fae /tools
parentb477c4812d1f0d0ed0fe56abcdcaa356cc3a652e (diff)
First semi-functional version of extract_types.py
Diffstat (limited to 'tools')
-rw-r--r--tools/extract_types.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/tools/extract_types.py b/tools/extract_types.py
new file mode 100644
index 000000000..2e9b0a496
--- /dev/null
+++ b/tools/extract_types.py
@@ -0,0 +1,80 @@
+import os
+import sys
+import astroid
+import traceback
+
+top_level = sys.argv[1].strip("/")
+
+if top_level.count("/") == 1:
+ top_level, module = top_level.split("/")
+ modules = [module]
+else:
+ modules = os.listdir(top_level)
+ modules = sorted(modules)
+
+ok = 0
+total = 0
+for module in modules:
+ module_path = os.path.join(top_level, module)
+ if not os.path.isdir(module_path):
+ continue
+ pyi_lines = []
+ classes = os.listdir(module_path)
+ classes = [x for x in sorted(classes) if x.endswith(".c")]
+ if classes and classes[-1] == "__init__.c":
+ classes.insert(0, classes.pop())
+ for class_file in classes:
+ class_path = os.path.join(module_path, class_file)
+ with open(class_path, "r") as f:
+ for line in f:
+ if line.startswith("//|"):
+ if line[3] == " ":
+ line = line[4:]
+ elif line[3] == "\n":
+ line = line[3:]
+ else:
+ continue
+ pyi_lines.append(line)
+
+ raw_stubs = [x for x in sorted(classes) if x.endswith(".pyi")]
+ if raw_stubs and raw_stubs[-1] == "__init__.pyi":
+ raw_stubs.insert(0, raw_stubs.pop())
+ for raw_stub in raw_stubs:
+ raw_stub_path = os.path.join(module_path, raw_stub)
+ with open(raw_stub_path, "r") as f:
+ pyi_lines.extend(f.readlines())
+ stub_contents = "".join(pyi_lines)
+
+ # Validate that the module is a parseable stub.
+ total += 1
+ try:
+ tree = astroid.parse(stub_contents)
+ #print(tree.repr_tree())
+ for i in tree.body:
+ for j in i.body:
+ if isinstance(j, astroid.scoped_nodes.FunctionDef):
+ argdict = j.args.__dict__
+ a = argdict.pop('lineno')
+ a = argdict.pop('col_offset')
+ a = argdict.pop('parent')
+ print(argdict)
+ if j.returns:
+ returndict = j.returns.__dict__
+ a = returndict.pop('lineno')
+ a = returndict.pop('col_offset')
+ a = returndict.pop('parent')
+ print(returndict)
+ print('\n')
+ #print(tree.body[0].body[0])
+ else:
+ print(type(j))
+ ok += 1
+ except astroid.exceptions.AstroidSyntaxError as e:
+ e = e.__cause__
+ traceback.print_exception(type(e), e, e.__traceback__)
+ print()
+
+print(f"{ok} ok out of {total}")
+
+if ok != total:
+ sys.exit(total - ok)