forked from ellxc/piperbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Namespaces.py
165 lines (133 loc) · 4.14 KB
/
Namespaces.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from collections.abc import MutableMapping, Mapping
from collections import defaultdict
class MutableNameSpace(MutableMapping):
def __init__(self, data=None, all=False):
if data is None:
data = {}
self._data = data
self._all = all
def __repr__(self):
ret = "{"
ret += ", ".join(["{}: {}".format(repr(key), repr(val)) for key, val in self._data.items()])
ret += "}"
return ret
def __len__(self):
return len(self._data)
def __iter__(self):
return iter(self._data)
def __delitem__(self, key):
del self._data[key]
def __delattr__(self, item):
del self._data[item]
def setdefault(self, key, default=None):
if key in self._data:
return self._data[key]
else:
self._data[key] = default
def copy(self):
return MutableNameSpace(self._data.copy(), all=self._all)
def __getattr__(self, item):
if item in self._data:
ret = self._data[item]
if isinstance(ret, MutableMapping) and not isinstance(ret, MutableNameSpace):
return MutableNameSpace(ret, all=self._all)
else:
return ret
else:
if self._all:
self._data[item] = MutableNameSpace({}, all=self._all)
return self._data[item]
else:
raise KeyError('%s' % item)
def __setattr__(self, key, value):
if key.startswith("_"):
object.__setattr__(self, key, value)
elif isinstance(value, dict):
return self._data.__setitem__(key, MutableNameSpace(value, all=self._all))
else:
return self._data.__setitem__(key, value)
def __contains__(self, item):
return item in self._data
def __getitem__(self, key):
return self.__getattr__(key)
def __setitem__(self, key, value):
return self.__setattr__(key, value)
def __setstate__(self, vals):
self._data = vals["data"]
self._all = vals["all"]
def __getstate__(self):
return {
"data": self._data,
"all": self._all,
}
class ReadOnlyNameSpace(Mapping):
def __init__(self, data=None, all=False):
if data is None:
data = {}
self._data = data
self._all = all
def __getattr__(self, item):
return self.__getitem__(item)
def __setattr__(self, key, value):
if key.startswith("_"):
object.__setattr__(self, key, value)
else:
raise AttributeError("Read only")
def __getitem__(self, key):
if key in self._data or not self._all:
ret = self._data[key]
if self._all and isinstance(ret, MutableNameSpace):
return ReadOnlyNameSpace(ret._data, all=self._all)
if isinstance(ret, dict):
return ReadOnlyNameSpace(ret, all=self._all)
if isinstance(ret, list):
return list(ret)
if isinstance(ret, set):
return set(ret)
else:
return ret
else:
raise KeyError('%s' % key)
def __len__(self):
return len(self._data)
def __iter__(self):
return iter(self._data)
def __repr__(self):
ret = "{"
ret += ", ".join(["{}: {}".format(repr(key), repr(val)) for key, val in self._data.items()])
ret += "}"
return ret
def __getstate__(self):
return {
"data": self._data,
"all": self._all,
}
def __setstate__(self, vals):
self._data = vals["data"]
self._all = vals["all"]
bob = {
"karma": 5,
"seval": {},
"general": {
"foo": "bar",
"baz": 7,
}
}
jon = {
"karma": 5,
"seval": {},
"asd": {},
"general": {
"foo": "bar",
"baz": 7,
}
}
userst = defaultdict(dict, {
"bob": bob,
"jon": jon
})
users = MutableNameSpace(userst)
users.jon.channels = []
MutableNameSpace(userst).jon.channels.append("derp")
self = MutableNameSpace({}, all=True)
readonlyusers = ReadOnlyNameSpace(userst, all=True)