2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-25 03:08:27 +08:00

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 artiq.gui.tools import Window, ListSyncer
from artiq.gui.tools import Window, ListSyncer, DictSyncer
from artiq.management.sync_struct import Subscriber
class _ParameterStoreSyncer:
def __init__(self, parameters_store, init):
self.parameters_store = parameters_store
self.parameters_store.clear()
for name, value in sorted(init.items(), key=itemgetter(0)):
self.parameters_store.append(self._convert(name, value))
class _ParameterStoreSyncer(DictSyncer):
def order_key(self, kv_pair):
return kv_pair[0]
def _convert(self, name, value):
def convert(self, name, 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):
def convert(self, x):

View File

@ -3,7 +3,7 @@ import asyncio
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
@ -16,16 +16,12 @@ class _QueueStoreSyncer(ListSyncer):
return row
class _PeriodicStoreSyncer:
def __init__(self, periodic_store, init):
self.periodic_store = periodic_store
self.periodic_store.clear()
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))
class _PeriodicStoreSyncer(DictSyncer):
def order_key(self, kv_pair):
# order by next run time, and then by PRID
return (kv_pair[1][0], kv_pair[0])
def _convert(self, prid, x):
def convert(self, prid, x):
next_run, run_params, timeout, period = x
row = [time.strftime("%m/%d %H:%M:%S", time.localtime(next_run)),
prid, run_params["file"]]
@ -34,34 +30,6 @@ class _PeriodicStoreSyncer:
row.append(str(period))
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):
def __init__(self):

View File

@ -29,3 +29,50 @@ class ListSyncer:
def __delitem__(self, 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