annotate idavoll/memory_backend.py @ 27:e6d62c93cd0a

Initial revision
author Ralph Meijer <ralphm@ik.nu>
date Sun, 17 Oct 2004 13:54:12 +0000
parents
children ff7c73b253bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.application import service
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 from twisted.internet import defer
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.protocols.jabber import jid
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 import backend
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 class Subscription:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 def __init__(self, state):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 self.state = state
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
9
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 class NodeConfiguration:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 def __init__(self):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 self.persist_items = False
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 self.deliver_payloads = False
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 class Node:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 def __init__(self, id):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17 self.id = id
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 self.configuration = NodeConfiguration()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 self.subscriptions = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 self.affiliations = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 self.items = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 class MemoryBackendService(service.Service):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 __implements__ = backend.IService,
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 def __init__(self):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 self.nodes = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 node = Node("ralphm/mood/ralphm@ik.nu")
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 node.subscriptions["ralphm@doe.ik.nu"] = Subscription("subscribed")
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 node.subscriptions["notify@ik.nu/mood_monitor"] = Subscription("subscribed")
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33 node.affiliations["ralphm@ik.nu"] = "owner"
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 node.affiliations["ralphm@doe.ik.nu"] = "publisher"
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 node.configuration.persist_items = True
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36 node.configuration.deliver_payloads = True
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 self.nodes[node.id] = node
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39 def do_publish(self, node_id, publisher, items):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 try:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41 node = self.nodes[node_id]
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 persist_items = node.configuration.persist_items
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 deliver_payloads = node.configuration.deliver_payloads
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 except KeyError:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 raise backend.NodeNotFound
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 try:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 if node.affiliations[publisher] not in ['owner', 'publisher']:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 raise backend.NotAuthorized
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 except KeyError:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51 raise backend.NotAuthorized
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
53 if items and not persist_items and not deliver_payloads:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54 raise backend.NoPayloadAllowed
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 elif not items and (persist_items or deliver_payloads):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 raise backend.PayloadExpected
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 print "publish by %s to %s" % (publisher, node_id)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 if persist_items or deliver_payloads:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61 for item in items:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 if item["id"] is None:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 item["id"] = 'random' # FIXME
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 if persist_items:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66 self.storeItems(node_id, publisher, items)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68 if items and not deliver_payloads:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 for item in items:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 item.children = []
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 recipients = self.get_subscribers(node_id)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73 recipients.addCallback(self.magic_filter, node_id, items)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 recipients.addCallback(self.pubsub_service.do_notification, node_id)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 return defer.succeed(None)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78 def do_subscribe(self, node_id, subscriber, requestor):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 # expect subscriber and requestor to be a jid.JID
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 try:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81 node = self.nodes[node_id]
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82 except KeyError:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 raise backend.NodeNotFound
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
85 affiliation = node.affiliations.get(requestor.full(), 'none')
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 if affiliation == 'banned':
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
88 raise backend.NotAuthorized
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
90 print subscriber.full()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91 print subscriber.userhostJID().full()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 print requestor.full()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 if subscriber.userhostJID() != requestor:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95 raise backend.NotAuthorized
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
97 try:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 subscription = node.subscriptions[subscriber.full()]
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 except KeyError:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100 subscription = Subscription('subscribed')
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 node.subscriptions[subscriber.full()] = subscription
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 print node.subscriptions
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105 return defer.succeed({
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 'affiliation': affiliation,
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 'node': node_id,
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
108 'jid': subscriber,
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 'subscription': subscription.state})
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
111 def magic_filter(self, subscribers, node_id, items):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112 list = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
113 for subscriber in subscribers:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
114 list[subscriber] = items
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
115
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 return list
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
118 def get_subscribers(self, node_id):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 d = defer.Deferred()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120 try:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
121 return defer.succeed(self.nodes[node_id].subscriptions.keys())
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122 except:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 return defer.fail()
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125 def storeItems(self, node_id, publisher, items):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126 for item in items:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
127 self.nodes[node_id].items[item["id"]] = item
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
128
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
129 print self.nodes[node_id].items
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
130
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
131 def create_node(self, node_id, owner):
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
132 result = {}
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 if not node_id:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135 raise backend.NoInstantNodes
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137 if node_id in self.nodes:
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
138 raise backend.NodeExists
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
139
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
140 node = Node(node_id)
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
141 node.affiliations[owner.full()] = 'owner'
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
142 self.nodes[node_id] = node
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
143
e6d62c93cd0a Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
144 return defer.succeed({'node_id': node.id})