Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify: Remove Peto and the Operation concept
- Loading branch information
1 parent
37b4db8
commit 35c72f3
Showing
10 changed files
with
137 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ nosetests.xml | |
coverage.xml | ||
*,cover | ||
.hypothesis/ | ||
.mypy_cache/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,73 @@ | ||
"""Action base class.""" | ||
"""A registry for all the business operations of an application.""" | ||
|
||
from abc import ABCMeta, abstractmethod | ||
from types import FunctionType | ||
from .core import Kerno | ||
from .state import Rezulto | ||
|
||
from typing import TYPE_CHECKING, Any, Callable, Union, Type, cast | ||
if TYPE_CHECKING: | ||
from typing import Dict # noqa | ||
|
||
class Action(metaclass=ABCMeta): | ||
"""Base for Action classes. Actions are composable and chainable. | ||
|
||
If you don't need undo functionality, you may skip subclassing Action and | ||
just write a function that takes the argument ``peto``. | ||
Instead of returning something, it should modify ``peto.rezulto``. | ||
""" | ||
class Action(metaclass=ABCMeta): | ||
"""Abstract base for class-based actions.""" | ||
|
||
def __init__(self, peto): # , **kw): | ||
"""Constructor.""" | ||
self.peto = peto | ||
# for key, val in kw.items(): | ||
# setattr(self, key, val) | ||
def __init__(self, kerno: Kerno, user: Any, repo: Any) -> None: | ||
"""Construct.""" | ||
self.kerno = kerno | ||
self.user = user | ||
self.repo = repo | ||
|
||
@abstractmethod | ||
def __call__(self): | ||
"""Override this method to do the main work of the action. | ||
def __call__(self, **kw) -> Rezulto: | ||
"""Must be overridden in subclasses and return a Rezulto.""" | ||
|
||
|
||
TypActionFunction = Callable[..., Rezulto] # a function that returns a Rezulto | ||
TypActionClass = Type[Action] # a subclass of Action | ||
TypAction = Union[TypActionClass, TypActionFunction] # either of these | ||
|
||
|
||
class ActionRegistry: | ||
"""Kerno's action registry.""" | ||
|
||
Instead of returning something, modify self.peto.rezulto. | ||
def __init__(self, kerno: Kerno) -> None: | ||
"""Construct. | ||
``kerno`` must be the Kerno instance for the current application. | ||
""" | ||
pass | ||
self.kerno = kerno | ||
self.actions = {} # type: Dict[str, TypAction] | ||
|
||
def add(self, name: str, action: TypAction) -> None: | ||
"""Register an Action under ``name`` at startup for later use.""" | ||
# assert callable(action) | ||
if name in self.actions: | ||
raise ValueError( | ||
'An action with the name {} is already registered.'.format( | ||
name)) | ||
self.actions[name] = action | ||
|
||
def remove(self, name: str) -> None: | ||
"""Delete the action with ``name``.""" | ||
del self.actions[name] | ||
|
||
# @abstractmethod # prevents instantiation when not implemented | ||
# def undo(self): | ||
"""Optionally also implement undo for this action.""" | ||
def run(self, name: str, user: Any, repo: Any, **kw) -> Rezulto: | ||
"""Execute, as ``user``, the action stored under ``name``. | ||
``user`` is the User instance requesting the current operation. | ||
This method will become more complex when we introduce events. | ||
""" | ||
# when = when or datetime.utcnow() | ||
action = self.actions[name] | ||
if issubclass(cast(TypActionClass, action), Action): | ||
action_instance = cast(Action, action( | ||
kerno=self.kerno, user=user, repo=repo)) | ||
return action_instance(**kw) | ||
elif isinstance(action, FunctionType): | ||
return action(kerno=self.kerno, user=user, repo=repo, **kw) | ||
else: | ||
raise TypeError( | ||
'"{}" is not a function or an Action subclass!'.format(action)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.