aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/stateobject.py
blob: 5e4ae6e3db571bb261a99ea0c96e9a2e8ad23f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import absolute_import, print_function, division

import six
from typing import Any
from typing import List

import netlib.basetypes


def _is_list(cls):
    # The typing module is broken on Python 3.5.0, fixed on 3.5.1.
    is_list_bugfix = getattr(cls, "__origin__", False) == getattr(List[Any], "__origin__", True)
    return issubclass(cls, List) or is_list_bugfix


class StateObject(netlib.basetypes.Serializable):

    """
    An object with serializable state.

    State attributes can either be serializable types(str, tuple, bool, ...)
    or StateObject instances themselves.
    """

    _stateobject_attributes = None
    """
    An attribute-name -> class-or-type dict containing all attributes that
    should be serialized. If the attribute is a class, it must implement the
    Serializable protocol.
    """

    def get_state(self):
        """
        Retrieve object state.
        """
        state = {}
        for attr, cls in six.iteritems(self._stateobject_attributes):
            val = getattr(self, attr)
            if val is None:
                state[attr] = None
            elif hasattr(val, "get_state"):
                state[attr] = val.get_state()
            elif _is_list(cls):
                state[attr] = [x.get_state() for x in val]
            else:
                state[attr] = val
        return state

    def set_state(self, state):
        """
        Load object state from data returned by a get_state call.
        """
        state = state.copy()
        for attr, cls in six.iteritems(self._stateobject_attributes):
            val = state.pop(attr)
            if val is None:
                setattr(self, attr, val)
            else:
                curr = getattr(self, attr)
                if hasattr(curr, "set_state"):
                    curr.set_state(val)
                elif hasattr(cls, "from_state"):
                    obj = cls.from_state(val)
                    setattr(self, attr, obj)
                elif _is_list(cls):
                    cls = cls.__parameters__[0] if cls.__parameters__ else cls.__args__[0]
                    setattr(self, attr, [cls.from_state(x) for x in val])
                else:  # primitive types such as int, str, ...
                    setattr(self, attr, cls(val))
        if state:
            raise RuntimeWarning("Unexpected State in __setstate__: {}".format(state))