annotate idavoll/memory_storage.py @ 192:a219fe70a762

Use ServiceMaker to setup twistd plugins, if available.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 30 May 2008 09:56:08 +0000
parents fc687620599b
children e404775b12df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
1 # Copyright (c) 2003-2008 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
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 import copy
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 from zope.interface import implements
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 from twisted.internet import defer
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
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
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10
115
97f10cfea28c Changed default configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 112
diff changeset
11 default_config = {"pubsub#persist_items": True,
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
12 "pubsub#deliver_payloads": True,
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 179
diff changeset
13 "pubsub#send_last_published_item": 'on_sub',
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
14 "pubsub#node_type": "leaf"}
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 class Storage:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
18 implements(iidavoll.IStorage)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 def __init__(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 self._nodes = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 def get_node(self, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 node = self._nodes[node_id]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 except KeyError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
27 return defer.fail(error.NodeNotFound())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 return defer.succeed(node)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 def get_node_ids(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 return defer.succeed(self._nodes.keys())
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
34 def create_node(self, node_id, owner, config=None):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 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
36 return defer.fail(error.NodeExists())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 if not config:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39 config = copy.copy(default_config)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
41 if config['pubsub#node_type'] != 'leaf':
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 raise NotImplementedError
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 node = LeafNode(node_id, owner, config)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 self._nodes[node_id] = node
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 def delete_node(self, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51 del self._nodes[node_id]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52 except KeyError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
53 return defer.fail(error.NodeNotFound())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57 def get_affiliations(self, entity):
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
58 entity = entity.userhost()
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
59 return defer.succeed([(node.id, node._affiliations[entity])
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 for name, node in self._nodes.iteritems()
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
61 if entity in node._affiliations])
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 def get_subscriptions(self, entity):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 subscriptions = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 for node in self._nodes.itervalues():
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 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
67 subscriber = jid.internJID(subscriber)
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
68 if subscriber.userhostJID() == entity.userhostJID():
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 subscriptions.append((node.id, subscriber,
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 subscription.state))
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 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
73
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
74
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 class Node:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
77 implements(iidavoll.INode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 def __init__(self, node_id, owner, config):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 self.id = node_id
112
72c8812fa1b5 Fix interpretation of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
81 self._affiliations = {owner.userhost(): 'owner'}
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82 self._subscriptions = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 self._config = config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 def get_type(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86 return self.type
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88 def get_configuration(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89 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
90
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91 def get_meta_data(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 config = copy.copy(self._config)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 config["pubsub#node_type"] = self.type
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 return config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96 def set_configuration(self, options):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97 for option in options:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 if option in self._config:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 self._config[option] = options[option]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 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
102
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 def get_affiliation(self, entity):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104 return defer.succeed(self._affiliations.get(entity.full()))
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
106 def get_subscription(self, subscriber):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108 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
109 except KeyError:
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
110 state = None
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
111 else:
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
112 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
113 return defer.succeed(state)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
115 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
116 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
117 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
118
120
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
119 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
120 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
121 return defer.succeed(None)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 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
124 try:
8892331314c8 Change Node.add_subscription() to return a Failure when a subscription already
Ralph Meijer <ralphm@ik.nu>
parents: 115
diff changeset
125 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
126 except KeyError:
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
127 return defer.fail(error.NotSubscribed())
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
128
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
129 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
130
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 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
132 subscribers = [jid.internJID(subscriber) for subscriber, subscription
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133 in self._subscriptions.iteritems()
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 if subscription.state == 'subscribed']
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(subscribers)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
138 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
139 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
140 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
141 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
142 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
143
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
144 return defer.succeed(False)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
145
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
146 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
147 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
148 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
149
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
150 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
151
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
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 class LeafNodeMixin:
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
154
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
155 type = 'leaf'
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
156
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
157 def __init__(self):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
158 self._items = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
159 self._itemlist = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
160
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
161 def store_items(self, items, publisher):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
162 for data in items:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
163 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
164 data = data.toXml()
8f6956b9a688 Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents: 126
diff changeset
165 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
166 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
167 item = (data, publisher)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
168 if id in self._items:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
169 self._itemlist.remove(self._items[id])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
170 self._items[id] = item
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
171 self._itemlist.append(item)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
172
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
173 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
174
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
175 def remove_items(self, item_ids):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
176 deleted = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
177
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
178 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
179 try:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
180 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
181 except KeyError:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
182 pass
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
183 else:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
184 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
185 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
186 deleted.append(item_id)
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
187
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 139
diff changeset
188 return defer.succeed(deleted)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
189
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
190 def get_items(self, max_items=None):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
191 if max_items:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
192 list = self._itemlist[-max_items:]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
193 else:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
194 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
195 return defer.succeed([item[0] for item in list])
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
196
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
197 def get_items_by_id(self, item_ids):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
198 items = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
199 for item_id in item_ids:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
200 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
201 item = self._items[item_id]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
202 except KeyError:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
203 pass
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
204 else:
139
8f6956b9a688 Follow API change in Twisted Xish, while still being compatible with
Ralph Meijer <ralphm@ik.nu>
parents: 126
diff changeset
205 items.append(item[0])
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
206 return defer.succeed(items)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
207
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
208 def purge(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
209 self._items = {}
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
210 self._itemlist = []
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
211
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
212 return defer.succeed(None)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
213
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
214
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
215 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
216
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
217 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
218
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
219 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
220 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
221 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
222
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
223
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
224 class Subscription:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
225
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
226 def __init__(self, state):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
227 self.state = state