This commit is contained in:
Ra 2022-08-17 18:54:56 -06:00 committed by GitHub
commit 95650ddd83
1 changed files with 50 additions and 0 deletions

50
extendable.py Normal file
View File

@ -0,0 +1,50 @@
from enum import IntFlag, EnumMeta
class BaseExt(IntFlag):
__extending__ = None
@classmethod
def append(cls, members: list[tuple[str, int]]):
_members = BaseExt(cls.__name__, members)
for member in _members:
cls._member_names_.append(member.name)
cls._value2member_map_[member.value] = member
cls._member_map_[member.name] = member
class Extendable(EnumMeta):
def __subclasscheck__(cls, sub):
return issubclass(sub, cls.__bases__[0])
def __new__(cls, name, bases, kwds, **kwargs):
ext = kwargs.get("extending", None)
extends = [(m.name, m.value) for m in ext] if ext else []
new_cls = BaseExt(
name, [(k, v) for k, v in kwds.items() if not k.startswith("_")] + extends
)
new_cls.__extending__ = ext if ext else None
return new_cls
class First(BaseExt, metaclass=Extendable):
B = 0x0
C = 0x1
D = 0x2
E = 0x4
F = 0x8
G = 0x10
H = 0x20
I = 0x40
class Second(
BaseExt,
extending=First,
metaclass=Extendable,
):
BLIND = 0x80
DYING = 0x100
First.append([("J", 1000)])