2
2
from contextshell .NodePath import NodePath
3
3
from contextshell .TreeRoot import TreeRoot
4
4
from contextshell .Action import Action
5
- from typing import Callable , List , Optional
5
+ from contextshell .CallableAction import action_from_function
6
+ from typing import Callable , List , Optional , Dict , Union , Any
6
7
7
8
8
9
# CHECK: how to implement TemporaryTreeRoot (based on NodeTreeRoot)
9
10
class NodeTreeRoot (TreeRoot ):
10
11
"""Frontend to the (passive) node-based data storage"""
11
12
def __init__ (self ):
12
13
self .root = self .create_node (None )
13
- from contextshell .ActionFinder import ActionFinder
14
- self .action_finder = ActionFinder (self )
15
14
self .install_default_actions ()
16
15
17
- def install_default_actions (self ):
18
- def create (tree : NodeTreeRoot , target : NodePath , action : NodePath , name , value = None ):
19
- tree .create (NodePath .join (target , name ), value )
20
-
21
- self .action_finder .install_action ("." , "create" , create )
22
-
23
- def exists (tree : NodeTreeRoot , target : NodePath , action : NodePath , name ):
24
- return tree .exists (NodePath .join (target , name ))
25
-
26
- self .action_finder .install_action ("." , "exists" , exists )
27
-
28
- def get (tree : NodeTreeRoot , target : NodePath , action : NodePath ):
29
- return tree .get (target )
16
+ def create_action (self , target : NodePath , path : str , value = None ):
17
+ self .create (NodePath .join (target , path ), value )
30
18
31
- self .action_finder .install_action ("." , "get" , get )
19
+ def exists_action (self , target : NodePath , path : str ) -> bool :
20
+ return self .exists (NodePath .join (target , path ))
32
21
33
- def set_action ( tree : NodeTreeRoot , target : NodePath , action : NodePath , new_value ):
34
- return tree . set (target , new_value )
22
+ def get_action ( self , target : NodePath ):
23
+ return self . get (target )
35
24
36
- self .action_finder .install_action ("." , "set" , set_action )
25
+ def set_action (self , target : NodePath , new_value ):
26
+ return self .set (target , new_value )
37
27
38
- def list_action (tree : NodeTreeRoot , target : NodePath , action : NodePath ):
39
- all_list = tree .list (target )
40
- return list (filter (lambda p : not self .is_attribute (p ), all_list ))
28
+ def list_action (self , target : NodePath ):
29
+ all_list = self .list (target )
30
+ return list (filter (lambda p : not self .is_attribute (p ), all_list ))
41
31
42
- self .action_finder .install_action ("." , "list" , list_action )
32
+ def list_all_action (self , target : NodePath ):
33
+ return self .list (target )
43
34
44
- def list_all (tree : NodeTreeRoot , target : NodePath , action : NodePath ):
45
- return tree .list (target )
35
+ def list_attributes_action (self , target : NodePath ):
36
+ all_list = self .list (target )
37
+ return list (filter (self .is_attribute , all_list ))
46
38
47
- self .action_finder .install_action ("." , "list.all" , list_all )
39
+ def list_actions_action (self , target : NodePath ):
40
+ # FIXME: use the same mechanism as in self.find_action
41
+ actions_branch = NodePath .join (target , '@actions' )
42
+ return self .list (actions_branch )
48
43
49
- def list_attributes ( tree : NodeTreeRoot , target : NodePath , action : NodePath ):
50
- all_list = tree . list ( target )
51
- return list ( filter ( self .is_attribute , all_list ) )
44
+ def remove_action ( self , target : NodePath ):
45
+ # CHECK: use 'path' argument?
46
+ return self .remove ( target )
52
47
53
- self .action_finder .install_action ("." , "list.attributes" , list_attributes )
54
-
55
- def list_actions (tree : NodeTreeRoot , target : NodePath , action : NodePath ):
56
- # FIXME: use self.action_finder or other way of listing available actions
57
- from contextshell .ActionFinder import ActionFinder
58
- actions_branch = NodePath .join (target , ActionFinder .actions_branch_name )
59
- return tree .list (actions_branch )
60
-
61
- self .action_finder .install_action ("." , "list.actions" , list_actions )
62
-
63
- def remove (tree : NodeTreeRoot , target : NodePath , action : NodePath ):
64
- return tree .remove (target )
65
-
66
- self .action_finder .install_action ("." , "remove" , remove )
48
+ def install_default_actions (self ):
49
+ self .install_global_action (action_from_function (self .create_action ))
50
+ self .install_global_action (action_from_function (self .exists_action ))
51
+ self .install_global_action (action_from_function (self .get_action ))
52
+ self .install_global_action (action_from_function (self .set_action ))
53
+ self .install_global_action (action_from_function (self .list_action ))
54
+ self .install_global_action (action_from_function (self .list_all_action ))
55
+ self .install_global_action (action_from_function (self .list_attributes_action ))
56
+ self .install_global_action (action_from_function (self .list_actions_action ))
57
+ self .install_global_action (action_from_function (self .remove_action ))
67
58
68
59
def install_global_action (self , action : Action ):
69
60
self .install_action (NodePath ('.' ), action )
@@ -93,7 +84,7 @@ def _find_action_in(self, target: NodePath, action: NodePath) -> Optional[Action
93
84
return action_implementation
94
85
95
86
def _is_action_implementation (self , node_value ) -> bool :
96
- return isinstance (node_value , Callable )
87
+ return isinstance (node_value , Action )
97
88
98
89
# def install_type(self, type: NodeType):
99
90
# raise NotImplementedError()
@@ -115,10 +106,13 @@ def is_action(self, path: NodePath):
115
106
return self ._is_action_implementation (node_value )
116
107
117
108
def execute (self , target : NodePath , action : NodePath , * args ):
118
- action_impl = self .action_finder .find_action (target , action )
109
+ #print("Execute: {}: {} {}".format(target, action, args))
110
+ action_impl = self .find_action (target , action )
119
111
if action_impl is None :
120
112
raise NameError ("Could not find action named '{}'" .format (action ))
121
- return action_impl (self , target , action , * args )
113
+ from collections import OrderedDict # FIXME: Temporary, until execute interface changes
114
+ arguments = OrderedDict (enumerate (args ))
115
+ return action_impl .invoke (target , action , arguments )
122
116
123
117
def create_node (self , value ):
124
118
return Node (value )
0 commit comments