fix: added exceptions
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
from abc import abstractmethod, ABCMeta
|
from abc import abstractmethod, ABCMeta
|
||||||
|
class Unavailable(BaseException): pass
|
||||||
|
class NotAllowed(BaseException): pass
|
||||||
|
|
||||||
class Action(BaseException, metaclass=ABCMeta):
|
class Action(BaseException, metaclass=ABCMeta):
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
@@ -26,10 +28,11 @@ class ActionFlow:
|
|||||||
def do(self, action: str):
|
def do(self, action: str):
|
||||||
index = self.indices.get(action)
|
index = self.indices.get(action)
|
||||||
assert index is not None
|
assert index is not None
|
||||||
|
|
||||||
if self.on[index] is not None:
|
if self.on[index]: raise Unavailable(action)
|
||||||
value = self.actions[index].prepare()
|
if self.on[index] is None: raise NotAllowed(action)
|
||||||
self.on[index] = value
|
value = self.actions[index].prepare()
|
||||||
|
self.on[index] = bool(value)
|
||||||
|
|
||||||
def react(self):
|
def react(self):
|
||||||
for index in self.indices.values():
|
for index in self.indices.values():
|
||||||
@@ -43,7 +46,7 @@ class ActionFlow:
|
|||||||
assert index is not None
|
assert index is not None
|
||||||
return index
|
return index
|
||||||
|
|
||||||
def allow(self, kind, value: bool):
|
def allow(self, kind, value=True):
|
||||||
index = self.index(kind)
|
index = self.index(kind)
|
||||||
self.on[index] = False if value else None
|
self.on[index] = False if value else None
|
||||||
|
|
||||||
@@ -65,4 +68,4 @@ class ActionFlow:
|
|||||||
|
|
||||||
def capabilities(self) -> dict[str, bool]:
|
def capabilities(self) -> dict[str, bool]:
|
||||||
items = self.indices.items()
|
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 }
|
||||||
|
|||||||
Reference in New Issue
Block a user