mitmproxy/libmproxy/stateobject.py

84 lines
2.7 KiB
Python
Raw Normal View History

from __future__ import absolute_import
2014-09-16 21:54:17 +00:00
class StateObject(object):
2014-02-07 02:56:57 +00:00
def _get_state(self):
raise NotImplementedError # pragma: nocover
2014-01-31 00:06:35 +00:00
2014-02-07 02:56:57 +00:00
def _load_state(self, state):
raise NotImplementedError # pragma: nocover
2014-01-31 00:06:35 +00:00
@classmethod
2014-02-07 02:56:57 +00:00
def _from_state(cls, state):
raise NotImplementedError # pragma: nocover
2014-02-07 01:36:39 +00:00
# Usually, this function roughly equals to the following code:
# f = cls()
# f._load_state(state)
# return f
2014-01-31 00:06:35 +00:00
def __eq__(self, other):
try:
return self._get_state() == other._get_state()
2014-09-16 21:54:17 +00:00
except AttributeError:
# we may compare with something that's not a StateObject
2014-01-31 00:06:35 +00:00
return False
2014-09-04 17:08:54 +00:00
def __ne__(self, other):
return not self.__eq__(other)
2014-01-31 00:06:35 +00:00
class SimpleStateObject(StateObject):
"""
A StateObject with opionated conventions that tries to keep everything DRY.
2014-09-16 21:54:17 +00:00
Simply put, you agree on a list of attributes and their type. Attributes can
either be primitive types(str, tuple, bool, ...) or StateObject instances
themselves. SimpleStateObject uses this information for the default
_get_state(), _from_state(s) and _load_state(s) methods. Overriding
_get_state or _load_state to add custom adjustments is always possible.
2014-01-31 00:06:35 +00:00
"""
_stateobject_attributes = None # none by default to raise an exception if definition was forgotten
"""
2014-09-16 21:54:17 +00:00
An attribute-name -> class-or-type dict containing all attributes that
should be serialized If the attribute is a class, this class must be a
subclass of StateObject.
2014-01-31 00:06:35 +00:00
"""
def _get_state(self):
return {attr: self._get_state_attr(attr, cls)
for attr, cls in self._stateobject_attributes.iteritems()}
def _get_state_attr(self, attr, cls):
"""
helper for _get_state.
returns the value of the given attribute
"""
val = getattr(self, attr)
if hasattr(val, "_get_state"):
return val._get_state()
else:
return val
def _load_state(self, state):
for attr, cls in self._stateobject_attributes.iteritems():
self._load_state_attr(attr, cls, state)
def _load_state_attr(self, attr, cls, state):
"""
helper for _load_state.
loads the given attribute from the state.
"""
if state.get(attr, None) is None:
2014-01-31 00:06:35 +00:00
setattr(self, attr, None)
return
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:
2014-09-16 21:54:17 +00:00
setattr(self, attr, cls(state[attr]))