Skip to content

Commit 9a3ffb5

Browse files
update docstrings using raylib_api.json
1 parent a98e0be commit 9a3ffb5

18 files changed

+15396
-7910
lines changed

create_stub_pyray.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
from raylib.static import rl, ffi
1616

1717
from inspect import ismethod, getmembers, isbuiltin
18-
import inflection, sys
19-
20-
print("""from typing import Any
21-
22-
class PyRay:
23-
def pointer(self, struct):
24-
return ffi.addressof(struct)
25-
""")
18+
import inflection, sys, json
2619

20+
f = open("raylib_api.json", "r")
21+
js = json.load(f)
2722

2823
def ctype_to_python_type(t):
2924
if t == '_Bool':
@@ -47,23 +42,44 @@ def ctype_to_python_type(t):
4742
else:
4843
return t
4944

45+
print("""from typing import Any
46+
47+
class PyRay:
48+
def pointer(self, struct):
49+
return ffi.addressof(struct)
50+
""")
51+
52+
53+
5054

5155
for name, attr in getmembers(rl):
5256
uname = inflection.underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
5357
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>":
54-
58+
json_array = [x for x in js['functions'] if x['name'] == name]
59+
json_object = {}
60+
if len(json_array) > 0:
61+
json_object = json_array[0]
5562
sig = ""
5663
for i, arg in enumerate(ffi.typeof(attr).args):
5764
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
5865
"_pointer").replace(
59-
" ", "")
66+
" ", "")+"_"+str(i)
67+
if 'params' in json_object:
68+
p = json_object['params']
69+
param_name = list(p)[i]
70+
6071
param_type = ctype_to_python_type(arg.cname)
61-
sig += f", {param_name}_{i}: {param_type}"
72+
sig += f", {param_name}: {param_type}"
6273

6374
return_type = ffi.typeof(attr).result.cname
6475

76+
description = attr.__doc__
77+
78+
if 'description' in json_object:
79+
description = json_object['description']
80+
6581
print(
66-
f' def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}:\n """{attr.__doc__}"""\n ...')
82+
f' def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}:\n """{description}"""\n ...')
6783

6884
elif str(type(attr)) == "<class '_cffi_backend._CDataBase'>":
6985
return_type = ffi.typeof(attr).result.cname
@@ -75,11 +91,16 @@ def ctype_to_python_type(t):
7591

7692
for struct in ffi.list_types()[0]:
7793
print("processing", struct, file=sys.stderr)
94+
# json_array = [x for x in js['structs'] if x['name'] == name]
95+
# json_object = {}
96+
# if len(json_array) > 0:
97+
# json_object = json_array[0]
7898
if ffi.typeof(struct).kind == "struct":
7999
if ffi.typeof(struct).fields is None:
80100
print("weird empty struct, skipping", file=sys.stderr)
81101
break
82102
print(f" class {struct}:")
103+
print(f' """ comment """')
83104
sig = ""
84105
for arg in ffi.typeof(struct).fields:
85106
sig += ", " + arg[0]

create_stub_static.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
from raylib.static import rl, ffi
1616

1717
from inspect import ismethod, getmembers, isbuiltin
18-
import inflection, sys
18+
import inflection, sys, json
1919

20-
print("""from typing import Any
20+
f = open("raylib_api.json", "r")
21+
js = json.load(f)
2122

22-
class struct: ...
2323

24-
""")
2524

2625

2726
def ctype_to_python_type(t):
@@ -47,22 +46,38 @@ def ctype_to_python_type(t):
4746
return t
4847

4948

49+
print("""from typing import Any
50+
51+
class struct: ...
52+
53+
""")
54+
5055
for name, attr in getmembers(rl):
5156
uname = name
5257
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>":
53-
58+
json_array = [x for x in js['functions'] if x['name'] == name]
59+
json_object = {}
60+
if len(json_array) > 0:
61+
json_object = json_array[0]
5462
sig = ""
5563
for i, arg in enumerate(ffi.typeof(attr).args):
5664
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
5765
"_pointer").replace(
58-
" ", "")
66+
" ", "")+"_"+str(i)
67+
if 'params' in json_object:
68+
p = json_object['params']
69+
param_name = list(p)[i]
5970
param_type = ctype_to_python_type(arg.cname)
60-
sig += f"{param_name}_{i}: {param_type},"
71+
sig += f"{param_name}: {param_type},"
6172

6273
return_type = ffi.typeof(attr).result.cname
74+
description = attr.__doc__
75+
76+
if 'description' in json_object:
77+
description = json_object['description']
6378

6479
print(
65-
f'def {uname}({sig}) -> {ctype_to_python_type(return_type)}:\n """{attr.__doc__}"""\n ...')
80+
f'def {uname}({sig}) -> {ctype_to_python_type(return_type)}:\n """{description}"""\n ...')
6681

6782
elif str(type(attr)) == "<class '_cffi_backend._CDataBase'>":
6883
return_type = ffi.typeof(attr).result.cname
1.49 KB
Binary file not shown.
962 KB
Binary file not shown.
-211 KB
Binary file not shown.
641 KB
Binary file not shown.

docs-src/raylib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ However, here is a list of available functions:
3838
Functions API reference
3939
-----------------------
4040

41-
.. automodule:: raylib.static.rl
41+
.. autoapimodule:: raylib.static
4242
:members:
4343
:undoc-members:
4444

docs/_sources/raylib.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ However, here is a list of available functions:
3838
Functions API reference
3939
-----------------------
4040

41-
.. automodule:: raylib.static.rl
41+
.. autoapimodule:: raylib.static
4242
:members:
4343
:undoc-members:
4444

0 commit comments

Comments
 (0)