Skip to content

Commit 0c154a3

Browse files
committed
Rename interface TreeRoot -> ActionExecutor
1 parent 808bf81 commit 0c154a3

17 files changed

+73
-56
lines changed

contextshell/Action.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
from contextshell.NodePath import NodePath
3-
from contextshell.TreeRoot import ActionArgsPack
3+
from contextshell.ActionExecutor import ActionArgsPack
44

55

66
class Action(ABC):

contextshell/ActionEndpoint.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from abc import ABC, abstractmethod
2+
from typing import Optional
3+
4+
from contextshell.Action import Action
5+
from contextshell.ActionExecutor import ActionExecutor, ActionArgsPack
6+
from contextshell.NodePath import NodePath
7+
from collections import OrderedDict
8+
9+
10+
class ActionEndpoint(ActionExecutor):
11+
@abstractmethod
12+
def find_action(self, target: NodePath, action: NodePath) -> Optional[Action]:
13+
raise NotImplementedError()
14+
15+
def execute(self, target: NodePath, action_name: NodePath, args: ActionArgsPack = None):
16+
#print("Execute: {}: {} {}".format(target, action, args))
17+
if not args:
18+
args = OrderedDict()
19+
action_impl = self.find_action(target, action_name)
20+
if action_impl is None:
21+
raise NameError("Could not find action named '{}'".format(action_name))
22+
return action_impl.invoke(target, action_name, args)
23+

contextshell/TreeRoot.py renamed to contextshell/ActionExecutor.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
from abc import ABC, abstractmethod
2+
from collections import OrderedDict
3+
24
from contextshell.NodePath import NodePath
35
from typing import Dict, Union, Any, Tuple, List
4-
from collections import OrderedDict
56

67
ArgumentValue = Any
78
ActionArgsPack = Dict[Union[NodePath, int], ArgumentValue]
89
PositionalArguments = List[ArgumentValue]
910
KeywordArguments = Dict[str, ArgumentValue]
1011

1112

12-
# CHECK: Rename TreeRoot to ActionEndpoint or something more appropriate?
13-
class TreeRoot(ABC):
13+
class ActionExecutor(ABC):
14+
"""Interface for backends allowing execution of arbitrary actions"""
1415
@abstractmethod
15-
def execute(self, target: NodePath, action: NodePath, args: ActionArgsPack = None):
16+
def execute(self, target: NodePath, action_name: NodePath, args: ActionArgsPack = None):
1617
raise NotImplementedError()
1718

1819

contextshell/CallableAction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Callable
2-
from contextshell.TreeRoot import unpack_argument_tree, ActionArgsPack
2+
from contextshell.ActionExecutor import unpack_argument_tree, ActionArgsPack
33
from contextshell.Action import Action
44
from contextshell.NodePath import NodePath
55

contextshell/CommandInterpreter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from contextshell.Command import Command
22
from contextshell.NodePath import NodePath
3-
from contextshell.TreeRoot import TreeRoot, parse_argument_tree
3+
from contextshell.ActionExecutor import ActionExecutor, parse_argument_tree
44

55

66
class CommandInterpreter:
7-
def __init__(self, tree: TreeRoot) -> None:
7+
def __init__(self, tree: ActionExecutor) -> None:
88
self.tree = tree
99

1010
def execute(self, command: Command):

contextshell/VirtualTree.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from contextshell.TreeRoot import TreeRoot, ActionArgsPack
1+
from contextshell.ActionExecutor import ActionExecutor, ActionArgsPack
22
from contextshell.NodePath import NodePath
33
from collections import OrderedDict
44

55

6-
class VirtualTree(TreeRoot):
6+
class VirtualTree(ActionExecutor):
77
"""Abstract frontend allowing embedding (mounting) of more specific tree roots"""
88

99
def __init__(self):
10-
self.mounts: OrderedDict[NodePath, TreeRoot] = OrderedDict()
10+
self.mounts: OrderedDict[NodePath, ActionExecutor] = OrderedDict()
1111

