Mercurial > libervia-pubsub
annotate idavoll/backend.py @ 5:05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Implement configuration of persistence and sending payloads in the
notifications or not.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sun, 27 Jun 2004 14:09:17 +0000 |
parents | 9701df89c534 |
children | 46cd13c68ac0 |
rev | line source |
---|---|
1 | 1 from twisted.application import service |
2 from twisted.python import components, failure | |
3 from twisted.internet import defer, reactor | |
4 | |
5 class IBackendService(components.Interface): | |
6 """ Interface to a backend service of a pubsub service """ | |
7 | |
2 | 8 def do_publish(self, node, publisher, item): |
9 """ Returns a deferred that returns """ | |
10 | |
1 | 11 class BackendException(Exception): |
12 def __init__(self, msg = ''): | |
13 self.msg = msg | |
14 | |
15 def __str__(self): | |
16 return self.msg | |
17 | |
18 class NodeNotFound(BackendException): | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
19 def __init__(self, msg = 'Node not found'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
20 BackendException.__init__(self, msg) |
1 | 21 |
22 class NotAuthorized(BackendException): | |
23 pass | |
24 | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
25 class PayloadExpected(BackendException): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
26 def __init__(self, msg = 'Payload expected'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
27 BackendException.__init__(self, msg) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
28 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
29 class NoPayloadAllowed(BackendException): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
30 def __init__(self, msg = 'No payload allowed'): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
31 BackendException.__init__(self, msg) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
32 |
1 | 33 class MemoryBackendService(service.Service): |
34 | |
35 __implements__ = IBackendService, | |
36 | |
37 def __init__(self): | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
38 self.nodes = { |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
39 "ralphm/mood/ralphm@ik.nu": { |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
40 "persist_items": True, |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
41 "deliver_payloads": True, |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
42 } |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
43 } |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
44 self.subscribers = { |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
45 "ralphm/mood/ralphm@ik.nu": [ |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
46 "notify@ik.nu/mood_monitor" |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
47 ] |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
48 } |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
49 self.affiliations = { |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
50 "ralphm/mood/ralphm@ik.nu": { |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
51 "ralphm@ik.nu": "owner", |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
52 "ralphm@doe.ik.nu": "publisher" |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
53 } |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
54 } |
1 | 55 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
56 def do_publish(self, node, publisher, items): |
1 | 57 try: |
58 try: | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
59 config = self.nodes[node] |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
60 persist_items = config["persist_items"] |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
61 deliver_payloads = config["deliver_payloads"] |
1 | 62 except KeyError: |
63 raise NodeNotFound | |
64 | |
65 try: | |
66 affiliation = self.affiliations[node][publisher] | |
67 if affiliation not in ['owner', 'publisher']: | |
68 raise NotAuthorized | |
69 except KeyError: | |
70 raise NotAuthorized() | |
2 | 71 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
72 # the following is under the assumption that the publisher |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
73 # has to provide an item when the node is persistent, but |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
74 # an empty notification is to be sent. |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
75 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
76 if items and not persist_items and not deliver_payloads: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
77 raise NoPayloadAllowed |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
78 elif not items and (persist_items or deliver_payloads): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
79 raise PayloadExpected |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
80 |
1 | 81 print "publish by %s to %s" % (publisher, node) |
2 | 82 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
83 if persist_items or deliver_payloads: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
84 for item in items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
85 if item["id"] is None: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
86 item["id"] = 'random' |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
87 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
88 if persist_items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
89 self.storeItems(node, publisher, items) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
90 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
91 if items and not deliver_payloads: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
92 for item in items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
93 item.children = [] |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
94 |
2 | 95 recipients = self.get_subscribers(node) |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
96 recipients.addCallback(self.magic_filter, node, items) |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
97 recipients.addCallback(self.pubsub_service.do_notification, node) |
2 | 98 |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
99 return defer.succeed(None) |
1 | 100 except: |
101 f = failure.Failure() | |
102 return defer.fail(f) | |
103 | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
104 def magic_filter(self, subscribers, node, items): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
105 list = {} |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
106 for subscriber in subscribers: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
107 list[subscriber] = items |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
108 |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
109 return list |
2 | 110 |
1 | 111 def get_subscribers(self, node): |
112 d = defer.Deferred() | |
113 try: | |
114 result = self.subscribers[node] | |
115 except: | |
116 f = failure.Failure() | |
117 reactor.callLater(0, d.errback, f) | |
118 else: | |
119 reactor.callLater(0, d.callback, result) | |
120 | |
121 return d | |
122 | |
5
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
123 def storeItems(self, node, publisher, items): |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
124 for item in items: |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
125 print "Storing item %s" % item.toXml() |
05a5d412e1b1
Added PayloadExpected and NoPayloadAllowed exceptions.
Ralph Meijer <ralphm@ik.nu>
parents:
2
diff
changeset
|
126 |