gui: factorize dict synchronization

This commit is contained in:
Sebastien Bourdeauducq 2015-01-02 22:09:02 +08:00
parent f352e7f752
commit 650baa9fc1
3 changed files with 58 additions and 69 deletions

View File

@ -4,43 +4,17 @@ import time
from gi.repository import Gtk from gi.repository import Gtk
from artiq.gui.tools import Window, ListSyncer from artiq.gui.tools import Window, ListSyncer, DictSyncer
from artiq.management.sync_struct import Subscriber from artiq.management.sync_struct import Subscriber
class _ParameterStoreSyncer: class _ParameterStoreSyncer(DictSyncer):
def __init__(self, parameters_store, init): def order_key(self, kv_pair):
self.parameters_store = parameters_store return kv_pair[0]
self.parameters_store.clear()
for name, value in sorted(init.items(), key=itemgetter(0)):
self.parameters_store.append(self._convert(name, value))
def _convert(self, name, value): def convert(self, name, value):
return [name, str(value)] return [name, str(value)]
def _find_index(self, name):
for i, e in enumerate(self.parameters_store):
if e[0] == name:
return i
raise KeyError
def __setitem__(self, name, value):
try:
i = self._find_index(name)
except KeyError:
pass
else:
del self.parameters_store[i]
j = len(self.parameters_store)
for i, e in enumerate(self.parameters_store):
if e[0] > name:
j = i
break
self.parameters_store.insert(j, self._convert(name, value))
def __delitem__(self, key):
del self.parameters_store[self._find_index(key)]
class _LastChangesStoreSyncer(ListSyncer): class _LastChangesStoreSyncer(ListSyncer):
def convert(self, x): def convert(self, x):

View File

@ -3,7 +3,7 @@ import asyncio
from gi.repository import Gtk from gi.repository import Gtk
from artiq.gui.tools import Window, ListSyncer from artiq.gui.tools import Window, ListSyncer, DictSyncer
from artiq.management.sync_struct import Subscriber from artiq.management.sync_struct import Subscriber
@ -16,16 +16,12 @@ class _QueueStoreSyncer(ListSyncer):
return row return row
class _PeriodicStoreSyncer: class _PeriodicStoreSyncer(DictSyncer):
def __init__(self, periodic_store, init): def order_key(self, kv_pair):
self.periodic_store = periodic_store # order by next run time, and then by PRID
self.periodic_store.clear() return (kv_pair[1][0], kv_pair[0])
self.order = []
for prid, x in sorted(init.items(), key=lambda e: (e[1][0], e[0])):
self.periodic_store.append(self._convert(prid, x))
self.order.append((x[0], prid))
def _convert(self, prid, x): def convert(self, prid, x):
next_run, run_params, timeout, period = x next_run, run_params, timeout, period = x
row = [time.strftime("%m/%d %H:%M:%S", time.localtime(next_run)), row = [time.strftime("%m/%d %H:%M:%S", time.localtime(next_run)),
prid, run_params["file"]] prid, run_params["file"]]
@ -34,34 +30,6 @@ class _PeriodicStoreSyncer:
row.append(str(period)) row.append(str(period))
return row return row
def _find_index(self, prid):
for i, e in enumerate(self.periodic_store):
if e[1] == prid:
return i
raise KeyError
def __setitem__(self, prid, x):
try:
i = self._find_index(prid)
except KeyError:
pass
else:
del self.periodic_store[i]
del self.order[i]
ord_el = (x[0], prid)
j = len(self.order)
for i, o in enumerate(self.order):
if o > ord_el:
j = i
break
self.periodic_store.insert(j, self._convert(prid, x))
self.order.insert(j, ord_el)
def __delitem__(self, key):
i = self._find_index(key)
del self.periodic_store[i]
del self.order[i]
class SchedulerWindow(Window): class SchedulerWindow(Window):
def __init__(self): def __init__(self):

View File

@ -29,3 +29,50 @@ class ListSyncer:
def __delitem__(self, key): def __delitem__(self, key):
del self.store[key] del self.store[key]
def convert(self, x):
raise NotImplementedError
class DictSyncer:
def __init__(self, store, init):
self.store = store
self.store.clear()
self.order = []
for k, v in sorted(init.items(), key=self.order_key):
self.store.append(self.convert(k, v))
self.order.append((k, self.order_key((k, v))))
def _find_index(self, key):
for i, e in enumerate(self.order):
if e[0] == key:
return i
raise KeyError
def __setitem__(self, key, value):
try:
i = self._find_index(key)
except KeyError:
pass
else:
del self.store[i]
del self.order[i]
ord_el = self.order_key((key, value))
j = len(self.order)
for i, (k, o) in enumerate(self.order):
if o > ord_el:
j = i
break
self.store.insert(j, self.convert(key, value))
self.order.insert(j, (key, ord_el))
def __delitem__(self, key):
i = self._find_index(key)
del self.store[i]
del self.order[i]
def order_key(self, kv_pair):
raise NotImplementedError
def convert(self, key, value):
raise NotImplementedError