1
|
|
|
"""Common definition.""" |
2
|
|
|
# from collections import namedtuple |
3
|
|
|
from typing import Any, Awaitable, Callable, List, NamedTuple, Optional, TypeVar, Union |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
__all__ = [ |
7
|
|
|
'CallableFunction', |
8
|
|
|
'AsyncInnerFunction', |
9
|
|
|
'SUCCESS', |
10
|
|
|
'FAILURE', |
11
|
|
|
'ExceptionDecorator', |
12
|
|
|
'NodeMetadata', |
13
|
|
|
'node_metadata', |
14
|
|
|
] |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
CallableFunction = Union[Callable[..., Awaitable[Any]], Callable] |
18
|
|
|
"""Something callable with or without async.""" |
19
|
|
|
|
20
|
|
|
AsyncInnerFunction = Callable[[], Awaitable[Any]] |
21
|
|
|
"""Function signature of async function implementation.""" |
22
|
|
|
|
23
|
|
|
SUCCESS = True # a success call |
24
|
|
|
"""Success constant.""" |
25
|
|
|
|
26
|
|
|
FAILURE = not SUCCESS # Well defined falsy... |
27
|
|
|
"""Failure constant.""" |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class ExceptionDecorator(Exception): |
31
|
|
|
"""ExceptionDecorator exception is a decorator on a real exception. |
32
|
|
|
|
33
|
|
|
This will ensure that ```assert ExceptionDecorator.__bool__ == False```. |
34
|
|
|
This permit to return exception as a 'FAILURE' status. |
35
|
|
|
""" |
36
|
|
|
|
37
|
|
|
def __init__(self, exception: Exception): |
38
|
|
|
super().__init__() |
39
|
|
|
self.exception = exception |
40
|
|
|
|
41
|
|
|
def __bool__(self): |
42
|
|
|
return False |
43
|
|
|
|
44
|
|
|
def __repr__(self): |
45
|
|
|
return self.exception.__repr__() |
46
|
|
|
|
47
|
|
|
def __str__(self): |
48
|
|
|
return self.exception.__str__() |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class NodeMetadata(NamedTuple): |
52
|
|
|
"""NodeMetadata is our node definition. |
53
|
|
|
|
54
|
|
|
# Attributes |
55
|
|
|
- name (str): named operation |
56
|
|
|
- properties (List[str]): a list of property name. |
57
|
|
|
- edges (List[str]): a list of member name which act as edges. |
58
|
|
|
""" |
59
|
|
|
|
60
|
|
|
name: str |
61
|
|
|
properties: List[str] |
62
|
|
|
edges: List[str] |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
T = TypeVar('T', bound=CallableFunction) |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def node_metadata( # pylint: disable=protected-access |
69
|
|
|
name: Optional[str] = None, |
70
|
|
|
properties: Optional[List[str]] = None, |
71
|
|
|
edges: Optional[List[str]] = None, |
72
|
|
|
): |
73
|
|
|
"""'node_metadata' is a function decorator which add meta information about node. |
74
|
|
|
|
75
|
|
|
We add a property on decorated function named '__node_metadata'. |
76
|
|
|
|
77
|
|
|
# Parameters |
78
|
|
|
name (Optional[str]): override name of decorated function, |
79
|
|
|
default is function name left striped with '_' |
80
|
|
|
properties (Optional[List[str]]): a list of property name ([] as default) |
81
|
|
|
edges (Optional[List[str]]): a list of edges name (["child", "children"] as default) |
82
|
|
|
|
83
|
|
|
# Returns |
84
|
|
|
the decorator function |
85
|
|
|
""" |
86
|
|
|
|
87
|
|
|
def decorate_function(function: T) -> T: |
88
|
|
|
function.__node_metadata = NodeMetadata( |
89
|
|
|
name=name if name else function.__name__.lstrip('_'), |
90
|
|
|
properties=properties or [], |
91
|
|
|
edges=edges or ['child', 'children'], |
92
|
|
|
) |
93
|
|
|
return function |
94
|
|
|
|
95
|
|
|
return decorate_function |
96
|
|
|
|