forked from M-Labs/artiq
1
0
Fork 0

gui: better state error handling

Remains limited by issue pyqtgraph/pyqtgraph#204
This commit is contained in:
Sebastien Bourdeauducq 2015-08-01 19:52:13 +08:00
parent 8ad88438c7
commit b2f720da67
1 changed files with 18 additions and 2 deletions

View File

@ -1,10 +1,14 @@
import asyncio import asyncio
from collections import OrderedDict from collections import OrderedDict
import logging
from artiq.tools import TaskObject from artiq.tools import TaskObject
from artiq.protocols import pyon from artiq.protocols import pyon
logger = logging.getLogger(__name__)
# support Qt CamelCase naming scheme for save/restore state # support Qt CamelCase naming scheme for save/restore state
def _save_state(obj): def _save_state(obj):
method = getattr(obj, "save_state", None) method = getattr(obj, "save_state", None)
@ -38,6 +42,8 @@ class StateManager(TaskObject):
try: try:
data = pyon.load_file(self.filename) data = pyon.load_file(self.filename)
except FileNotFoundError: except FileNotFoundError:
logger.info("State database '%s' not found, using defaults",
self.filename)
return return
# The state of one object may depend on the state of another, # The state of one object may depend on the state of another,
# e.g. the display state may create docks that are referenced in # e.g. the display state may create docks that are referenced in
@ -47,10 +53,20 @@ class StateManager(TaskObject):
for name, obj in reversed(list(self.stateful_objects.items())): for name, obj in reversed(list(self.stateful_objects.items())):
state = data.get(name, None) state = data.get(name, None)
if state is not None: if state is not None:
try:
_restore_state(obj, state) _restore_state(obj, state)
except:
logger.warning("Failed to restore state for object '%s'",
name, exc_info=True)
def save(self): def save(self):
data = {k: _save_state(v) for k, v in self.stateful_objects.items()} data = dict()
for k, v in self.stateful_objects.items():
try:
data[k] = _save_state(v)
except:
logger.warning("Failed to save state for object '%s'", k,
exc_info=True)
pyon.store_file(self.filename, data) pyon.store_file(self.filename, data)
@asyncio.coroutine @asyncio.coroutine