aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-07-07 12:45:06 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-07-07 12:45:06 +0200
commit1396f2ef54b71d4344e28329a4d874a73e2e8e12 (patch)
treedd330f0547dcff34a67154461ef6f2949381a7a6
parent820ac976e61c2d908a35a2cb2914dffc9822e095 (diff)
Split the ctl_client in its own file, using guacamole.
-rw-r--r--README.rst1
-rw-r--r--mypy.ini3
-rw-r--r--pyapd/ctl.py22
-rw-r--r--pyapd/ctl_client.py28
-rw-r--r--tests/test_ctl.py21
-rw-r--r--tests/test_ctl_client.py23
6 files changed, 56 insertions, 42 deletions
diff --git a/README.rst b/README.rst
index 4b343f7..6e2d817 100644
--- a/README.rst
+++ b/README.rst
@@ -21,6 +21,7 @@ explicitely.
* `Python3 <https://www.python.org/>` >= 3.7
* `Tornado <https://www.tornadoweb.org>`_
* `ruamel.yaml <https://bitbucket.org/ruamel/yaml>`_ >= 0.15
+* `guacamole <https://github.com/zyga/guacamole/>`
License
=======
diff --git a/mypy.ini b/mypy.ini
index 4447f48..2db00c6 100644
--- a/mypy.ini
+++ b/mypy.ini
@@ -5,3 +5,6 @@ ignore_missing_imports = True
[mypy-tornado.*]
ignore_missing_imports = True
+
+[mypy-guacamole.*]
+ignore_missing_imports = True
diff --git a/pyapd/ctl.py b/pyapd/ctl.py
index b7021ec..1355d1c 100644
--- a/pyapd/ctl.py
+++ b/pyapd/ctl.py
@@ -7,7 +7,7 @@ import tornado.netutil
import tornado.ioloop
import tornado.iostream
-from . import objects, config
+from . import objects
class Commands():
@@ -70,23 +70,3 @@ class CtlServer():
res = "syntax error"
# TODO: wrap res in a sensible json
return res
-
-
-class CtlClient():
- def __init__(self, conf: config.Config):
- self.config = conf
-
- def send_command(self, cmd):
- print("Sending command", cmd)
- s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- s.connect(self.config.ctl_socket)
- s.send(json.dumps(cmd).encode())
-
- def ping(self):
- return {"command": "ping"}
-
- def add_object(self, obj: str):
- return {
- "command": "add_object",
- "obj": obj,
- }
diff --git a/pyapd/ctl_client.py b/pyapd/ctl_client.py
new file mode 100644
index 0000000..8c692f9
--- /dev/null
+++ b/pyapd/ctl_client.py
@@ -0,0 +1,28 @@
+import json
+import socket
+
+import guacamole
+
+
+class ClientCommand(guacamole.Command):
+ def send_command(self, cmd):
+ print("Sending command", cmd)
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ s.connect(self.config.ctl_socket)
+ s.send(json.dumps(cmd).encode())
+
+
+class Ping(ClientCommand):
+ def get_command(self):
+ return {"command": "ping"}
+
+ def invoked(self, ctx):
+ print(self.get_command)
+
+
+class AddObject(ClientCommand):
+ def get_command(self, obj: str):
+ return {
+ "command": "add_object",
+ "obj": obj,
+ }
diff --git a/tests/test_ctl.py b/tests/test_ctl.py
index ed6a5e4..f58ce5a 100644
--- a/tests/test_ctl.py
+++ b/tests/test_ctl.py
@@ -1,6 +1,5 @@
import os
import tempfile
-import unittest
from tornado.testing import AsyncTestCase, gen_test
@@ -41,23 +40,3 @@ class TestCtlServerCommands(AsyncTestCase):
))
self.assertIn('ok', res)
self.assertEqual(len(self.ctl_server.app.store.objects), 1)
-
-
-class TestCtlClient(unittest.TestCase):
- def setUp(self):
- self.client = ctl.CtlClient(
- config.Config('tests/data/test_config.yaml')
- )
-
- def test_cmd_ping(self):
- cmd = self.client.ping()
- self.assertEqual(cmd, {"command": "ping"})
-
- def test_cmd_add_object(self):
- obj = {
- "id": "http://example.org/123456",
- "type": "object",
- }
- cmd = self.client.add_object(obj)
- self.assertEqual(cmd['command'], 'add_object')
- self.assertEqual(cmd['obj'], obj)
diff --git a/tests/test_ctl_client.py b/tests/test_ctl_client.py
new file mode 100644
index 0000000..9b787c0
--- /dev/null
+++ b/tests/test_ctl_client.py
@@ -0,0 +1,23 @@
+import unittest
+
+from pyapd import ctl_client, config
+
+
+class TestCtlClient(unittest.TestCase):
+ def setUp(self):
+ self.config = config.Config('tests/data/test_config.yaml')
+
+ def test_cmd_ping(self):
+ ping = ctl_client.Ping()
+ cmd = ping.get_command()
+ self.assertEqual(cmd, {"command": "ping"})
+
+ def test_cmd_add_object(self):
+ obj = {
+ "id": "http://example.org/123456",
+ "type": "object",
+ }
+ add_object = ctl_client.AddObject()
+ cmd = add_object.get_command(obj)
+ self.assertEqual(cmd['command'], 'add_object')
+ self.assertEqual(cmd['obj'], obj)