1212
# TODO: rename to attach/detach
13-
def mount(self, path: NodePath, root: TreeRoot):
13+
def mount(self, path: NodePath, root: ActionExecutor):
1414
if path.is_relative:
1515
raise ValueError("Could not mount relative path")
1616
if path in self.mounts:

contextshell/backends/Filesystem.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from contextshell.NodePath import NodePath
2-
from contextshell.TreeRoot import TreeRoot, ActionArgsPack
2+
from contextshell.ActionExecutor import ActionExecutor, ActionArgsPack
33
from contextshell.backends.Module import Module
44

55

6-
class FilesystemRoot(TreeRoot):
6+
class FilesystemRoot(ActionExecutor):
77
def __init__(self, root_directory_path: str):
88
self.root_directory_path = root_directory_path
99

contextshell/backends/NodeTree.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from contextshell.Node import Node
22
from contextshell.NodePath import NodePath
3-
from contextshell.TreeRoot import TreeRoot, ActionArgsPack, OrderedDict
3+
from contextshell.ActionEndpoint import ActionEndpoint
4+
from contextshell.ActionExecutor import ActionArgsPack, OrderedDict
45
from contextshell.Action import Action
56
from contextshell.CallableAction import action_from_function
67
from typing import List, Optional
78

89

910
# CHECK: how to implement TemporaryTreeRoot (based on NodeTreeRoot)
10-
class NodeTreeRoot(TreeRoot):
11+
class NodeTreeRoot(ActionEndpoint):
1112
"""Frontend to the (passive) node-based data storage"""
1213
def __init__(self):
1314
self.root = self.create_node(None)
@@ -118,15 +119,6 @@ def find_type(self, target: NodePath, type_name: NodePath): # TODO: add type-hi
118119
return None
119120
return type_node.get()
120121

121-
def execute(self, target: NodePath, action_name: NodePath, args: ActionArgsPack = None):
122-
#print("Execute: {}: {} {}".format(target, action, args))
123-
if not args:
124-
args = OrderedDict()
125-
action_impl = self.find_action(target, action_name)
126-
if action_impl is None:
127-
raise NameError("Could not find action named '{}'".format(action_name))
128-
return action_impl.invoke(target, action_name, args)
129-
130122
def create_node(self, value):
131123
return Node(value)
132124

repl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/python3.6
22
import readline
33

4-
from contextshell.TreeRoot import *
4+
from contextshell.ActionExecutor import ActionExecutor
55
from contextshell.Shell import *
66

7-
def create_context_tree() -> TreeRoot:
7+
def create_context_tree() -> ActionExecutor:
88
pass
99

1010
if __name__ == '__main__':

script.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/python3
22
import sys
33

4-
from TreeRoot import *
4+
from ActionEndpoint import *
55

66
from contextshell.Shell import *
77

88
if len(sys.argv) < 2:
99
raise ValueError('Script requires single argument')
1010

11-
tree = TreeRoot()
11+
tree = ActionEndpoint()
1212
shell = Shell(tree)
1313

1414
with open(sys.argv[1]) as script:

test_coverage.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
rm -r htmlcov
2+
rm -r htmlcov || true
33
coverage3 erase
44
coverage3 run --source=contextshell -m unittest discover -s tests -t tests -p '*Tests.py'
55
coverage3 report

tests/functional/FilesystemTests.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import os
33
import tempfile
4-
from contextshell.TreeRoot import TreeRoot
4+
from contextshell.ActionExecutor import ActionExecutor
55
from contextshell.VirtualTree import VirtualTree
66
from tests.functional.ShellTestsBase import TreeRootTestsBase
77
from tests.functional.TestExecutor import script_test
@@ -11,7 +11,7 @@
1111
class FilesystemTestsBase(TreeRootTestsBase):
1212
test_directory_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_data')
1313

14-
def create_tree_root(self) -> TreeRoot:
14+
def create_tree_root(self) -> ActionExecutor:
1515
self.test_directory = tempfile.TemporaryDirectory(FilesystemTestsBase.__name__)
1616
return FilesystemRoot(self.test_directory.name) # FIXME: make this work
1717

