7b8490b90f0decbd73005248159.../extendable.py

51 lines
1.1 KiB
Python

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)])