annotate sat_pubsub/memory_storage.py @ 241:d2bdff8a4b45

changed version handler to SàT Pubsub
author Goffi <goffi@goffi.org>
date Sat, 26 May 2012 16:09:27 +0200
parents 64f780413b82
children d55620ceafed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
1 #!/usr/bin/python
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
2 #-*- coding: utf-8 -*-
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
3
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
4 """
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
5 Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
6 Copyright (c) 2012 Jérôme Poisson
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
7
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
8
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
9 This program is free software: you can redistribute it and/or modify
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
10 it under the terms of the GNU Affero General Public License as published by
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
11 the Free Software Foundation, either version 3 of the License, or
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
12 (at your option) any later version.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
13
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
14 This program is distributed in the hope that it will be useful,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
17 GNU Affero General Public License for more details.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
18
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
19 You should have received a copy of the GNU Affero General Public License
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
21 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
22
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
23 This program is based on Idavoll (http://idavoll.ik.nu/),
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
24 originaly written by Ralph Meijer (http://ralphm.net/blog/)
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
25 It is sublicensed under AGPL v3 (or any later version) as allowed by the original
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
26 license.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
27
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
28 --
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
29
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
30 Here is a copy of the original license:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
31
235
64f780413b82 fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents: 234
diff changeset
32 Copyright (c) 2003-2011 Ralph Meijer
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
33
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
34 Permission is hereby granted, free of charge, to any person obtaining
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
35 a copy of this software and associated documentation files (the
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
36 "Software"), to deal in the Software without restriction, including
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
37 without limitation the rights to use, copy, modify, merge, publish,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
38 distribute, sublicense, and/or sell copies of the Software, and to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
39 permit persons to whom the Software is furnished to do so, subject to
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
40 the following conditions:
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
41
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
42 The above copyright notice and this permission notice shall be
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
43 included in all copies or substantial portions of the Software.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
44
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
46 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
47 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
48 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
49 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
50 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
51 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
52
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 232
diff changeset
53 """
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 148
diff changeset
54
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 import copy
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 from zope.interface import implements
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 from twisted.internet import defer
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 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
59
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
60 from wokkel.pubsub import Subscription
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61
234
51af2ed8bd50 replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents: 233
diff changeset
62 from sat_pubsub import error, iidavoll
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 class Storage:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
66 implements(iidavoll.IStorage)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
68 defaultConfig = {
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
69 'leaf': {
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
70 "pubsub#persist_items": True,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
71 "pubsub#deliver_payloads": True,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
72 "pubsub#send_last_published_item": 'on_sub',
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
73 },
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
74 'collection': {
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
75 "pubsub#deliver_payloads": True,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
76 "pubsub#send_last_published_item": 'on_sub',
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
77 }
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
78 }
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
79
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 def __init__(self):
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
81 rootNode = CollectionNode('', jid.JID('localhost'),
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
82 copy.copy(self.defaultConfig['collection']))
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
83 self._nodes = {'': rootNode}
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
85
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
86 def getNode(self, nodeIdentifier):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 try:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
88 node = self._nodes[nodeIdentifier]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89 except KeyError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
90 return defer.fail(error.NodeNotFound())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 return defer.succeed(node)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
94
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
95 def getNodeIds(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96 return defer.succeed(self._nodes.keys())
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
98
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
99 def createNode(self, nodeIdentifier, owner, config):
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
100 if nodeIdentifier 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
101 return defer.fail(error.NodeExists())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
103 if config['pubsub#node_type'] != 'leaf':
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
104 raise error.NoCollections()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
106 node = LeafNode(nodeIdentifier, owner, config)
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
107 self._nodes[nodeIdentifier] = node
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
111
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
112 def deleteNode(self, nodeIdentifier):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113 try:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
114 del self._nodes[nodeIdentifier]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115 except KeyError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
116 return defer.fail(error.NodeNotFound())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
120
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
121 def getAffiliations(self, entity):
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
122 entity = entity.userhost()
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
123 return defer.succeed([(node.nodeIdentifier, node._affiliations[entity])
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 for name, node in self._nodes.iteritems()
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
125 if entity in node._affiliations])
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
127
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
128 def getSubscriptions(self, entity):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
129 subscriptions = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
130 for node in self._nodes.itervalues():
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 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
132 subscriber = jid.internJID(subscriber)
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
133 if subscriber.userhostJID() == entity.userhostJID():
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
134 subscriptions.append(subscription)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136 return defer.succeed(subscriptions)
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
137
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
138
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
139 def getDefaultConfiguration(self, nodeType):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
140 if nodeType == 'collection':
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
141 raise error.NoCollections()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
142
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
143 return self.defaultConfig[nodeType]
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
144
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
145
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
146 class Node:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
147
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
148 implements(iidavoll.INode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
149
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
150 def __init__(self, nodeIdentifier, owner, config):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
151 self.nodeIdentifier = nodeIdentifier
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
152 self._affiliations = {owner.userhost(): 'owner'}
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
153 self._subscriptions = {}
221
a430976f2977 Make a copy of the config to prevent later modification.
Ralph Meijer <ralphm@ik.nu>
parents: 215
diff changeset
154 self._config = copy.copy(config)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
155
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
156
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
157 def getType(self):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
158 return self.nodeType
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
159
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
160
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
161 def getConfiguration(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
162 return self._config
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
163
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
164
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
165 def getMetaData(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
166 config = copy.copy(self._config)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
167 config["pubsub#node_type"] = self.nodeType
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
168 return config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
169
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
170
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
171 def setConfiguration(self, options):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
172 for option in options:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
173 if option in self._config:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
174 self._config[option] = options[option]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
175
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
176 return defer.succeed(None)
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
177
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
178
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
179 def getAffiliation(self, entity):
226
b7018ec56ee5 Always use the bare JID for affilitations.
Ralph Meijer <ralphm@ik.nu>
parents: 223
diff changeset
180 return defer.succeed(self._affiliations.get(entity.userhost()))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
181
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
182
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
183 def getSubscription(self, subscriber):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
184 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
185 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
186 except KeyError:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
187 return defer.succeed(None)
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
188 else:
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
189 return defer.succeed(subscription)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
190
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
191
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
192 def getSubscriptions(self, state=None):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
193 return defer.succeed(
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
194 [subscription
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
195 for subscription in self._subscriptions.itervalues()
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
196 if state is None or subscription.state == state])
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
197
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
198
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
199
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
200 def addSubscription(self, subscriber, state, options):
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
201 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
202 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
203
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
204 subscription = Subscription(self.nodeIdentifier, subscriber, state,
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
205 options)
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
206 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
207 return defer.succeed(None)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
208
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
209
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
210 def removeSubscription(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
211 try:
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
212 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
213 except KeyError:
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
214 return defer.fail(error.NotSubscribed())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
215
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
216 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
217
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
218
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
219 def isSubscribed(self, entity):
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
220 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
221 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
222 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
223 return defer.succeed(True)
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
224
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
225 return defer.succeed(False)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
226
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
227
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
228 def getAffiliations(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
229 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
230 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
231
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
232 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
233
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
234
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
235
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
236 class PublishedItem(object):
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
237 """
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
238 A published item.
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
239
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
240 This represent an item as it was published by an entity.
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
241
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
242 @ivar element: The DOM representation of the item that was published.
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
243 @type element: L{Element<twisted.words.xish.domish.Element>}
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
244 @ivar publisher: The entity that published the item.
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
245 @type publisher: L{JID<twisted.words.protocols.jabber.jid.JID>}
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
246 """
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
247
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
248 def __init__(self, element, publisher):
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
249 self.element = element
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
250 self.publisher = publisher
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
251
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
252
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
253
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
254 class LeafNode(Node):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
255
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
256 implements(iidavoll.ILeafNode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
257
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
258 nodeType = 'leaf'
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
259
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
260 def __init__(self, nodeIdentifier, owner, config):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
261 Node.__init__(self, nodeIdentifier, owner, config)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
262 self._items = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
263 self._itemlist = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
264
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
265
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
266 def storeItems(self, items, publisher):
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
267 for element in items:
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
268 item = PublishedItem(element, publisher)
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
269 itemIdentifier = element["id"]
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
270 if itemIdentifier in self._items:
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
271 self._itemlist.remove(self._items[itemIdentifier])
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
272 self._items[itemIdentifier] = item
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
273 self._itemlist.append(item)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
274
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
275 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
276
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
277
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
278 def removeItems(self, itemIdentifiers):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
279 deleted = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
280
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
281 for itemIdentifier in itemIdentifiers:
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
282 try:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
283 item = self._items[itemIdentifier]
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
284 except KeyError:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
285 pass
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
286 else:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
287 self._itemlist.remove(item)
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
288 del self._items[itemIdentifier]
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
289 deleted.append(itemIdentifier)
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
290
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
291 return defer.succeed(deleted)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
292
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
293
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
294 def getItems(self, maxItems=None):
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
295 if maxItems:
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
296 itemList = self._itemlist[-maxItems:]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
297 else:
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
298 itemList = self._itemlist
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
299 return defer.succeed([item.element for item in itemList])
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
300
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
301
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
302 def getItemsById(self, itemIdentifiers):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
303 items = []
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
304 for itemIdentifier in itemIdentifiers:
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
305 try:
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
306 item = self._items[itemIdentifier]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
307 except KeyError:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
308 pass
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
309 else:
202
77c61e2b8c75 Use `domish.Element`s to represent items, instead of serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 198
diff changeset
310 items.append(item.element)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
311 return defer.succeed(items)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
312
198
e404775b12df Change naming and spacing conventions to match Twisted's.
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
313
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
314 def purge(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
315 self._items = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
316 self._itemlist = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
317
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
318 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
319
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
320
206
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
321 class CollectionNode(Node):
274a45d2a5ab Implement root collection that includes all leaf nodes.
Ralph Meijer <ralphm@ik.nu>
parents: 204
diff changeset
322 nodeType = 'collection'
204
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
323
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
324
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
325
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
326 class GatewayStorage(object):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
327 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
328 Memory based storage facility for the XMPP-HTTP gateway.
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
329 """
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
330
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
331 def __init__(self):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
332 self.callbacks = {}
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
333
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
334
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
335 def addCallback(self, service, nodeIdentifier, callback):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
336 try:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
337 callbacks = self.callbacks[service, nodeIdentifier]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
338 except KeyError:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
339 callbacks = set([callback])
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
340 self.callbacks[service, nodeIdentifier] = callbacks
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
341 else:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
342 callbacks.add(callback)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
343 pass
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
344
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
345 return defer.succeed(None)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
346
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
347
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
348 def removeCallback(self, service, nodeIdentifier, callback):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
349 try:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
350 callbacks = self.callbacks[service, nodeIdentifier]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
351 callbacks.remove(callback)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
352 except KeyError:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
353 return defer.fail(error.NotSubscribed())
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
354 else:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
355 if not callbacks:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
356 del self.callbacks[service, nodeIdentifier]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
357
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
358 return defer.succeed(not callbacks)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
359
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
360
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
361 def getCallbacks(self, service, nodeIdentifier):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
362 try:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
363 callbacks = self.callbacks[service, nodeIdentifier]
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
364 except KeyError:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
365 return defer.fail(error.NoCallbacks())
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
366 else:
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
367 return defer.succeed(callbacks)
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
368
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
369
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
370 def hasCallbacks(self, service, nodeIdentifier):
b4bf0a5ce50d Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents: 202
diff changeset
371 return defer.succeed((service, nodeIdentifier) in self.callbacks)