sync_struct/Notifier: do not pass root param to publish

This commit is contained in:
Sebastien Bourdeauducq 2015-07-12 14:50:46 +02:00
parent 0e92cfe053
commit 8b02b58a77
2 changed files with 23 additions and 22 deletions

View File

@ -13,6 +13,7 @@ immutable types. Lists and dicts can be nested arbitrarily.
import asyncio import asyncio
from operator import getitem from operator import getitem
from functools import partial
from artiq.protocols import pyon from artiq.protocols import pyon
from artiq.protocols.asyncio_server import AsyncioServer from artiq.protocols.asyncio_server import AsyncioServer
@ -154,9 +155,9 @@ class Notifier:
""" """
self._backing_struct.append(x) self._backing_struct.append(x)
if self.root.publish is not None: if self.root.publish is not None:
self.root.publish(self.root, {"action": "append", self.root.publish({"action": "append",
"path": self._path, "path": self._path,
"x": x}) "x": x})
def insert(self, i, x): def insert(self, i, x):
"""Insert an element into a list. """Insert an element into a list.
@ -164,9 +165,9 @@ class Notifier:
""" """
self._backing_struct.insert(i, x) self._backing_struct.insert(i, x)
if self.root.publish is not None: if self.root.publish is not None:
self.root.publish(self.root, {"action": "insert", self.root.publish({"action": "insert",
"path": self._path, "path": self._path,
"i": i, "x": x}) "i": i, "x": x})
def pop(self, i=-1): def pop(self, i=-1):
"""Pop an element from a list. The returned element is not """Pop an element from a list. The returned element is not
@ -176,25 +177,25 @@ class Notifier:
""" """
r = self._backing_struct.pop(i) r = self._backing_struct.pop(i)
if self.root.publish is not None: if self.root.publish is not None:
self.root.publish(self.root, {"action": "pop", self.root.publish({"action": "pop",
"path": self._path, "path": self._path,
"i": i}) "i": i})
return r return r
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._backing_struct.__setitem__(key, value) self._backing_struct.__setitem__(key, value)
if self.root.publish is not None: if self.root.publish is not None:
self.root.publish(self.root, {"action": "setitem", self.root.publish({"action": "setitem",
"path": self._path, "path": self._path,
"key": key, "key": key,
"value": value}) "value": value})
def __delitem__(self, key): def __delitem__(self, key):
self._backing_struct.__delitem__(key) self._backing_struct.__delitem__(key)
if self.root.publish is not None: if self.root.publish is not None:
self.root.publish(self.root, {"action": "delitem", self.root.publish({"action": "delitem",
"path": self._path, "path": self._path,
"key": key}) "key": key})
def __getitem__(self, key): def __getitem__(self, key):
item = getitem(self._backing_struct, key) item = getitem(self._backing_struct, key)
@ -217,7 +218,7 @@ class Publisher(AsyncioServer):
self._notifier_names = {id(v): k for k, v in notifiers.items()} self._notifier_names = {id(v): k for k, v in notifiers.items()}
for notifier in notifiers.values(): for notifier in notifiers.values():
notifier.publish = self.publish notifier.publish = partial(self.publish, notifier)
@asyncio.coroutine @asyncio.coroutine
def _handle_connection_cr(self, reader, writer): def _handle_connection_cr(self, reader, writer):
@ -256,8 +257,8 @@ class Publisher(AsyncioServer):
finally: finally:
writer.close() writer.close()
def publish(self, notifier, obj): def publish(self, notifier, mod):
line = pyon.encode(obj) + "\n" line = pyon.encode(mod) + "\n"
line = line.encode() line = line.encode()
notifier_name = self._notifier_names[id(notifier)] notifier_name = self._notifier_names[id(notifier)]
for recipient in self._recipients[notifier_name]: for recipient in self._recipients[notifier_name]:

View File

@ -67,7 +67,7 @@ class SchedulerCase(unittest.TestCase):
expect = _get_basic_steps(1, expid) expect = _get_basic_steps(1, expid)
done = asyncio.Event() done = asyncio.Event()
expect_idx = 0 expect_idx = 0
def notify(notifier, mod): def notify(mod):
nonlocal expect_idx nonlocal expect_idx
self.assertEqual(mod, expect[expect_idx]) self.assertEqual(mod, expect[expect_idx])
expect_idx += 1 expect_idx += 1
@ -104,7 +104,7 @@ class SchedulerCase(unittest.TestCase):
background_running = asyncio.Event() background_running = asyncio.Event()
done = asyncio.Event() done = asyncio.Event()
expect_idx = 0 expect_idx = 0
def notify(notifier, mod): def notify(mod):
nonlocal expect_idx nonlocal expect_idx
if mod == {"path": [0], if mod == {"path": [0],
"value": "running", "value": "running",
@ -138,7 +138,7 @@ class SchedulerCase(unittest.TestCase):
first_preparing = asyncio.Event() first_preparing = asyncio.Event()
done = asyncio.Event() done = asyncio.Event()
expect_idx = 0 expect_idx = 0
def notify(notifier, mod): def notify(mod):
nonlocal expect_idx nonlocal expect_idx
if mod == {"path": [0], if mod == {"path": [0],
"value": "preparing", "value": "preparing",