annotate idavoll/memory_storage.py @ 157:21fcfb86433f

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