aboutsummaryrefslogtreecommitdiffstats
path: root/t/__init__.py
blob: a714b9e2e209bf4a33178bbb7601fd629fd5cd21 (plain) (blame)
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
import json
import mop

CLASS_REGISTRY = {}

class InMemoryDatabase(object):
    def __init__(self):
        self.store = {}
        self.class_registry = {}

    def register_class(self, c):
        self.class_registry[c.get_name()] = c

    def insert(self, name, obj):
        data = self._get_repr(obj)
        self.store[name] = json.dumps(
            data,
            separators=(',', ':'),
            sort_keys=True
        )

    def lookup(self, name):
        if name in self.store:
            data = json.loads(self.store[name])
            if data["type"] == "plain":
                return data["data"]
            elif data["type"] == "object":
                metaclass = self.class_registry[data["class"]]
                return metaclass.create_instance(data["data"])
            else:
                raise Exception("unknown object type")
        else:
            raise Exception("object not in database")

    def _get_repr(self, obj):
        if type(obj) == type([]):
            return { "type": "plain", "data": obj }
        if type(obj) == type({}):
            return { "type": "plain", "data": obj }
        if type(obj) == type(""):
            return { "type": "plain", "data": obj }
        if type(obj) == type(0):
            return { "type": "plain", "data": obj }
        if type(obj) == type(True):
            return { "type": "plain", "data": obj }
        if type(obj) == type(None):
            return { "type": "plain", "data": obj }
        if hasattr(obj, 'isa') and obj.isa(mop.Object):
            self.register_class(obj.metaclass)
            return {
                "type": "object",
                "class": obj.metaclass.get_name(),
                "data": obj.slots,
            }
        raise Exception("unknown object type")