CEnum
This commit is contained in:
parent
4f31fc0ddc
commit
5317df0635
1
TODO
1
TODO
@ -1,5 +1,4 @@
|
||||
Someday
|
||||
* Event Support
|
||||
* have Window pass-through to Renderer
|
||||
|
||||
Maybe
|
||||
|
@ -1,8 +1,9 @@
|
||||
import ctypes
|
||||
from .internal import _SDL, errcheck, Version
|
||||
from enum import CEnum
|
||||
|
||||
# Constants
|
||||
class INIT(object):
|
||||
class INIT(CEnum):
|
||||
TIMER = 0x00000001
|
||||
AUDIO = 0x00000010
|
||||
VIDEO = 0x00000020
|
||||
|
46
csdl/enum.py
Normal file
46
csdl/enum.py
Normal file
@ -0,0 +1,46 @@
|
||||
""" CEnum & supporting classes, based on idea from Mike Bayer
|
||||
see http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/
|
||||
"""
|
||||
|
||||
class CEnumSymbol(int):
|
||||
def __new__(cls, name, value, description, **kwargs):
|
||||
val = super(CEnumSymbol, cls).__new__(cls, value)
|
||||
val.name = name
|
||||
val.description = description
|
||||
return val
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s>" % self.name
|
||||
|
||||
class CEnumMeta(type):
|
||||
def __init__(cls, classname, bases, dict_):
|
||||
cls._reg = reg = cls._reg.copy()
|
||||
for k, v in dict_.items():
|
||||
if isinstance(v, tuple):
|
||||
sym = reg[v[0]] = CEnumSymbol(k, *v)
|
||||
setattr(cls, k, sym)
|
||||
elif isinstance(v, int):
|
||||
sym = reg[v] = CEnumSymbol(k, v, k)
|
||||
setattr(cls, k, sym)
|
||||
return type.__init__(cls, classname, bases, dict_)
|
||||
|
||||
def __iter__(cls):
|
||||
return iter(cls._reg.values())
|
||||
|
||||
class CEnum(object):
|
||||
__metaclass__ = CEnumMeta
|
||||
_reg = {}
|
||||
|
||||
@classmethod
|
||||
def from_int(cls, value):
|
||||
try:
|
||||
return cls._reg[value]
|
||||
except KeyError:
|
||||
raise ValueError(
|
||||
"Invalid value for %r: %r" %
|
||||
(cls.__name__, value)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def values(cls):
|
||||
return cls._reg.keys()
|
Loading…
Reference in New Issue
Block a user