Mercurial > libervia-pubsub
comparison idavoll/backend.py @ 43:9685b7e291ef
Moved common stuff out of pgsql_backend.py to backend.py.
Implemented Storage class for memory backend.
Implemented item storage for pgsql Storage.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Mon, 01 Nov 2004 12:37:40 +0000 |
parents | 788114f9b5bc |
children | bc7438476a67 |
comparison
equal
deleted
inserted
replaced
42:7d088c61e131 | 43:9685b7e291ef |
---|---|
1 from twisted.python import components | 1 from twisted.python import components |
2 from twisted.application import service | 2 from twisted.application import service |
3 from twisted.xish import utility | 3 from twisted.xish import utility |
4 from twisted.internet import defer | |
4 | 5 |
5 class Error(Exception): | 6 class Error(Exception): |
6 msg = '' | 7 msg = '' |
7 | 8 |
8 def __str__(self): | 9 def __str__(self): |
138 | 139 |
139 class BackendService(service.MultiService, utility.EventDispatcher): | 140 class BackendService(service.MultiService, utility.EventDispatcher): |
140 | 141 |
141 __implements__ = IBackendService, | 142 __implements__ = IBackendService, |
142 | 143 |
143 def __init__(self): | 144 def __init__(self, storage): |
144 service.MultiService.__init__(self) | 145 service.MultiService.__init__(self) |
145 utility.EventDispatcher.__init__(self) | 146 utility.EventDispatcher.__init__(self) |
147 self.storage = storage | |
146 | 148 |
147 def get_supported_affiliations(self): | 149 def get_supported_affiliations(self): |
148 return ['none', 'owner', 'outcast', 'publisher'] | 150 return ['none', 'owner', 'outcast', 'publisher'] |
151 | |
152 def publish(self, node_id, items, requestor): | |
153 d1 = self.storage.get_node_configuration(node_id) | |
154 d2 = self.storage.get_affiliation(node_id, requestor.full()) | |
155 d = defer.DeferredList([d1, d2], fireOnOneErrback=1) | |
156 d.addErrback(lambda x: x.value[0]) | |
157 d.addCallback(self._do_publish, node_id, items, requestor) | |
158 return d | |
159 | |
160 def _do_publish(self, result, node_id, items, requestor): | |
161 print result | |
162 configuration = result[0][1] | |
163 persist_items = configuration["persist_items"] | |
164 deliver_payloads = configuration["deliver_payloads"] | |
165 affiliation = result[1][1] | |
166 | |
167 if affiliation not in ['owner', 'publisher']: | |
168 raise NotAuthorized | |
169 | |
170 if items and not persist_items and not deliver_payloads: | |
171 raise NoPayloadAllowed | |
172 elif not items and (persist_items or deliver_payloads): | |
173 raise PayloadExpected | |
174 | |
175 print "publish by %s to %s" % (requestor.full(), node_id) | |
176 | |
177 if persist_items or deliver_payloads: | |
178 for item in items: | |
179 if item["id"] is None: | |
180 item["id"] = 'random' # FIXME | |
181 | |
182 if persist_items: | |
183 d = self.store_items(node_id, items, requestor.full()) | |
184 else: | |
185 d = defer.succeed(None) | |
186 | |
187 d.addCallback(self._do_notify, node_id, items, deliver_payloads) | |
188 | |
189 def _do_notify(self, result, node_id, items, deliver_payloads): | |
190 if items and not deliver_payloads: | |
191 for item in items: | |
192 item.children = [] | |
193 | |
194 self.dispatch({ 'items': items, 'node_id': node_id }, | |
195 '//event/pubsub/notify') | |
196 | |
197 def get_notification_list(self, node_id, items): | |
198 d = self.storage.get_subscribers(node_id) | |
199 d.addCallback(self._magic_filter, node_id, items) | |
200 return d | |
201 | |
202 def _magic_filter(self, subscribers, node_id, items): | |
203 list = {} | |
204 for subscriber in subscribers: | |
205 list[subscriber] = items | |
206 | |
207 return list | |
208 | |
209 def store_items(self, node_id, items, publisher): | |
210 return self.storage.store_items(node_id, items, publisher) | |
149 | 211 |
150 class NotificationService(service.Service): | 212 class NotificationService(service.Service): |
151 | 213 |
152 def register_notifier(self, observerfn, *args, **kwargs): | 214 def register_notifier(self, observerfn, *args, **kwargs): |
153 self.parent.addObserver('//event/pubsub/notify', observerfn, | 215 self.parent.addObserver('//event/pubsub/notify', observerfn, |