diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-06-10 11:44:51 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-06-10 11:44:51 +0200 |
commit | f39b4f955e855897414610c729fc6da9ab2df2f1 (patch) | |
tree | 252d2ccfba54255203c39ecf257c176a5614feb7 | |
parent | 643e6c89834728af8746249ba856cf65279cb102 (diff) |
Add ctl command to add arbitrary objects
-rw-r--r-- | pyapd/app.py | 2 | ||||
-rw-r--r-- | pyapd/ctl.py | 36 | ||||
-rw-r--r-- | tests/test_ctl.py | 32 |
3 files changed, 51 insertions, 19 deletions
diff --git a/pyapd/app.py b/pyapd/app.py index 39948ee..a1ef7bf 100644 --- a/pyapd/app.py +++ b/pyapd/app.py @@ -25,7 +25,7 @@ class App(tornado.web.Application): def __init__(self, config, *args, **kw): self.config = config self.store = getattr(stores, self.config.backend).Store() - self.ctl = ctl.CtlServer(config) + self.ctl = ctl.CtlServer(self) urls = [ (r'/', RootHandler), URLSpec(r'/object/(.*)', handler=ObjectHandler), diff --git a/pyapd/ctl.py b/pyapd/ctl.py index 8be523d..fa86ccd 100644 --- a/pyapd/ctl.py +++ b/pyapd/ctl.py @@ -7,21 +7,29 @@ import tornado.netutil import tornado.ioloop import tornado.iostream +from . import objects +from . import app as apd_app + class Commands(): - def __init__(self): - pass + def __init__(self, srv): + self.srv = srv def ping(self): return "pong" + def add_object(self, obj: dict): + new_obj = objects.Object(**obj) + self.srv.app.store.add(new_obj) + return "ok" + class CtlServer(): - def __init__(self, config): - self.commands = Commands() - self.config = config + def __init__(self, app): + self.commands = Commands(self) + self.app = app self.ctl_socket = tornado.netutil.bind_unix_socket( - self.config.ctl_socket, + self.app.config.ctl_socket, ) io_loop = tornado.ioloop.IOLoop.current() io_loop.add_handler( @@ -50,14 +58,16 @@ class CtlServer(): # stream.write(res.encode()) print("command result was:", res) - async def get_command(self, message): + async def get_command(self, message: str): args = json.loads(message) - cmd = args.pop('command') - try: - res = getattr(self.commands, cmd)(*args) - except AttributeError: + cmd_name = args.pop('command') + cmd = getattr(self.commands, cmd_name) + if not cmd: res = "no such command" - except TypeError: - res = "syntax error" + else: + try: + res = cmd(**args) + except TypeError: + res = "syntax error" # TODO: wrap res in a sensible json return res diff --git a/tests/test_ctl.py b/tests/test_ctl.py index ddac775..f58ce5a 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -3,18 +3,40 @@ import tempfile from tornado.testing import AsyncTestCase, gen_test -from pyapd import ctl, config +from pyapd import ctl, config, app -class TestCtlServer(AsyncTestCase): +class TestCtlServerCommands(AsyncTestCase): def setUp(self): super().setUp() self.tempdir = tempfile.TemporaryDirectory() - conf = config.Config('tests/data/test_config.yaml') - conf.ctl_socket = os.path.join(self.tempdir.name, 'pyapd.sock') - self.ctl_server = ctl.CtlServer(conf) + test_app = app.App(config.Config('tests/data/test_config.yaml')) + test_app.config.ctl_socket = os.path.join( + self.tempdir.name, + 'pyapd.sock' + ) + self.ctl_server = ctl.CtlServer(test_app) @gen_test def test_ping_command(self): res = yield self.ctl_server.get_command('{"command": "ping"}') self.assertIn('pong', res) + + @gen_test + def test_add_object_command(self): + self.assertEqual(len(self.ctl_server.app.store.objects), 0) + obj = ''' + { + "id": "http://example.org/123456", + "type": "object" + } + ''' + res = yield (self.ctl_server.get_command(''' + {{ + "command": "add_object", + "obj": {obj} + }} + '''.format(obj=obj) + )) + self.assertIn('ok', res) + self.assertEqual(len(self.ctl_server.app.store.objects), 1) |