Typehints for colours (#131)

This commit is contained in:
Richard Smith 2024-07-01 11:11:35 +01:00 committed by GitHub
parent edfd75af8d
commit 3260a18e7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 138 additions and 101 deletions

View file

@ -19,7 +19,8 @@ import inflection, sys, json
known_functions = {}
known_structs = {}
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"), Path("glfw3.json")):
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"),
Path("glfw3.json")):
f = open(filename, "r")
js = json.load(f)
for fn in js["functions"]:
@ -39,6 +40,8 @@ def ctype_to_python_type(t):
return "int"
elif t == "unsigned long long":
return "int"
elif t == "uint64_t":
return "int"
elif t == "double":
return "float"
elif "char * *" in t:
@ -50,12 +53,13 @@ def ctype_to_python_type(t):
elif "*" in t:
return "Any"
elif t.startswith("struct"):
return t.replace("struct ","")
return t.replace("struct ", "")
elif t.startswith("unsigned"):
return t.replace("unsigned ", "")
else:
return t
print("""from typing import Any
@ -78,7 +82,7 @@ for name, attr in getmembers(rl):
for i, arg in enumerate(ffi.typeof(attr).args):
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
"_pointer").replace(
" ", "")+"_"+str(i)
" ", "") + "_" + str(i)
if 'params' in json_object:
p = json_object['params']
param_name = list(p)[i]['name']
@ -103,8 +107,8 @@ for name, attr in getmembers(rl):
print(
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
else:
#print("*****", str(type(attr)))
t = str(type(attr))[8:-2] # this isolates the type
# print("*****", str(type(attr)))
t = str(type(attr))[8:-2] # this isolates the type
if t != "int":
print(f"{name}: {t}")
@ -118,7 +122,7 @@ for struct in ffi.list_types()[0]:
# so we don't want it in the pyray API
continue
if ffi.typeof(struct).fields is None:
print("weird empty struct, skipping "+struct, file=sys.stderr)
print("weird empty struct, skipping " + struct, file=sys.stderr)
continue
print(f"class {struct}:")
print(f' """ struct """')
@ -130,9 +134,36 @@ for struct in ffi.list_types()[0]:
for arg in ffi.typeof(struct).fields:
print(f" self.{arg[0]}={arg[0]}")
#elif ffi.typeof(struct).kind == "enum":
# elif ffi.typeof(struct).kind == "enum":
# print(f"{struct}: int")
else:
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
print("""
LIGHTGRAY : Color
GRAY : Color
DARKGRAY : Color
YELLOW : Color
GOLD : Color
ORANGE : Color
PINK : Color
RED : Color
MAROON : Color
GREEN : Color
LIME : Color
DARKGREEN : Color
SKYBLUE : Color
BLUE : Color
DARKBLUE : Color
PURPLE : Color
VIOLET : Color
DARKPURPLE : Color
BEIGE : Color
BROWN : Color
DARKBROWN : Color
WHITE : Color
BLACK : Color
BLANK : Color
MAGENTA : Color
RAYWHITE : Color
""")

View file

@ -19,7 +19,8 @@ import inflection, sys, json
known_functions = {}
known_structs = {}
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"), Path("glfw3.json")):
for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), Path("raygui.json"), Path("physac.json"),
Path("glfw3.json")):
f = open(filename, "r")
js = json.load(f)
for fn in js["functions"]:
@ -30,7 +31,6 @@ for filename in (Path("raylib.json"), Path("raymath.json"), Path("rlgl.json"), P
known_structs[st["name"]] = st
def ctype_to_python_type(t):
if t == '_Bool':
return "bool"
@ -40,8 +40,12 @@ def ctype_to_python_type(t):
return "int"
elif t == "unsigned long long":
return "int"
elif t == "uint64_t":
return "int"
elif t == "double":
return "float"
elif "char * *" in t:
return "list[str]"
elif "char *" in t:
return "str"
elif "char" in t:
@ -49,7 +53,7 @@ def ctype_to_python_type(t):
elif "*" in t:
return "Any"
elif t.startswith("struct"):
return t.replace("struct ","")
return t.replace("struct ", "")
elif t.startswith("unsigned"):
return t.replace("unsigned ", "")
else:
@ -81,14 +85,16 @@ for name, attr in getmembers(rl):
param_name = str(arg.cname).split("(", 1)[0] + "_callback_" + str(i)
else:
param_name = arg.cname.replace("struct", "").replace("char *", "str").replace("*",
"_pointer").replace(" ", "")+"_"+str(i)
"_pointer").replace(" ",
"") + "_" + str(
i)
if 'params' in json_object:
p = json_object['params']
#print("param_name: ", param_name, "i", i, "params: ",p,file=sys.stderr)
# print("param_name: ", param_name, "i", i, "params: ",p,file=sys.stderr)
param_name = list(p)[i]['name']
# don't use a python reserved word:
if param_name in reserved_words:
param_name = param_name+"_"+str(i)
param_name = param_name + "_" + str(i)
param_type = ctype_to_python_type(arg.cname)
sig += f"{param_name}: {param_type},"
@ -106,7 +112,7 @@ for name, attr in getmembers(rl):
print(
f'def {uname}(*args) -> {ctype_to_python_type(return_type)}:\n """VARARG FUNCTION - MAY NOT BE SUPPORTED BY CFFI"""\n ...')
else:
#print("*****", str(type(attr)))
# print("*****", str(type(attr)))
print(f"{name}: {str(type(attr))[8:-2]}") # this isolates the type
for struct in ffi.list_types()[0]:
@ -130,3 +136,31 @@ for struct in ffi.list_types()[0]:
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
print("""
LIGHTGRAY : Color
GRAY : Color
DARKGRAY : Color
YELLOW : Color
GOLD : Color
ORANGE : Color
PINK : Color
RED : Color
MAROON : Color
GREEN : Color
LIME : Color
DARKGREEN : Color
SKYBLUE : Color
BLUE : Color
DARKBLUE : Color
PURPLE : Color
VIOLET : Color
DARKPURPLE : Color
BEIGE : Color
BROWN : Color
DARKBROWN : Color
WHITE : Color
BLACK : Color
BLANK : Color
MAGENTA : Color
RAYWHITE : Color
""")

View file

@ -50,10 +50,8 @@ echo "creating pyi files"
python3 create_stub_pyray.py > pyray/__init__.pyi
python3 create_enums.py >> pyray/__init__.pyi
cat raylib/colors.py >> pyray/__init__.pyi
python3 create_stub_static.py >raylib/__init__.pyi
cat raylib/colors.py >> raylib/__init__.pyi
echo "installing sphinx modules"

View file

@ -2344,10 +2344,10 @@ def glfw_get_required_instance_extensions(count: Any,) -> list[str]:
def glfw_get_time() -> float:
""""""
...
def glfw_get_timer_frequency() -> uint64_t:
def glfw_get_timer_frequency() -> int:
""""""
...
def glfw_get_timer_value() -> uint64_t:
def glfw_get_timer_value() -> int:
""""""
...
def glfw_get_version(major: Any,minor: Any,rev: Any,) -> None:
@ -3397,6 +3397,34 @@ class rlVertexBuffer:
self.indices=indices
self.vaoId=vaoId
self.vboId=vboId
LIGHTGRAY : Color
GRAY : Color
DARKGRAY : Color
YELLOW : Color
GOLD : Color
ORANGE : Color
PINK : Color
RED : Color
MAROON : Color
GREEN : Color
LIME : Color
DARKGREEN : Color
SKYBLUE : Color
BLUE : Color
DARKBLUE : Color
PURPLE : Color
VIOLET : Color
DARKPURPLE : Color
BEIGE : Color
BROWN : Color
DARKBROWN : Color
WHITE : Color
BLACK : Color
BLANK : Color
MAGENTA : Color
RAYWHITE : Color
from enum import IntEnum
class ConfigFlags(IntEnum):
@ -4111,44 +4139,3 @@ class GuiIconName(IntEnum):
ICON_254 = 254
ICON_255 = 255
# Copyright (c) 2021 Richard Smith and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# This Source Code may also be made available under the following Secondary
# licenses when the conditions for such availability set forth in the Eclipse
# Public License, v. 2.0 are satisfied: GNU General Public License, version 2
# with the GNU Classpath Exception which is
# available at https://www.gnu.org/software/classpath/license.html.
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
LIGHTGRAY =( 200, 200, 200, 255 )
GRAY =( 130, 130, 130, 255 )
DARKGRAY =( 80, 80, 80, 255 )
YELLOW =( 253, 249, 0, 255 )
GOLD =( 255, 203, 0, 255 )
ORANGE =( 255, 161, 0, 255 )
PINK =( 255, 109, 194, 255 )
RED =( 230, 41, 55, 255 )
MAROON =( 190, 33, 55, 255 )
GREEN =( 0, 228, 48, 255 )
LIME =( 0, 158, 47, 255 )
DARKGREEN =( 0, 117, 44, 255 )
SKYBLUE =( 102, 191, 255, 255 )
BLUE =( 0, 121, 241, 255 )
DARKBLUE =( 0, 82, 172, 255 )
PURPLE =( 200, 122, 255, 255 )
VIOLET =( 135, 60, 190, 255 )
DARKPURPLE =( 112, 31, 126, 255 )
BEIGE =( 211, 176, 131, 255 )
BROWN =( 127, 106, 79, 255 )
DARKBROWN =( 76, 63, 47, 255 )
WHITE =( 255, 255, 255, 255 )
BLACK =( 0, 0, 0, 255 )
BLANK =( 0, 0, 0, 0 )
MAGENTA =( 255, 0, 255, 255 )
RAYWHITE =( 245, 245, 245, 255 )

View file

@ -3093,10 +3093,10 @@ def glfwGetRequiredInstanceExtensions(count: Any,) -> str:
def glfwGetTime() -> float:
""""""
...
def glfwGetTimerFrequency() -> uint64_t:
def glfwGetTimerFrequency() -> int:
""""""
...
def glfwGetTimerValue() -> uint64_t:
def glfwGetTimerValue() -> int:
""""""
...
def glfwGetVersion(major: Any,minor: Any,rev: Any,) -> None:
@ -3886,44 +3886,31 @@ rlShaderUniformDataType: int
rlTextureFilter: int
rlTraceLogLevel: int
rlVertexBuffer: struct
# Copyright (c) 2021 Richard Smith and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# This Source Code may also be made available under the following Secondary
# licenses when the conditions for such availability set forth in the Eclipse
# Public License, v. 2.0 are satisfied: GNU General Public License, version 2
# with the GNU Classpath Exception which is
# available at https://www.gnu.org/software/classpath/license.html.
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
LIGHTGRAY =( 200, 200, 200, 255 )
GRAY =( 130, 130, 130, 255 )
DARKGRAY =( 80, 80, 80, 255 )
YELLOW =( 253, 249, 0, 255 )
GOLD =( 255, 203, 0, 255 )
ORANGE =( 255, 161, 0, 255 )
PINK =( 255, 109, 194, 255 )
RED =( 230, 41, 55, 255 )
MAROON =( 190, 33, 55, 255 )
GREEN =( 0, 228, 48, 255 )
LIME =( 0, 158, 47, 255 )
DARKGREEN =( 0, 117, 44, 255 )
SKYBLUE =( 102, 191, 255, 255 )
BLUE =( 0, 121, 241, 255 )
DARKBLUE =( 0, 82, 172, 255 )
PURPLE =( 200, 122, 255, 255 )
VIOLET =( 135, 60, 190, 255 )
DARKPURPLE =( 112, 31, 126, 255 )
BEIGE =( 211, 176, 131, 255 )
BROWN =( 127, 106, 79, 255 )
DARKBROWN =( 76, 63, 47, 255 )
WHITE =( 255, 255, 255, 255 )
BLACK =( 0, 0, 0, 255 )
BLANK =( 0, 0, 0, 0 )
MAGENTA =( 255, 0, 255, 255 )
RAYWHITE =( 245, 245, 245, 255 )
LIGHTGRAY : Color
GRAY : Color
DARKGRAY : Color
YELLOW : Color
GOLD : Color
ORANGE : Color
PINK : Color
RED : Color
MAROON : Color
GREEN : Color
LIME : Color
DARKGREEN : Color
SKYBLUE : Color
BLUE : Color
DARKBLUE : Color
PURPLE : Color
VIOLET : Color
DARKPURPLE : Color
BEIGE : Color
BROWN : Color
DARKBROWN : Color
WHITE : Color
BLACK : Color
BLANK : Color
MAGENTA : Color
RAYWHITE : Color