-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from AndPuQing/master
fix code style
- Loading branch information
Showing
8 changed files
with
98 additions
and
41 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
[flake8] | ||
max-line-length = 100 | ||
extend-ignore = E203 |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import functools | ||
import inspect | ||
from ast import literal_eval | ||
from typing import Callable, Optional | ||
|
||
from neetbox.core import Registry | ||
from neetbox.logging import logger | ||
from neetbox.utils.mvc import Singleton | ||
|
||
|
||
class PackedAction(Callable): | ||
def __init__(self, function: Callable, name=None, **kwargs): | ||
super().__init__(**kwargs) | ||
self.function = function | ||
self.name = name if name else function.__name__ | ||
self.argspec = inspect.getfullargspec(self.function) | ||
|
||
def __call__(self, **argv): | ||
self.function(argv) | ||
|
||
def eval_call(self, params: dict): | ||
eval_params = dict((k, literal_eval(v)) for k, v in params.items()) | ||
return self.function(**eval_params) | ||
|
||
|
||
class _NeetAction(metaclass=Singleton): | ||
__ACTION_POOL: Registry = Registry("__NEET_ACTIONS") | ||
|
||
def register( | ||
self, | ||
*, | ||
name: Optional[str] = None, | ||
): | ||
return functools.partial(self._register, name=name) | ||
|
||
def _register(self, function: Callable, name: str = None): | ||
packed = PackedAction(function=function, name=name) | ||
_NeetAction.__ACTION_POOL._register(what=packed, name=packed.name, force=True) | ||
return function | ||
|
||
def get_actions(self): | ||
action_names = _NeetAction.__ACTION_POOL.keys() | ||
actions = {} | ||
for n in action_names: | ||
actions[n] = _NeetAction.__ACTION_POOL[n].argspec | ||
return actions | ||
|
||
def eval_call(self, name: str, params: dict): | ||
if name not in _NeetAction.__ACTION_POOL: | ||
logger.err(f"Could not find action with name {name}, action stopped.") | ||
return False | ||
return _NeetAction.__ACTION_POOL[name].eval_call(params) | ||
|
||
|
||
# singleton | ||
neet_action = _NeetAction() | ||
|
||
|
||
# example | ||
if __name__ == "__main__": | ||
|
||
@neet_action.register(name="some") | ||
def some(a, b): | ||
print(a, b) | ||
|
||
print("registered actions:") | ||
print(neet_action.get_actions()) | ||
|
||
print("calling 'some") | ||
neet_action.eval_call("some", {"a": "3", "b": "4"}) |
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
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