Skip to content

Commit 2875e28

Browse files
remove dependency on inflection per @sDos280 suggestion to speed up pygbag WASM loading, #58
1 parent 3744686 commit 2875e28

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

pyray/__init__.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# available at https://www.gnu.org/software/classpath/license.html.
1212
#
1313
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
14+
import re
1415
import weakref
1516
from array import array
1617

@@ -23,12 +24,21 @@
2324
print("sorry deprecated enums dont work on dynamic version")
2425

2526
from inspect import getmembers, isbuiltin
26-
import inflection
2727

2828
current_module = __import__(__name__)
2929

3030

31-
def pointer(self, struct):
31+
def _underscore(word: str) -> str:
32+
"""
33+
from inflection
34+
"""
35+
word = re.sub(r"([A-Z]+)([A-Z][a-z])", r'\1_\2', word)
36+
word = re.sub(r"([a-z\d])([A-Z])", r'\1_\2', word)
37+
word = word.replace("-", "_")
38+
return word.lower()
39+
40+
41+
def pointer(struct):
3242
return ffi.addressof(struct)
3343

3444

@@ -40,11 +50,11 @@ def pointer(self, struct):
4050
# Another possibility is ffi.typeof() but that will throw an exception if you give it a type that isn't a ctype
4151
# Another way to improve performance might be to special-case simple types before doing the string comparisons
4252

43-
def makefunc(a):
53+
def _wrap_function(original_func):
4454
# print("makefunc ",a, ffi.typeof(a).args)
45-
def func(*args):
55+
def wrapped_func(*args):
4656
modified_args = []
47-
for (c_arg, arg) in zip(ffi.typeof(a).args, args):
57+
for (c_arg, arg) in zip(ffi.typeof(original_func).args, args):
4858
# print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
4959
if c_arg.kind == 'pointer':
5060
if type(arg) is str:
@@ -69,7 +79,7 @@ def func(*args):
6979
raise TypeError(
7080
"Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)")
7181
modified_args.append(arg)
72-
result = a(*modified_args)
82+
result = original_func(*modified_args)
7383
if result is None:
7484
return
7585
elif is_cdata(result) and str(result).startswith("<cdata 'char *'"):
@@ -85,11 +95,13 @@ def is_cdata(arg):
8595
return str(type(arg)) == "<class '_cffi_backend.__CDataOwn'>" or str(
8696
type(arg)) == "<class '_cffi_backend._CDataBase'>"
8797

88-
return func
98+
return wrapped_func
99+
89100

90101
global_weakkeydict = weakref.WeakKeyDictionary()
91102

92-
def makeStructHelper(struct):
103+
104+
def _make_struct_constructor_function(struct):
93105
def func(*args):
94106
# print(struct, args)
95107
modified_args = []
@@ -100,7 +112,7 @@ def func(*args):
100112
elif (field[1].type.kind == 'pointer'
101113
and (str(type(arg)) == "<class 'numpy.ndarray'>"
102114
or isinstance(arg, (array, bytes, bytearray, memoryview)))):
103-
arg = ffi.from_buffer(field[1].type, arg)
115+
arg = ffi.from_buffer(field[1].type, arg)
104116
modified_args.append(arg)
105117
s = ffi.new(f"struct {struct} *", modified_args)[0]
106118
global_weakkeydict[s] = modified_args
@@ -110,21 +122,21 @@ def func(*args):
110122

111123

112124
for name, attr in getmembers(rl):
113-
#print(name, dir(attr))
114-
uname = inflection.underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
125+
# print(name, attr)
126+
uname = _underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
115127
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str(
116128
type(attr)) == "<class '_cffi_backend._CDataBase'>":
117129
# print(attr.__call__)
118130
# print(attr.__doc__)
119131
# print(dir(attr))
120132
# print(dir(attr.__repr__))
121-
f = makefunc(attr)
133+
f = _wrap_function(attr)
122134
setattr(current_module, uname, f)
123135
else:
124136
setattr(current_module, name, attr)
125137

126138
for struct in ffi.list_types()[0]:
127-
f = makeStructHelper(struct)
139+
f = _make_struct_constructor_function(struct)
128140
setattr(current_module, struct, f)
129141

130142
# overwrite ffi enums with our own

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def has_ext_modules(foo):
3838
],
3939
packages=["raylib", "pyray"],
4040
include_package_data=True,
41-
install_requires=["cffi>=1.14.6","inflection"],
41+
install_requires=["cffi>=1.14.6"],
4242
distclass=BinaryDistribution,
4343
cffi_modules=["raylib/build.py:ffibuilder"]
4444
)

0 commit comments

Comments
 (0)