Mercurial > libervia-pubsub
comparison idavoll/memory_storage.py @ 198:e404775b12df
Change naming and spacing conventions to match Twisted's.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Tue, 10 Jun 2008 11:31:49 +0000 |
parents | fc687620599b |
children | 77c61e2b8c75 |
comparison
equal
deleted
inserted
replaced
197:9da5a95d408d | 198:e404775b12df |
---|---|
6 from twisted.internet import defer | 6 from twisted.internet import defer |
7 from twisted.words.protocols.jabber import jid | 7 from twisted.words.protocols.jabber import jid |
8 | 8 |
9 from idavoll import error, iidavoll | 9 from idavoll import error, iidavoll |
10 | 10 |
11 default_config = {"pubsub#persist_items": True, | 11 defaultConfig = {"pubsub#persist_items": True, |
12 "pubsub#deliver_payloads": True, | 12 "pubsub#deliver_payloads": True, |
13 "pubsub#send_last_published_item": 'on_sub', | 13 "pubsub#send_last_published_item": 'on_sub', |
14 "pubsub#node_type": "leaf"} | 14 "pubsub#node_type": "leaf"} |
15 | 15 |
16 class Storage: | 16 class Storage: |
18 implements(iidavoll.IStorage) | 18 implements(iidavoll.IStorage) |
19 | 19 |
20 def __init__(self): | 20 def __init__(self): |
21 self._nodes = {} | 21 self._nodes = {} |
22 | 22 |
23 def get_node(self, node_id): | 23 |
24 try: | 24 def getNode(self, nodeIdentifier): |
25 node = self._nodes[node_id] | 25 try: |
26 node = self._nodes[nodeIdentifier] | |
26 except KeyError: | 27 except KeyError: |
27 return defer.fail(error.NodeNotFound()) | 28 return defer.fail(error.NodeNotFound()) |
28 | 29 |
29 return defer.succeed(node) | 30 return defer.succeed(node) |
30 | 31 |
31 def get_node_ids(self): | 32 |
33 def getNodeIds(self): | |
32 return defer.succeed(self._nodes.keys()) | 34 return defer.succeed(self._nodes.keys()) |
33 | 35 |
34 def create_node(self, node_id, owner, config=None): | 36 |
35 if node_id in self._nodes: | 37 def createNode(self, nodeIdentifier, owner, config=None): |
38 if nodeIdentifier in self._nodes: | |
36 return defer.fail(error.NodeExists()) | 39 return defer.fail(error.NodeExists()) |
37 | 40 |
38 if not config: | 41 if not config: |
39 config = copy.copy(default_config) | 42 config = copy.copy(defaultConfig) |
40 | 43 |
41 if config['pubsub#node_type'] != 'leaf': | 44 if config['pubsub#node_type'] != 'leaf': |
42 raise NotImplementedError | 45 raise NotImplementedError |
43 | 46 |
44 node = LeafNode(node_id, owner, config) | 47 node = LeafNode(nodeIdentifier, owner, config) |
45 self._nodes[node_id] = node | 48 self._nodes[nodeIdentifier] = node |
46 | 49 |
47 return defer.succeed(None) | 50 return defer.succeed(None) |
48 | 51 |
49 def delete_node(self, node_id): | 52 |
50 try: | 53 def deleteNode(self, nodeIdentifier): |
51 del self._nodes[node_id] | 54 try: |
55 del self._nodes[nodeIdentifier] | |
52 except KeyError: | 56 except KeyError: |
53 return defer.fail(error.NodeNotFound()) | 57 return defer.fail(error.NodeNotFound()) |
54 | 58 |
55 return defer.succeed(None) | 59 return defer.succeed(None) |
56 | 60 |
57 def get_affiliations(self, entity): | 61 |
62 def getAffiliations(self, entity): | |
58 entity = entity.userhost() | 63 entity = entity.userhost() |
59 return defer.succeed([(node.id, node._affiliations[entity]) | 64 return defer.succeed([(node.nodeIdentifier, node._affiliations[entity]) |
60 for name, node in self._nodes.iteritems() | 65 for name, node in self._nodes.iteritems() |
61 if entity in node._affiliations]) | 66 if entity in node._affiliations]) |
62 | 67 |
63 def get_subscriptions(self, entity): | 68 |
69 def getSubscriptions(self, entity): | |
64 subscriptions = [] | 70 subscriptions = [] |
65 for node in self._nodes.itervalues(): | 71 for node in self._nodes.itervalues(): |
66 for subscriber, subscription in node._subscriptions.iteritems(): | 72 for subscriber, subscription in node._subscriptions.iteritems(): |
67 subscriber = jid.internJID(subscriber) | 73 subscriber = jid.internJID(subscriber) |
68 if subscriber.userhostJID() == entity.userhostJID(): | 74 if subscriber.userhostJID() == entity.userhostJID(): |
69 subscriptions.append((node.id, subscriber, | 75 subscriptions.append((node.nodeIdentifier, subscriber, |
70 subscription.state)) | 76 subscription.state)) |
71 | 77 |
72 return defer.succeed(subscriptions) | 78 return defer.succeed(subscriptions) |
73 | 79 |
74 | 80 |
81 | |
75 class Node: | 82 class Node: |
76 | 83 |
77 implements(iidavoll.INode) | 84 implements(iidavoll.INode) |
78 | 85 |
79 def __init__(self, node_id, owner, config): | 86 def __init__(self, nodeIdentifier, owner, config): |
80 self.id = node_id | 87 self.nodeIdentifier = nodeIdentifier |
81 self._affiliations = {owner.userhost(): 'owner'} | 88 self._affiliations = {owner.userhost(): 'owner'} |
82 self._subscriptions = {} | 89 self._subscriptions = {} |
83 self._config = config | 90 self._config = config |
84 | 91 |
85 def get_type(self): | 92 |
86 return self.type | 93 def getType(self): |
87 | 94 return self.nodeType |
88 def get_configuration(self): | 95 |
96 | |
97 def getConfiguration(self): | |
89 return self._config | 98 return self._config |
90 | 99 |
91 def get_meta_data(self): | 100 |
101 def getMetaData(self): | |
92 config = copy.copy(self._config) | 102 config = copy.copy(self._config) |
93 config["pubsub#node_type"] = self.type | 103 config["pubsub#node_type"] = self.nodeType |
94 return config | 104 return config |
95 | 105 |
96 def set_configuration(self, options): | 106 |
107 def setConfiguration(self, options): | |
97 for option in options: | 108 for option in options: |
98 if option in self._config: | 109 if option in self._config: |
99 self._config[option] = options[option] | 110 self._config[option] = options[option] |
100 | 111 |
101 return defer.succeed(None) | 112 return defer.succeed(None) |
102 | 113 |
103 def get_affiliation(self, entity): | 114 |
115 def getAffiliation(self, entity): | |
104 return defer.succeed(self._affiliations.get(entity.full())) | 116 return defer.succeed(self._affiliations.get(entity.full())) |
105 | 117 |
106 def get_subscription(self, subscriber): | 118 |
119 def getSubscription(self, subscriber): | |
107 try: | 120 try: |
108 subscription = self._subscriptions[subscriber.full()] | 121 subscription = self._subscriptions[subscriber.full()] |
109 except KeyError: | 122 except KeyError: |
110 state = None | 123 state = None |
111 else: | 124 else: |
112 state = subscription.state | 125 state = subscription.state |
113 return defer.succeed(state) | 126 return defer.succeed(state) |
114 | 127 |
115 def add_subscription(self, subscriber, state): | 128 |
129 def addSubscription(self, subscriber, state): | |
116 if self._subscriptions.get(subscriber.full()): | 130 if self._subscriptions.get(subscriber.full()): |
117 return defer.fail(error.SubscriptionExists()) | 131 return defer.fail(error.SubscriptionExists()) |
118 | 132 |
119 subscription = Subscription(state) | 133 subscription = Subscription(state) |
120 self._subscriptions[subscriber.full()] = subscription | 134 self._subscriptions[subscriber.full()] = subscription |
121 return defer.succeed(None) | 135 return defer.succeed(None) |
122 | 136 |
123 def remove_subscription(self, subscriber): | 137 |
138 def removeSubscription(self, subscriber): | |
124 try: | 139 try: |
125 del self._subscriptions[subscriber.full()] | 140 del self._subscriptions[subscriber.full()] |
126 except KeyError: | 141 except KeyError: |
127 return defer.fail(error.NotSubscribed()) | 142 return defer.fail(error.NotSubscribed()) |
128 | 143 |
129 return defer.succeed(None) | 144 return defer.succeed(None) |
130 | 145 |
131 def get_subscribers(self): | 146 |
147 def getSubscribers(self): | |
132 subscribers = [jid.internJID(subscriber) for subscriber, subscription | 148 subscribers = [jid.internJID(subscriber) for subscriber, subscription |
133 in self._subscriptions.iteritems() | 149 in self._subscriptions.iteritems() |
134 if subscription.state == 'subscribed'] | 150 if subscription.state == 'subscribed'] |
135 | 151 |
136 return defer.succeed(subscribers) | 152 return defer.succeed(subscribers) |
137 | 153 |
138 def is_subscribed(self, entity): | 154 |
155 def isSubscribed(self, entity): | |
139 for subscriber, subscription in self._subscriptions.iteritems(): | 156 for subscriber, subscription in self._subscriptions.iteritems(): |
140 if jid.internJID(subscriber).userhost() == entity.userhost() and \ | 157 if jid.internJID(subscriber).userhost() == entity.userhost() and \ |
141 subscription.state == 'subscribed': | 158 subscription.state == 'subscribed': |
142 return defer.succeed(True) | 159 return defer.succeed(True) |
143 | 160 |
144 return defer.succeed(False) | 161 return defer.succeed(False) |
145 | 162 |
146 def get_affiliations(self): | 163 |
164 def getAffiliations(self): | |
147 affiliations = [(jid.internJID(entity), affiliation) for entity, affiliation | 165 affiliations = [(jid.internJID(entity), affiliation) for entity, affiliation |
148 in self._affiliations.iteritems()] | 166 in self._affiliations.iteritems()] |
149 | 167 |
150 return defer.succeed(affiliations) | 168 return defer.succeed(affiliations) |
151 | 169 |
152 | 170 |
171 | |
153 class LeafNodeMixin: | 172 class LeafNodeMixin: |
154 | 173 |
155 type = 'leaf' | 174 nodeType = 'leaf' |
156 | 175 |
157 def __init__(self): | 176 def __init__(self): |
158 self._items = {} | 177 self._items = {} |
159 self._itemlist = [] | 178 self._itemlist = [] |
160 | 179 |
161 def store_items(self, items, publisher): | 180 |
181 def storeItems(self, items, publisher): | |
162 for data in items: | 182 for data in items: |
163 id = data["id"] | 183 itemIdentifier = data["id"] |
164 data = data.toXml() | 184 data = data.toXml() |
165 if isinstance(data, str): | 185 if isinstance(data, str): |
166 data = data.decode('utf-8') | 186 data = data.decode('utf-8') |
167 item = (data, publisher) | 187 item = (data, publisher) |
168 if id in self._items: | 188 if itemIdentifier in self._items: |
169 self._itemlist.remove(self._items[id]) | 189 self._itemlist.remove(self._items[itemIdentifier]) |
170 self._items[id] = item | 190 self._items[itemIdentifier] = item |
171 self._itemlist.append(item) | 191 self._itemlist.append(item) |
172 | 192 |
173 return defer.succeed(None) | 193 return defer.succeed(None) |
174 | 194 |
175 def remove_items(self, item_ids): | 195 |
196 def removeItems(self, itemIdentifiers): | |
176 deleted = [] | 197 deleted = [] |
177 | 198 |
178 for item_id in item_ids: | 199 for itemIdentifier in itemIdentifiers: |
179 try: | 200 try: |
180 item = self._items[item_id] | 201 item = self._items[itemIdentifier] |
181 except KeyError: | 202 except KeyError: |
182 pass | 203 pass |
183 else: | 204 else: |
184 self._itemlist.remove(item) | 205 self._itemlist.remove(item) |
185 del self._items[item_id] | 206 del self._items[itemIdentifier] |
186 deleted.append(item_id) | 207 deleted.append(itemIdentifier) |
187 | 208 |
188 return defer.succeed(deleted) | 209 return defer.succeed(deleted) |
189 | 210 |
190 def get_items(self, max_items=None): | 211 |
191 if max_items: | 212 def getItems(self, maxItems=None): |
192 list = self._itemlist[-max_items:] | 213 if maxItems: |
214 list = self._itemlist[-maxItems:] | |
193 else: | 215 else: |
194 list = self._itemlist | 216 list = self._itemlist |
195 return defer.succeed([item[0] for item in list]) | 217 return defer.succeed([item[0] for item in list]) |
196 | 218 |
197 def get_items_by_id(self, item_ids): | 219 |
220 def getItemsById(self, itemIdentifiers): | |
198 items = [] | 221 items = [] |
199 for item_id in item_ids: | 222 for itemIdentifier in itemIdentifiers: |
200 try: | 223 try: |
201 item = self._items[item_id] | 224 item = self._items[itemIdentifier] |
202 except KeyError: | 225 except KeyError: |
203 pass | 226 pass |
204 else: | 227 else: |
205 items.append(item[0]) | 228 items.append(item[0]) |
206 return defer.succeed(items) | 229 return defer.succeed(items) |
207 | 230 |
231 | |
208 def purge(self): | 232 def purge(self): |
209 self._items = {} | 233 self._items = {} |
210 self._itemlist = [] | 234 self._itemlist = [] |
211 | 235 |
212 return defer.succeed(None) | 236 return defer.succeed(None) |
213 | 237 |
214 | 238 |
239 | |
215 class LeafNode(Node, LeafNodeMixin): | 240 class LeafNode(Node, LeafNodeMixin): |
216 | 241 |
217 implements(iidavoll.ILeafNode) | 242 implements(iidavoll.ILeafNode) |
218 | 243 |
219 def __init__(self, node_id, owner, config): | 244 def __init__(self, nodeIdentifier, owner, config): |
220 Node.__init__(self, node_id, owner, config) | 245 Node.__init__(self, nodeIdentifier, owner, config) |
221 LeafNodeMixin.__init__(self) | 246 LeafNodeMixin.__init__(self) |
247 | |
222 | 248 |
223 | 249 |
224 class Subscription: | 250 class Subscription: |
225 | 251 |
226 def __init__(self, state): | 252 def __init__(self, state): |