Skip to content

Commit 3260a18

Browse files
Typehints for colours (#131)
1 parent edfd75a commit 3260a18

File tree

5 files changed

+138
-101
lines changed

5 files changed

+138
-101
lines changed

create_stub_pyray.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
known_functions = {}
2121
known_structs = {}
22-
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"), Path("glfw3.json")):
22+
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"),
23+
Path("glfw3.json")):
2324
f = open(filename, "r")
2425
js = json.load(f)
2526
for fn in js["functions"]:
@@ -39,6 +40,8 @@ def ctype_to_python_type(t):
3940
return "int"
4041
elif t == "unsigned long long":
4142
return "int"
43+
elif t == "uint64_t":
44+
return "int"
4245
elif t == "double":
4346
return "float"
4447
elif "char * *" in t:
@@ -50,12 +53,13 @@ def ctype_to_python_type(t):
5053
elif "*" in t:
5154
return "Any"
5255
elif t.startswith("struct"):
53-
return t.replace("struct ","")
56+
return t.replace("struct ", "")
5457
elif t.startswith("unsigned"):
5558
return t.replace("unsigned ", "")
5659
else:
5760
return t
5861

62+
5963
print("""from typing import Any
6064
6165
@@ -78,7 +82,7 @@ def pointer(struct):
7882
for i, arg in enumerate(ffi.typeof(attr).args):
7983
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
8084
"_pointer").replace(
81-
" ", "")+"_"+str(i)
85+
" ", "") + "_" + str(i)
8286
if 'params' in json_object:
8387
p = json_object['params']
8488
param_name = list(p)[i]['name']
@@ -103,8 +107,8 @@ def pointer(struct):
103107
print(
104108
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
105109
else:
106-
#print("*****", str(type(attr)))
107-
t = str(type(attr))[8:-2] # this isolates the type
110+
# print("*****", str(type(attr)))
111+
t = str(type(attr))[8:-2] # this isolates the type
108112
if t != "int":
109113
print(f"{name}: {t}")
110114

@@ -118,7 +122,7 @@ def pointer(struct):
118122
# so we don't want it in the pyray API
119123
continue
120124
if ffi.typeof(struct).fields is None:
121-
print("weird empty struct, skipping "+struct, file=sys.stderr)
125+
print("weird empty struct, skipping " + struct, file=sys.stderr)
122126
continue
123127
print(f"class {struct}:")
124128
print(f' """ struct """')
@@ -130,9 +134,36 @@ def pointer(struct):
130134
for arg in ffi.typeof(struct).fields:
131135
print(f" self.{arg[0]}={arg[0]}")
132136

133-
#elif ffi.typeof(struct).kind == "enum":
137+
# elif ffi.typeof(struct).kind == "enum":
134138
# print(f"{struct}: int")
135139
else:
136140
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
137141

138-
142+
print("""
143+
LIGHTGRAY : Color
144+
GRAY : Color
145+
DARKGRAY : Color
146+
YELLOW : Color
147+
GOLD : Color
148+
ORANGE : Color
149+
PINK : Color
150+
RED : Color
151+
MAROON : Color
152+
GREEN : Color
153+
LIME : Color
154+
DARKGREEN : Color
155+
SKYBLUE : Color
156+
BLUE : Color
157+
DARKBLUE : Color
158+
PURPLE : Color
159+
VIOLET : Color
160+
DARKPURPLE : Color
161+
BEIGE : Color
162+
BROWN : Color
163+
DARKBROWN : Color
164+
WHITE : Color
165+
BLACK : Color
166+
BLANK : Color
167+
MAGENTA : Color
168+
RAYWHITE : Color
169+
""")

create_stub_static.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
known_functions = {}
2121
known_structs = {}
22-
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"), Path("glfw3.json")):
22+
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"),
23+
Path("glfw3.json")):
2324
f = open(filename, "r")
2425
js = json.load(f)
2526
for fn in js["functions"]:
@@ -30,7 +31,6 @@
3031
known_structs[st["name"]] = st
3132

3233

33-
3434
def ctype_to_python_type(t):
3535
if t == '_Bool':
3636
return "bool"
@@ -40,16 +40,20 @@ def ctype_to_python_type(t):
4040
return "int"
4141
elif t == "unsigned long long":
4242
return "int"
43+
elif t == "uint64_t":
44+
return "int"
4345
elif t == "double":
4446
return "float"
47+
elif "char * *" in t:
48+
return "list[str]"
4549
elif "char *" in t:
4650
return "str"
4751
elif "char" in t:
4852
return "str" # not sure about this one
4953
elif "*" in t:
5054
return "Any"
5155
elif t.startswith("struct"):
52-
return t.replace("struct ","")
56+
return t.replace("struct ", "")
5357
elif t.startswith("unsigned"):
5458
return t.replace("unsigned ", "")
5559
else:
@@ -81,14 +85,16 @@ class struct: ...
8185
param_name = str(arg.cname).split("(", 1)[0] + "_callback_" + str(i)
8286
else:
8387
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
84-
"_pointer").replace(" ", "")+"_"+str(i)
88+
"_pointer").replace(" ",
89+
"") + "_" + str(
90+
i)
8591
if 'params' in json_object:
8692
p = json_object['params']
87-
#print("param_name: ", param_name, "i", i, "params: ",p,file=sys.stderr)
93+
# print("param_name: ", param_name, "i", i, "params: ",p,file=sys.stderr)
8894
param_name = list(p)[i]['name']
8995
# don't use a python reserved word:
9096
if param_name in reserved_words:
91-
param_name = param_name+"_"+str(i)
97+
param_name = param_name + "_" + str(i)
9298
param_type = ctype_to_python_type(arg.cname)
9399
sig += f"{param_name}: {param_type},"
94100

@@ -106,7 +112,7 @@ class struct: ...
106112
print(
107113
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
108114
else:
109-
#print("*****", str(type(attr)))
115+
# print("*****", str(type(attr)))
110116
print(f"{name}: {str(type(attr))[8:-2]}") # this isolates the type
111117

112118
for struct in ffi.list_types()[0]:
@@ -130,3 +136,31 @@ class struct: ...
130136
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
131137

132138

139+
print("""
140+
LIGHTGRAY : Color
141+
GRAY : Color
142+
DARKGRAY : Color
143+
YELLOW : Color
144+
GOLD : Color
145+
ORANGE : Color
146+
PINK : Color
147+
RED : Color
148+
MAROON : Color
149+
GREEN : Color
150+
LIME : Color
151+
DARKGREEN : Color
152+
SKYBLUE : Color
153+
BLUE : Color
154+
DARKBLUE : Color
155+
PURPLE : Color
156+
VIOLET : Color
157+
DARKPURPLE : Color
158+
BEIGE : Color
159+
BROWN : Color
160+
DARKBROWN : Color
161+
WHITE : Color
162+
BLACK : Color
163+
BLANK : Color
164+
MAGENTA : Color
165+
RAYWHITE : Color
166+
""")

make_docs.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ echo "creating pyi files"
5050

5151
python3 create_stub_pyray.py > pyray/__init__.pyi
5252
python3 create_enums.py >> pyray/__init__.pyi
53-
cat raylib/colors.py >> pyray/__init__.pyi
5453

5554
python3 create_stub_static.py >raylib/__init__.pyi
56-
cat raylib/colors.py >> raylib/__init__.pyi
5755

5856

5957
echo "installing sphinx modules"

pyray/__init__.pyi

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,10 +2344,10 @@ def glfw_get_required_instance_extensions(count: Any,) -> list[str]:
23442344
def glfw_get_time() -> float:
23452345
""""""
23462346
...
2347-
def glfw_get_timer_frequency() -> uint64_t:
2347+
def glfw_get_timer_frequency() -> int:
23482348
""""""
23492349
...
2350-
def glfw_get_timer_value() -> uint64_t:
2350+
def glfw_get_timer_value() -> int:
23512351
""""""
23522352
...
23532353
def glfw_get_version(major: Any,minor: Any,rev: Any,) -> None:
@@ -3397,6 +3397,34 @@ class rlVertexBuffer:
33973397
self.indices=indices
33983398
self.vaoId=vaoId
33993399
self.vboId=vboId
3400+
3401+
LIGHTGRAY : Color
3402+
GRAY : Color
3403+
DARKGRAY : Color
3404+
YELLOW : Color
3405+
GOLD : Color
3406+
ORANGE : Color
3407+
PINK : Color
3408+
RED : Color
3409+
MAROON : Color
3410+
GREEN : Color
3411+
LIME : Color
3412+
DARKGREEN : Color
3413+
SKYBLUE : Color
3414+
BLUE : Color
3415+
DARKBLUE : Color
3416+
PURPLE : Color
3417+
VIOLET : Color
3418+
DARKPURPLE : Color
3419+
BEIGE : Color
3420+
BROWN : Color
3421+
DARKBROWN : Color
3422+
WHITE : Color
3423+
BLACK : Color
3424+
BLANK : Color
3425+
MAGENTA : Color
3426+
RAYWHITE : Color
3427+
34003428
from enum import IntEnum
34013429

34023430
class ConfigFlags(IntEnum):
@@ -4111,44 +4139,3 @@ class GuiIconName(IntEnum):
41114139
ICON_254 = 254
41124140
ICON_255 = 255
41134141

4114-
# Copyright (c) 2021 Richard Smith and others
4115-
#
4116-
# This program and the accompanying materials are made available under the
4117-
# terms of the Eclipse Public License 2.0 which is available at
4118-
# http://www.eclipse.org/legal/epl-2.0.
4119-
#
4120-
# This Source Code may also be made available under the following Secondary
4121-
# licenses when the conditions for such availability set forth in the Eclipse
4122-
# Public License, v. 2.0 are satisfied: GNU General Public License, version 2
4123-
# with the GNU Classpath Exception which is
4124-
# available at https://www.gnu.org/software/classpath/license.html.
4125-
#
4126-
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
4127-
4128-
LIGHTGRAY =( 200, 200, 200, 255 )
4129-
GRAY =( 130, 130, 130, 255 )
4130-
DARKGRAY =( 80, 80, 80, 255 )
4131-
YELLOW =( 253, 249, 0, 255 )
4132-
GOLD =( 255, 203, 0, 255 )
4133-
ORANGE =( 255, 161, 0, 255 )
4134-
PINK =( 255, 109, 194, 255 )
4135-
RED =( 230, 41, 55, 255 )
4136-
MAROON =( 190, 33, 55, 255 )
4137-
GREEN =( 0, 228, 48, 255 )
4138-
LIME =( 0, 158, 47, 255 )
4139-
DARKGREEN =( 0, 117, 44, 255 )
4140-
SKYBLUE =( 102, 191, 255, 255 )
4141-
BLUE =( 0, 121, 241, 255 )
4142-
DARKBLUE =( 0, 82, 172, 255 )
4143-
PURPLE =( 200, 122, 255, 255 )
4144-
VIOLET =( 135, 60, 190, 255 )
4145-
DARKPURPLE =( 112, 31, 126, 255 )
4146-
BEIGE =( 211, 176, 131, 255 )
4147-
BROWN =( 127, 106, 79, 255 )
4148-
DARKBROWN =( 76, 63, 47, 255 )
4149-
WHITE =( 255, 255, 255, 255 )
4150-
BLACK =( 0, 0, 0, 255 )
4151-
BLANK =( 0, 0, 0, 0 )
4152-
MAGENTA =( 255, 0, 255, 255 )
4153-
RAYWHITE =( 245, 245, 245, 255 )
4154-

raylib/__init__.pyi

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,10 +3093,10 @@ def glfwGetRequiredInstanceExtensions(count: Any,) -> str:
30933093
def glfwGetTime() -> float:
30943094
""""""
30953095
...
3096-
def glfwGetTimerFrequency() -> uint64_t:
3096+
def glfwGetTimerFrequency() -> int:
30973097
""""""
30983098
...
3099-
def glfwGetTimerValue() -> uint64_t:
3099+
def glfwGetTimerValue() -> int:
31003100
""""""
31013101
...
31023102
def glfwGetVersion(major: Any,minor: Any,rev: Any,) -> None:
@@ -3886,44 +3886,31 @@ rlShaderUniformDataType: int
38863886
rlTextureFilter: int
38873887
rlTraceLogLevel: int
38883888
rlVertexBuffer: struct
3889-
# Copyright (c) 2021 Richard Smith and others
3890-
#
3891-
# This program and the accompanying materials are made available under the
3892-
# terms of the Eclipse Public License 2.0 which is available at
3893-
# http://www.eclipse.org/legal/epl-2.0.
3894-
#
3895-
# This Source Code may also be made available under the following Secondary
3896-
# licenses when the conditions for such availability set forth in the Eclipse
3897-
# Public License, v. 2.0 are satisfied: GNU General Public License, version 2
3898-
# with the GNU Classpath Exception which is
3899-
# available at https://www.gnu.org/software/classpath/license.html.
3900-
#
3901-
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
39023889

3903-
LIGHTGRAY =( 200, 200, 200, 255 )
3904-
GRAY =( 130, 130, 130, 255 )
3905-
DARKGRAY =( 80, 80, 80, 255 )
3906-
YELLOW =( 253, 249, 0, 255 )
3907-
GOLD =( 255, 203, 0, 255 )
3908-
ORANGE =( 255, 161, 0, 255 )
3909-
PINK =( 255, 109, 194, 255 )
3910-
RED =( 230, 41, 55, 255 )
3911-
MAROON =( 190, 33, 55, 255 )
3912-
GREEN =( 0, 228, 48, 255 )
3913-
LIME =( 0, 158, 47, 255 )
3914-
DARKGREEN =( 0, 117, 44, 255 )
3915-
SKYBLUE =( 102, 191, 255, 255 )
3916-
BLUE =( 0, 121, 241, 255 )
3917-
DARKBLUE =( 0, 82, 172, 255 )
3918-
PURPLE =( 200, 122, 255, 255 )
3919-
VIOLET =( 135, 60, 190, 255 )
3920-
DARKPURPLE =( 112, 31, 126, 255 )
3921-
BEIGE =( 211, 176, 131, 255 )
3922-
BROWN =( 127, 106, 79, 255 )
3923-
DARKBROWN =( 76, 63, 47, 255 )
3924-
WHITE =( 255, 255, 255, 255 )
3925-
BLACK =( 0, 0, 0, 255 )
3926-
BLANK =( 0, 0, 0, 0 )
3927-
MAGENTA =( 255, 0, 255, 255 )
3928-
RAYWHITE =( 245, 245, 245, 255 )
3890+
LIGHTGRAY : Color
3891+
GRAY : Color
3892+
DARKGRAY : Color
3893+
YELLOW : Color
3894+
GOLD : Color
3895+
ORANGE : Color
3896+
PINK : Color
3897+
RED : Color
3898+
MAROON : Color
3899+
GREEN : Color
3900+
LIME : Color
3901+
DARKGREEN : Color
3902+
SKYBLUE : Color
3903+
BLUE : Color
3904+
DARKBLUE : Color
3905+
PURPLE : Color
3906+
VIOLET : Color
3907+
DARKPURPLE : Color
3908+
BEIGE : Color
3909+
BROWN : Color
3910+
DARKBROWN : Color
3911+
WHITE : Color
3912+
BLACK : Color
3913+
BLANK : Color
3914+
MAGENTA : Color
3915+
RAYWHITE : Color
39293916

0 commit comments

Comments
 (0)