@@ -24,6 +24,7 @@ def tearDown(self):
2424
super().tearDown()
2525

2626

27+
@unittest.skip("Implement when actions could be registered in FilesystemTreeRoot")
2728
class FilesystemRootTests(FilesystemTestsBase):
2829
def setUp(self):
2930
super().setUp()
@@ -39,7 +40,7 @@ def test_contains_existing_file(self):
3940

4041
@unittest.skip("Those tests utilize attach actions which may not belong to the filesystem module")
4142
class AttachTests(TreeRootTestsBase):
42-
def create_tree_root(self) -> TreeRoot:
43+
def create_tree_root(self) -> ActionExecutor:
4344
pass
4445

4546
def configure_virtual_tree(self, virtual_tree: VirtualTree):

tests/functional/ShellTestsBase.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from abc import ABC, abstractmethod
33

4-
from contextshell.TreeRoot import TreeRoot
4+
from contextshell.ActionExecutor import ActionExecutor
55
from contextshell.Shell import Shell
66
from contextshell.NodePath import NodePath
77
from contextshell.CommandInterpreter import CommandInterpreter
@@ -17,7 +17,7 @@ def create_shell(self) -> Shell:
1717

1818
class TreeRootTestsBase(ShellScriptTestsBase):
1919
@abstractmethod
20-
def create_tree_root(self) -> TreeRoot:
20+
def create_tree_root(self) -> ActionExecutor:
2121
raise NotImplementedError()
2222

2323
def create_shell(self):
@@ -28,16 +28,16 @@ def create_shell(self):
2828
shell = Shell(interpreter)
2929
return shell
3030

31-
def configure_tree_root(self, tree_root: TreeRoot):
31+
def configure_tree_root(self, tree_root: ActionExecutor):
3232
pass
3333

3434

3535
# TODO: is this class needed when testing single TreeRoot-based class?
3636
class VirtualTreeTestsBase(TreeRootTestsBase):
37-
def create_tree_root(self) -> TreeRoot:
37+
def create_tree_root(self) -> ActionExecutor:
3838
return VirtualTree()
3939

40-
def configure_tree_root(self, tree_root: TreeRoot):
40+
def configure_tree_root(self, tree_root: ActionExecutor):
4141
self.configure_virtual_tree(tree_root)
4242

4343

tests/unit/ArgumentPackerTests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from contextshell.TreeRoot import pack_argument_tree, unpack_argument_tree, parse_argument_tree
2+
from contextshell.ActionExecutor import pack_argument_tree, unpack_argument_tree, parse_argument_tree
33
from contextshell.NodePath import NodePath as np
44
from collections import OrderedDict
55

tests/unit/Fakes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from contextshell.NodePath import NodePath
22
from contextshell.Action import Action
33
from typing import Dict
4-
from contextshell.TreeRoot import ActionArgsPack
4+
from contextshell.ActionEndpoint import ActionArgsPack
55

66

77
class FakeTree:

tests/unit/NodeTreeActionsTests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from contextshell.NodePath import NodePath as np
55
from contextshell.NodePath import NodePath
6-
from contextshell.TreeRoot import TreeRoot
6+
from contextshell.ActionEndpoint import ActionEndpoint
77

88

99
# class ActionTests(unittest.TestCase):

tests/unit/VirtualTreeTests.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from contextshell.TreeRoot import TreeRoot, ActionArgsPack, OrderedDict, pack_argument_tree
3+
from contextshell.ActionExecutor import ActionExecutor, ActionArgsPack, OrderedDict, pack_argument_tree
44
from contextshell.NodePath import NodePath
55

66

@@ -13,7 +13,7 @@ def np(representation):
1313
return NodePath(representation)
1414

1515

