Mercurial > libervia-pubsub
annotate idavoll/memory_storage.py @ 169:96afb248df5e
Fix typos in service creation. Make disco not respond when a resource is provided.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Thu, 04 Oct 2007 08:03:51 +0000 |
parents | ef22e4150caa |
children | bc269696ef42 |
rev | line source |
---|---|
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
1 # Copyright (c) 2003-2007 Ralph Meijer |
155
5191ba7c4df8
Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents:
148
diff
changeset
|
2 # See LICENSE for details. |
5191ba7c4df8
Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents:
148
diff
changeset
|
3 |
107 | 4 import copy |
5 from zope.interface import implements | |
6 from twisted.internet import defer | |
7 from twisted.words.protocols.jabber import jid | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
8 |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
9 from idavoll import error, iidavoll |
107 | 10 |
115 | 11 default_config = {"pubsub#persist_items": True, |
12 "pubsub#deliver_payloads": True} | |
107 | 13 |
14 class Storage: | |
15 | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
16 implements(iidavoll.IStorage) |
107 | 17 |
18 def __init__(self): | |
19 self._nodes = {} | |
20 | |
21 def get_node(self, node_id): | |
22 try: | |
23 node = self._nodes[node_id] | |
24 except KeyError: | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
25 return defer.fail(error.NodeNotFound()) |
107 | 26 |
27 return defer.succeed(node) | |
28 | |
29 def get_node_ids(self): | |
30 return defer.succeed(self._nodes.keys()) | |
31 | |
32 def create_node(self, node_id, owner, config = None, type='leaf'): | |
33 if node_id in self._nodes: | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
34 return defer.fail(error.NodeExists()) |
107 | 35 |
36 if not config: | |
37 config = copy.copy(default_config) | |
38 | |
39 if type != 'leaf': | |
40 raise NotImplementedError | |
41 | |
42 node = LeafNode(node_id, owner, config) | |
43 self._nodes[node_id] = node | |
44 | |
45 return defer.succeed(None) | |
46 | |
47 def delete_node(self, node_id): | |
48 try: | |
49 del self._nodes[node_id] | |
50 except KeyError: | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
51 return defer.fail(error.NodeNotFound()) |
107 | 52 |
53 return defer.succeed(None) | |
54 | |
55 def get_affiliations(self, entity): | |
112 | 56 entity = entity.userhost() |
57 return defer.succeed([(node.id, node._affiliations[entity]) | |
107 | 58 for name, node in self._nodes.iteritems() |
112 | 59 if entity in node._affiliations]) |
107 | 60 |
61 def get_subscriptions(self, entity): | |
62 subscriptions = [] | |
63 for node in self._nodes.itervalues(): | |
64 for subscriber, subscription in node._subscriptions.iteritems(): | |
148
b03e5ad81173
Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents:
146
diff
changeset
|
65 subscriber = jid.internJID(subscriber) |
112 | 66 if subscriber.userhostJID() == entity.userhostJID(): |
107 | 67 subscriptions.append((node.id, subscriber, |
68 subscription.state)) | |
69 | |
70 return defer.succeed(subscriptions) | |
71 | |
72 class Node: | |
73 | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
74 implements(iidavoll.INode) |
107 | 75 |
76 def __init__(self, node_id, owner, config): | |
77 self.id = node_id | |
112 | 78 self._affiliations = {owner.userhost(): 'owner'} |
107 | 79 self._subscriptions = {} |
80 self._config = config | |
81 | |
82 def get_type(self): | |
83 return self.type | |
84 | |
85 def get_configuration(self): | |
86 return self._config | |
87 | |
88 def get_meta_data(self): | |
89 config = copy.copy(self._config) | |
90 config["pubsub#node_type"] = self.type | |
91 return config | |
92 | |
93 def set_configuration(self, options): | |
94 for option in options: | |
95 if option in self._config: | |
96 self._config[option] = options[option] | |
97 | |
98 return defer.succeed(None) | |
99 | |
100 def get_affiliation(self, entity): | |
101 return defer.succeed(self._affiliations.get(entity.full())) | |
102 | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
103 def get_subscription(self, subscriber): |
107 | 104 try: |
105 subscription = self._subscriptions[subscriber.full()] | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
106 except KeyError: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
107 state = None |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
108 else: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
109 state = subscription.state |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
110 return defer.succeed(state) |
107 | 111 |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
112 def add_subscription(self, subscriber, state): |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
113 if self._subscriptions.get(subscriber.full()): |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
114 return defer.fail(error.SubscriptionExists()) |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
115 |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
116 subscription = Subscription(state) |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
117 self._subscriptions[subscriber.full()] = subscription |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
118 return defer.succeed(None) |
107 | 119 |
120 def remove_subscription(self, subscriber): | |
120
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
121 try: |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
122 del self._subscriptions[subscriber.full()] |
8892331314c8
Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents:
115
diff
changeset
|
123 except KeyError: |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
124 return defer.fail(error.SubscriptionNotFound()) |
107 | 125 |
126 return defer.succeed(None) | |
127 | |
128 def get_subscribers(self): | |
148
b03e5ad81173
Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents:
146
diff
changeset
|
129 subscribers = [jid.internJID(subscriber) for subscriber, subscription |
107 | 130 in self._subscriptions.iteritems() |
131 if subscription.state == 'subscribed'] | |
132 | |
133 return defer.succeed(subscribers) | |
134 | |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
135 def is_subscribed(self, entity): |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
136 for subscriber, subscription in self._subscriptions.iteritems(): |
148
b03e5ad81173
Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents:
146
diff
changeset
|
137 if jid.internJID(subscriber).userhost() == entity.userhost() and \ |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
138 subscription.state == 'subscribed': |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
139 return defer.succeed(True) |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
140 |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
141 return defer.succeed(False) |
107 | 142 |
145
f393bccec4bc
Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents:
142
diff
changeset
|
143 def get_affiliations(self): |
148
b03e5ad81173
Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents:
146
diff
changeset
|
144 affiliations = [(jid.internJID(entity), affiliation) for entity, affiliation |
145
f393bccec4bc
Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents:
142
diff
changeset
|
145 in self._affiliations.iteritems()] |
f393bccec4bc
Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents:
142
diff
changeset
|
146 |
f393bccec4bc
Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents:
142
diff
changeset
|
147 return defer.succeed(affiliations) |
f393bccec4bc
Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents:
142
diff
changeset
|
148 |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
149 class LeafNodeMixin: |
107 | 150 |
151 type = 'leaf' | |
152 | |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
153 def __init__(self): |
107 | 154 self._items = {} |
155 self._itemlist = [] | |
156 | |
157 def store_items(self, items, publisher): | |
158 for data in items: | |
159 id = data["id"] | |
139
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
160 data = data.toXml() |
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
161 if isinstance(data, str): |
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
162 data = data.decode('utf-8') |
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
163 item = (data, publisher) |
107 | 164 if id in self._items: |
165 self._itemlist.remove(self._items[id]) | |
166 self._items[id] = item | |
167 self._itemlist.append(item) | |
168 | |
169 return defer.succeed(None) | |
170 | |
171 def remove_items(self, item_ids): | |
172 deleted = [] | |
173 | |
174 for item_id in item_ids: | |
142
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
175 try: |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
176 item = self._items[item_id] |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
177 except KeyError: |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
178 pass |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
179 else: |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
180 self._itemlist.remove(item) |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
181 del self._items[item_id] |
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
182 deleted.append(item_id) |
107 | 183 |
142
812300cdbc22
Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents:
139
diff
changeset
|
184 return defer.succeed(deleted) |
107 | 185 |
186 def get_items(self, max_items=None): | |
187 if max_items: | |
188 list = self._itemlist[-max_items:] | |
189 else: | |
190 list = self._itemlist | |
139
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
191 return defer.succeed([item[0] for item in list]) |
107 | 192 |
193 def get_items_by_id(self, item_ids): | |
194 items = [] | |
195 for item_id in item_ids: | |
196 try: | |
197 item = self._items[item_id] | |
198 except KeyError: | |
199 pass | |
200 else: | |
139
8f6956b9a688
Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents:
126
diff
changeset
|
201 items.append(item[0]) |
107 | 202 return defer.succeed(items) |
203 | |
204 def purge(self): | |
205 self._items = {} | |
206 self._itemlist = [] | |
207 | |
208 return defer.succeed(None) | |
209 | |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
210 class LeafNode(Node, LeafNodeMixin): |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
211 |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
212 implements(iidavoll.ILeafNode) |
146
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
213 |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
214 def __init__(self, node_id, owner, config): |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
215 Node.__init__(self, node_id, owner, config) |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
216 LeafNodeMixin.__init__(self) |
b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents:
145
diff
changeset
|
217 |
107 | 218 class Subscription: |
219 | |
220 def __init__(self, state): | |
221 self.state = state |