Skip to content

Commit

Permalink
embark config to object
Browse files Browse the repository at this point in the history
  • Loading branch information
pmp-p committed Oct 4, 2024
1 parent 48a77b5 commit 70dd152
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 34 deletions.
58 changes: 25 additions & 33 deletions packages.d/pygame/pygame.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,46 @@ echo "
" 1>&2

sed -i 's|check.warn(importable)|pass|g' ${HOST_PREFIX}/lib/python3.13/site-packages/setuptools/command/build_py.py


sed -i 's|check.warn(importable)|pass|g' ${HOST_PREFIX}/lib/python${PYMAJOR}.${PYMINOR}/site-packages/setuptools/command/build_py.py

if ${CI:-false}
then
CYTHON_URL=git+https://github.com/pygame-web/cython.git

CYTHON=${CYTHON:-Cython-3.0.11-py2.py3-none-any.whl}
if echo $GITHUB_WORKSPACE|grep wip

# update cython
TEST_CYTHON=$($HPY -m cython -V 2>&1)
if echo $TEST_CYTHON| grep -q 3\\.1\\.0a0$
then
DEV=true
echo " * not upgrading cython $TEST_CYTHON
" 1>&2
else
DEV=${DEV:-false}
echo " * upgrading cython $TEST_CYTHON to at least 3.0.11
" 1>&2

# update cython
TEST_CYTHON=$($HPY -m cython -V 2>&1)
if echo $TEST_CYTHON| grep -q 3\\.1\\.0a0$
if echo $PYBUILD|grep -q 3\\.13$
then
echo " * not upgrading cython $TEST_CYTHON
" 1>&2
else
echo " * upgrading cython $TEST_CYTHON to 3.0.10
" 1>&2

if echo $PYBUILD|grep -q 3\\.13$
then
echo "
echo "
================= forcing Cython git instead of release ${CYTHON} =================
================= forcing Cython git instead of release ${CYTHON} =================
"
# /opt/python-wasm-sdk/python3-wasm -m pip install --upgrade --force --no-build-isolation git+${CYTHON_URL}
$HPY -m pip install --upgrade --force --no-build-isolation ${CYTHON_URL}
else
echo "
================= Using Cython release ${CYTHON} =================
"
# /opt/python-wasm-sdk/python3-wasm -m pip install --upgrade --force --no-build-isolation git+${CYTHON_URL}
$HPY -m pip install --upgrade --force --no-build-isolation ${CYTHON_URL}
else
echo "
"
pushd build
wget -q -c https://github.com/cython/cython/releases/download/3.0.11-1/${CYTHON}
/opt/python-wasm-sdk/python3-wasm -m pip install --upgrade --force $CYTHON
$HPY -m pip install --upgrade --force $CYTHON
popd
fi
================= Using Cython release ${CYTHON} =================
"
pushd build
wget -q -c https://github.com/cython/cython/releases/download/3.0.11-1/${CYTHON}
/opt/python-wasm-sdk/python3-wasm -m pip install --upgrade --force $CYTHON
$HPY -m pip install --upgrade --force $CYTHON
popd
fi

fi
fi

Expand Down
2 changes: 1 addition & 1 deletion src/pygbag/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .config_types import Config


from config_to_object import load_config
from .config_to_object import load_config


devmode = "--dev" in sys.argv
Expand Down
85 changes: 85 additions & 0 deletions src/pygbag/config_to_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# config-to-object 1.0.3 License: MIT License (MIT)
# Original Author: Yannik Keller
# https://pypi.org/project/config-to-object/
# https://github.com/yannikkellerde/config_to_object


from typing import NamedTuple
import sys
from ast import literal_eval
from collections import OrderedDict, namedtuple
import configparser
import os

def is_named_tuple_instance(x):
t = type(x)
b = t.__bases__
if len(b) != 1 or b[0] != tuple: return False
f = getattr(t, '_fields', None)
if not isinstance(f, tuple): return False
return all(type(n)==str for n in f)


def _do_recur(config:NamedTuple,force_name=None):
if force_name is None:
name = type(config).__name__
else:
name = force_name
dic = config._asdict()
classes = []
myclass_lines = [f"class {name}(NamedTuple):"]
for key,value in dic.items():
if is_named_tuple_instance(value):
classes.append(_do_recur(value))
myclass_lines.append(f" {key}:{type(value).__name__}")
else:
if type(value) in (list,tuple):
tname = type(value).__name__
tlist = [type(x) for x in value]
if len(value)>0 and tlist.count(tlist[0]) == len(tlist):
myclass_lines.append(f" {key}:{tname.capitalize()}[{type(value[0]).__name__}]")
else:
myclass_lines.append(f" {key}:{tname}")
else:
myclass_lines.append(f" {key}:{type(value).__name__}")
return "\n\n".join(classes+["\n".join(myclass_lines)])


def create_config_type_file(config:NamedTuple,fname:str,obj_name=None):
lines = ["# Automatically generated type hinting file for a .ini file",
"# Generated with config-to-object https://pypi.org/project/config-to-object/1.0.0/",
'# Run "ini_typefile your_config.ini type_file.py" to create a new type file',
"",
"from typing import NamedTuple, List, Tuple",
""]

to_write = "\n".join(lines+[_do_recur(config,obj_name)])
with open(fname,"w") as f:
f.write(to_write)

def multidict_to_namedtuple(dic:dict,name:str) -> NamedTuple:
for key in dic:
if type(dic[key]) == dict or type(dic[key]) == OrderedDict:
dic[key] = multidict_to_namedtuple(dic[key],key)
return namedtuple(name,dic.keys())(*dic.values())

def load_config(filename:str,comment_prefix=";",encoding=None) -> NamedTuple:
if type(filename) != str:
raise ValueError("Expected filename to be of type str. Found {} instead".format(type(filename)))
if not os.path.isfile(filename):
raise ValueError("Could not find file "+filename)
config_obj = configparser.ConfigParser(inline_comment_prefixes=comment_prefix)
config_obj.read(filename,encoding=encoding)
config_dict = config_obj._sections
for key in config_dict:
for key2 in config_dict[key]:
try:
config_dict[key][key2] = literal_eval(config_dict[key][key2])
except:
pass
return multidict_to_namedtuple(config_dict,"Config")

def command_line_config():
if len(sys.argv) < 3:
raise ValueError("Usage: ini_typefile ConfigFilename OutputFilename")
create_config_type_file(load_config(sys.argv[1]),sys.argv[2])

0 comments on commit 70dd152

Please sign in to comment.