16-
class FakeTreeRoot(TreeRoot):
16+
class FakeActionExecutor(ActionExecutor):
1717
def __init__(self):
1818
self.execute_target = None
1919
self.execute_action = None
@@ -30,14 +30,14 @@ def execute(self, target: NodePath, action: NodePath, args: ActionArgsPack = Non
3030
class MountTests(unittest.TestCase):
3131
def test_relative_target(self):
3232
vt = create_virtual_tree()
33-
tree_root = FakeTreeRoot()
33+
tree_root = FakeActionExecutor()
3434

3535
with self.assertRaises(ValueError):
3636
vt.mount(np("foo"), tree_root)
3737

3838
def test_mount_visible_in_mapping(self):
3939
vt = create_virtual_tree()
40-
tree_root = FakeTreeRoot()
40+
tree_root = FakeActionExecutor()
4141

4242
vt.mount(np("."), tree_root)
4343

@@ -46,14 +46,14 @@ def test_mount_visible_in_mapping(self):
4646
def test_mount_on_same_path(self):
4747
vt = create_virtual_tree()
4848
mount_path = np(".")
49-
vt.mount(mount_path, FakeTreeRoot())
49+
vt.mount(mount_path, FakeActionExecutor())
5050

5151
with self.assertRaises(KeyError):
52-
vt.mount(mount_path, FakeTreeRoot())
52+
vt.mount(mount_path, FakeActionExecutor())
5353

5454
def test_umount_removes_mapping(self):
5555
vt = create_virtual_tree()
56-
vt.mount(np("."), FakeTreeRoot())
56+
vt.mount(np("."), FakeActionExecutor())
5757

5858
vt.umount(np("."))
5959

@@ -63,22 +63,22 @@ def test_umount_removes_mapping(self):
6363
class ExecuteTests(unittest.TestCase):
6464
def test_relative_target(self):
6565
vt = create_virtual_tree()
66-
vt.mount(np("."), FakeTreeRoot())
66+
vt.mount(np("."), FakeActionExecutor())
6767

6868
with self.assertRaises(ValueError):
6969
vt.execute(np("foo"), np("action"))
7070

7171
def test_no_matching_provider(self):
7272
vt = create_virtual_tree()
73-
vt.mount(np(".foo"), FakeTreeRoot())
73+
vt.mount(np(".foo"), FakeActionExecutor())
7474

7575
with self.assertRaises(RuntimeError):
7676
vt.execute(np(".bar"), np("action"))
7777

7878
def test_target_remapping(self):
7979
"""Target path is remapped to match provider's root"""
8080
vt = create_virtual_tree()
81-
tree_root = FakeTreeRoot()
81+
tree_root = FakeActionExecutor()
8282
vt.mount(np(".foo"), tree_root)
8383

8484
vt.execute(np(".foo.bar"), np("action"))
@@ -87,7 +87,7 @@ def test_target_remapping(self):
8787

8888
def test_action_forwarding(self):
8989
vt = create_virtual_tree()
90-
tree_root = FakeTreeRoot()
90+
tree_root = FakeActionExecutor()
9191
vt.mount(np("."), tree_root)
9292

9393
vt.execute(np("."), np("action"))
@@ -96,7 +96,7 @@ def test_action_forwarding(self):
9696

9797
def test_args_forwarding(self):
9898
vt = create_virtual_tree()
99-
tree_root = FakeTreeRoot()
99+
tree_root = FakeActionExecutor()
100100
vt.mount(np("."), tree_root)
101101
packed_args = pack_argument_tree('foo', 123)
102102

@@ -106,7 +106,7 @@ def test_args_forwarding(self):
106106

107107
def test_return_value_forwarding(self):
108108
vt = create_virtual_tree()
109-
tree_root = FakeTreeRoot()
109+
tree_root = FakeActionExecutor()
110110
tree_root.execute_return = 'RETURN_VALUE'
111111
vt.mount(np("."), tree_root)
112112

@@ -116,8 +116,8 @@ def test_return_value_forwarding(self):
116116

117117
def test_most_specific_provider_is_matched(self):
118118
vt = create_virtual_tree()
119-
short_path_root = FakeTreeRoot()
120-
long_path_root = FakeTreeRoot()
119+
short_path_root = FakeActionExecutor()
120+
long_path_root = FakeActionExecutor()
121121
vt.mount(np(".foo"), short_path_root)
122122
vt.mount(np(".foo.bar"), long_path_root)
123123

0 commit comments

Comments
 (0)