fix: added exceptions

This commit is contained in:
2026-04-28 17:35:32 +08:00
parent eaf5f5cf04
commit 6378ed21ca

View File

@@ -1,4 +1,6 @@
from abc import abstractmethod, ABCMeta
class Unavailable(BaseException): pass
class NotAllowed(BaseException): pass
class Action(BaseException, metaclass=ABCMeta):
def __init__(self, *args):
@@ -26,10 +28,11 @@ class ActionFlow:
def do(self, action: str):
index = self.indices.get(action)
assert index is not None
if self.on[index] is not None:
value = self.actions[index].prepare()
self.on[index] = value
if self.on[index]: raise Unavailable(action)
if self.on[index] is None: raise NotAllowed(action)
value = self.actions[index].prepare()
self.on[index] = bool(value)
def react(self):
for index in self.indices.values():
@@ -43,7 +46,7 @@ class ActionFlow:
assert index is not None
return index
def allow(self, kind, value: bool):
def allow(self, kind, value=True):
index = self.index(kind)
self.on[index] = False if value else None
@@ -65,4 +68,4 @@ class ActionFlow:
def capabilities(self) -> dict[str, bool]:
items = self.indices.items()
return { k: self.on[v] == False for k, v in items }
return { k: not self.on[v] for k, v in items if self.on[v] is not None }