12
12
#
13
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
14
14
15
+ from pathlib import Path
15
16
from raylib import rl , ffi
16
-
17
17
from inspect import ismethod , getmembers , isbuiltin
18
18
import inflection , sys , json
19
19
20
- f = open ("raylib.json" , "r" )
21
- js = json .load (f )
20
+ known_functions = {}
21
+ known_structs = {}
22
+ for filename in (Path ("raylib.json" ), Path ("raymath.json" ), Path ("rlgl.json" ), Path ("raygui.json" ), Path ("physac.json" )):
23
+ f = open (filename , "r" )
24
+ js = json .load (f )
25
+ for fn in js ["functions" ]:
26
+ if fn ["name" ] not in known_functions :
27
+ known_functions [fn ["name" ]] = fn
28
+ for st in js ["structs" ]:
29
+ if st ["name" ] not in known_structs :
30
+ known_structs [st ["name" ]] = st
31
+
22
32
23
33
def ctype_to_python_type (t ):
24
34
if t == '_Bool' :
@@ -51,16 +61,17 @@ def pointer(struct):
51
61
...
52
62
""" )
53
63
54
-
55
-
64
+ # These words can be used for c arg names, but not in python
65
+ reserved_words = ( "in" , "list" , "tuple" , "set" , "dict" , "from" , "range" , "min" , "max" , "any" , "all" , "len" )
56
66
57
67
for name , attr in getmembers (rl ):
58
68
uname = inflection .underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
59
69
if isbuiltin (attr ) or str (type (attr )) == "<class '_cffi_backend.__FFIFunctionWrapper'>" :
60
- json_array = [x for x in js ['functions' ] if x ['name' ] == name ]
61
- json_object = {}
62
- if len (json_array ) > 0 :
63
- json_object = json_array [0 ]
70
+ json_object = known_functions .get (name , None )
71
+ if json_object is None :
72
+ # this is _not_ an exported function from raylib, raymath, rlgl raygui or physac
73
+ # so we don't want it in the pyray API
74
+ continue
64
75
sig = ""
65
76
for i , arg in enumerate (ffi .typeof (attr ).args ):
66
77
param_name = arg .cname .replace ("struct" , "" ).replace ("char *" , "str" ).replace ("*" ,
@@ -69,7 +80,9 @@ def pointer(struct):
69
80
if 'params' in json_object :
70
81
p = json_object ['params' ]
71
82
param_name = list (p )[i ]['name' ]
72
-
83
+ # don't use a python reserved word:
84
+ if param_name in reserved_words :
85
+ param_name = param_name + "_" + str (i )
73
86
param_type = ctype_to_python_type (arg .cname )
74
87
sig += f"{ param_name } : { param_type } ,"
75
88
@@ -95,11 +108,13 @@ def pointer(struct):
95
108
96
109
for struct in ffi .list_types ()[0 ]:
97
110
print ("processing" , struct , file = sys .stderr )
98
- # json_array = [x for x in js['structs'] if x['name'] == name]
99
- # json_object = {}
100
- # if len(json_array) > 0:
101
- # json_object = json_array[0]
111
+
102
112
if ffi .typeof (struct ).kind == "struct" :
113
+ json_object = known_structs .get (struct , None )
114
+ if json_object is None :
115
+ # this is _not_ an exported struct from raylib, raymath, rlgl raygui or physac
116
+ # so we don't want it in the pyray API
117
+ continue
103
118
if ffi .typeof (struct ).fields is None :
104
119
print ("weird empty struct, skipping " + struct , file = sys .stderr )
105
120
continue
0 commit comments