-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
55 lines (36 loc) · 1.29 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from collections import namedtuple
from typing import List
ExecutionResult = namedtuple("ExecutionResult", ["type", "payload"])
ExecutionResult.__new__.__defaults__ = (None,) * len(ExecutionResult._fields)
REGISTRY = {}
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Command(metaclass=Singleton):
"""
The normal API browser command. The fields are used in form_builder to render the form on canvas
And allows hook to execute the command on input
"""
def title(self) -> str:
raise NotImplementedError
def form_fields(self) -> List[dict]:
raise NotImplementedError
def description(self) -> str:
return self.title()
def execute(self, params: dict) -> ExecutionResult:
raise NotImplementedError
def register(command_type):
""" A Decorator that helps to register command
# Arguments
command_type: string to store the command
:return: the wrapped
"""
def wrapper(singleton):
REGISTRY[command_type] = singleton()
return singleton
return wrapper
def registered_commands():
return REGISTRY