2014-03-10 21:36:47 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2014-09-16 21:54:17 +00:00
|
|
|
|
2014-01-31 03:45:39 +00:00
|
|
|
class StateObject(object):
|
2014-01-31 00:06:35 +00:00
|
|
|
"""
|
2014-09-16 23:35:14 +00:00
|
|
|
An object with serializable state.
|
2014-01-31 00:06:35 +00:00
|
|
|
|
2014-09-16 23:35:14 +00:00
|
|
|
State attributes can either be serializable types(str, tuple, bool, ...)
|
|
|
|
or StateObject instances themselves.
|
2014-01-31 00:06:35 +00:00
|
|
|
"""
|
2014-09-16 23:35:14 +00:00
|
|
|
# An attribute-name -> class-or-type dict containing all attributes that
|
2014-09-17 01:58:56 +00:00
|
|
|
# should be serialized. If the attribute is a class, it must implement the
|
|
|
|
# StateObject protocol.
|
2014-09-16 23:35:14 +00:00
|
|
|
_stateobject_attributes = None
|
2014-09-17 01:58:56 +00:00
|
|
|
# A set() of attributes that should be ignored for short state
|
|
|
|
_stateobject_long_attributes = frozenset([])
|
2014-01-31 00:06:35 +00:00
|
|
|
|
2014-09-21 20:46:48 +00:00
|
|
|
def from_state(self, state):
|
|
|
|
raise NotImplementedError()
|
2014-01-31 00:06:35 +00:00
|
|
|
|
2014-09-17 01:58:56 +00:00
|
|
|
def get_state(self, short=False):
|
|
|
|
"""
|
|
|
|
Retrieve object state. If short is true, return an abbreviated
|
|
|
|
format with long data elided.
|
|
|
|
"""
|
2014-09-16 23:35:14 +00:00
|
|
|
state = {}
|
|
|
|
for attr, cls in self._stateobject_attributes.iteritems():
|
2014-09-17 01:58:56 +00:00
|
|
|
if short and attr in self._stateobject_long_attributes:
|
|
|
|
continue
|
|
|
|
val = getattr(self, attr)
|
|
|
|
if hasattr(val, "get_state"):
|
|
|
|
state[attr] = val.get_state(short)
|
|
|
|
else:
|
|
|
|
state[attr] = val
|
2014-09-16 23:35:14 +00:00
|
|
|
return state
|
2014-09-16 21:54:17 +00:00
|
|
|
|
2014-09-16 23:35:14 +00:00
|
|
|
def load_state(self, state):
|
2014-09-17 01:58:56 +00:00
|
|
|
"""
|
|
|
|
Load object state from data returned by a get_state call.
|
|
|
|
"""
|
2014-09-16 23:35:14 +00:00
|
|
|
for attr, cls in self._stateobject_attributes.iteritems():
|
|
|
|
if state.get(attr, None) is None:
|
|
|
|
setattr(self, attr, None)
|
|
|
|
else:
|
|
|
|
curr = getattr(self, attr)
|
|
|
|
if hasattr(curr, "load_state"):
|
|
|
|
curr.load_state(state[attr])
|
|
|
|
elif hasattr(cls, "from_state"):
|
|
|
|
setattr(self, attr, cls.from_state(state[attr]))
|
|
|
|
else:
|
|
|
|
setattr(self, attr, cls(state[attr]))
|