Skip to content

Commit 13ca8d2

Browse files
add doc strings and fake classes to the type hints
1 parent 3044797 commit 13ca8d2

File tree

2 files changed

+2621
-533
lines changed

2 files changed

+2621
-533
lines changed

create_stub.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from raylib.static import rl, ffi
22

3-
from inspect import ismethod,getmembers,isbuiltin
4-
import inflection
5-
3+
from inspect import ismethod, getmembers, isbuiltin
4+
import inflection, sys
65

76
print("""from typing import Any
87
@@ -23,36 +22,61 @@ def ctype_to_python_type(t):
2322
return "float"
2423
elif "char *" in t:
2524
return "str"
25+
elif "char" in t:
26+
return "str" # not sure about this one
2627
elif "*" in t:
2728
return "Any"
2829
elif t.startswith("struct"):
29-
return "Any" # This should be CData but cant get PyCharm to understand that - it just shows up as None
30+
return t.replace("struct ","")
3031
elif t.startswith("unsigned"):
31-
return t.replace("unsigned ","")
32+
return t.replace("unsigned ", "")
3233
else:
3334
return t
3435

36+
3537
for name, attr in getmembers(rl):
3638
uname = inflection.underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
3739
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>":
3840

39-
4041
sig = ""
4142
for i, arg in enumerate(ffi.typeof(attr).args):
42-
param_name = arg.cname.replace("struct","").replace("char *","str").replace("*","_pointer").replace(" ","")
43+
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
44+
"_pointer").replace(
45+
" ", "")
4346
param_type = ctype_to_python_type(arg.cname)
4447
sig += f", {param_name}_{i}: {param_type}"
4548

46-
4749
return_type = ffi.typeof(attr).result.cname
4850

51+
print(
52+
f' def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}:\n """{attr.__doc__}"""\n ...')
53+
54+
elif str(type(attr)) == "<class '_cffi_backend._CDataBase'>":
55+
return_type = ffi.typeof(attr).result.cname
56+
print(
57+
f' def {uname}(self, *args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
58+
else:
59+
#print("*****", str(type(attr)))
60+
print(f" {name}: {str(type(attr))[8:-2]}") # this isolates the type
4961

50-
print(f" def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}: ...")
62+
for struct in ffi.list_types()[0]:
63+
print("processing", struct, file=sys.stderr)
64+
if ffi.typeof(struct).kind == "struct":
65+
if ffi.typeof(struct).fields is None:
66+
print("weird empty struct, skipping", file=sys.stderr)
67+
break
68+
print(f" class {struct}:")
69+
sig = ""
70+
for arg in ffi.typeof(struct).fields:
71+
sig += ", " + arg[0]
72+
print(f" def __init__(self{sig}):")
5173

74+
for arg in ffi.typeof(struct).fields:
75+
print(f" self.{arg[0]}={arg[0]}")
5276

77+
elif ffi.typeof(struct).kind == "enum":
78+
print(f" {struct}: int")
5379
else:
54-
print(f" {name}: {str(type(attr))[8:-2]}") # this isolates the type
80+
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
5581

5682

57-
for struct in ffi.list_types()[0]:
58-
print(f" {struct}: Any") # This should be CData but cant get PyCharm to understand that - it just shows up as None

0 commit comments

Comments
 (0)