fix: removed 'normalize' in ActionFlow

This commit is contained in:
2026-04-29 15:21:27 +08:00
parent 311d0006f3
commit bf54e41f28

View File

@@ -21,16 +21,13 @@ class ActionFlow:
self.actions: list[type[Action]] = [] self.actions: list[type[Action]] = []
self.on: list[bool] = [] self.on: list[bool] = []
@classmethod def do(self, action: type[Action]):
def normalize(cls, kind): name = action.__name__
return str.upper(kind.__name__) index = self.indices.get(name)
def do(self, action: str):
index = self.indices.get(action)
assert index is not None assert index is not None
if self.on[index]: raise Unavailable(action) if self.on[index]: raise Unavailable(name)
if self.on[index] is None: raise NotAllowed(action) if self.on[index] is None: raise NotAllowed(name)
value = self.actions[index].prepare() value = self.actions[index].prepare()
self.on[index] = bool(value) self.on[index] = bool(value)
@@ -40,27 +37,27 @@ class ActionFlow:
self.on[index] = False self.on[index] = False
self.actions[index].perform() self.actions[index].perform()
def index(self, kind: type[Action]) -> int: def index(self, action: type[Action]) -> int:
name = self.normalize(kind) name = action.__name__
index = self.indices.get(name) index = self.indices.get(name)
assert index is not None assert index is not None
return index return index
def allow(self, kind, value=True): def allow(self, action: type[Action], value=True):
index = self.index(kind) index = self.index(action)
self.on[index] = False if value else None self.on[index] = False if value else None
def append(self, kind): def append(self, action: type[Action]):
name = self.normalize(kind) name = action.__name__
index = len(self.indices) index = len(self.indices)
self.indices[name] = index self.indices[name] = index
self.actions.insert(index, kind) self.actions.insert(index, action)
self.on.insert(index, None) self.on.insert(index, None)
def remove(self, kind): def remove(self, action: type[Action]):
name = self.normalize(kind) name = action.__name__
index = self.index(kind) index = self.index(action)
self.indices.pop(name) self.indices.pop(name)
self.actions.pop(index) self.actions.pop(index)