management/sync_struct: export process_mod function

This commit is contained in:
Sebastien Bourdeauducq 2015-01-14 11:36:04 +08:00
parent 6c24e699fd
commit 4160d24abc
1 changed files with 20 additions and 14 deletions

View File

@ -8,6 +8,24 @@ from artiq.management.tools import AsyncioServer
_init_string = b"ARTIQ sync_struct\n" _init_string = b"ARTIQ sync_struct\n"
def process_mod(target, mod):
for key in mod["path"]:
target = getitem(target, key)
action = mod["action"]
if action == "append":
target.append(mod["x"])
elif action == "insert":
target.insert(mod["i"], mod["x"])
elif action == "pop":
target.pop(mod["i"])
elif action == "setitem":
target.__setitem__(mod["key"], mod["value"])
elif action == "delitem":
target.__delitem__(mod["key"])
else:
raise ValueError
class Subscriber: class Subscriber:
def __init__(self, notifier_name, target_builder, notify_cb=None): def __init__(self, notifier_name, target_builder, notify_cb=None):
self.notifier_name = notifier_name self.notifier_name = notifier_name
@ -49,23 +67,11 @@ class Subscriber:
if not line: if not line:
return return
obj = pyon.decode(line.decode()) obj = pyon.decode(line.decode())
action = obj["action"]
if action == "init": if obj["action"] == "init":
target = self.target_builder(obj["struct"]) target = self.target_builder(obj["struct"])
else: else:
for key in obj["path"]: process_mod(target, obj)
target = getitem(target, key)
if action == "append":
target.append(obj["x"])
elif action == "insert":
target.insert(obj["i"], obj["x"])
elif action == "pop":
target.pop(obj["i"])
elif action == "setitem":
target.__setitem__(obj["key"], obj["value"])
elif action == "delitem":
target.__delitem__(obj["key"])
if self.notify_cb is not None: if self.notify_cb is not None:
self.notify_cb() self.notify_cb()