Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0277.py @ 594:e629371a28d3
Fix pep8 support in src/plugins.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
comparison
equal
deleted
inserted
replaced
593:70bae685d05c | 594:e629371a28d3 |
---|---|
25 from twisted.words.protocols.jabber import error as jab_error | 25 from twisted.words.protocols.jabber import error as jab_error |
26 import twisted.internet.error | 26 import twisted.internet.error |
27 from twisted.words.xish import domish | 27 from twisted.words.xish import domish |
28 from sat.tools.xml_tools import ElementParser | 28 from sat.tools.xml_tools import ElementParser |
29 | 29 |
30 from wokkel import disco,pubsub | 30 from wokkel import disco, pubsub |
31 from feed.atom import Entry, Author | 31 from feed.atom import Entry, Author |
32 import uuid | 32 import uuid |
33 from time import time | 33 from time import time |
34 | 34 |
35 NS_MICROBLOG = 'urn:xmpp:microblog:0' | 35 NS_MICROBLOG = 'urn:xmpp:microblog:0' |
38 OPT_MAX_ITEMS = 'pubsub#max_items' | 38 OPT_MAX_ITEMS = 'pubsub#max_items' |
39 OPT_DELIVER_PAYLOADS = 'pubsub#deliver_payloads' | 39 OPT_DELIVER_PAYLOADS = 'pubsub#deliver_payloads' |
40 OPT_SEND_ITEM_SUBSCRIBE = 'pubsub#send_item_subscribe' | 40 OPT_SEND_ITEM_SUBSCRIBE = 'pubsub#send_item_subscribe' |
41 | 41 |
42 PLUGIN_INFO = { | 42 PLUGIN_INFO = { |
43 "name": "Microblogging over XMPP Plugin", | 43 "name": "Microblogging over XMPP Plugin", |
44 "import_name": "XEP-0277", | 44 "import_name": "XEP-0277", |
45 "type": "XEP", | 45 "type": "XEP", |
46 "protocols": [], | 46 "protocols": [], |
47 "dependencies": ["XEP-0163","XEP-0060"], | 47 "dependencies": ["XEP-0163", "XEP-0060"], |
48 "main": "XEP_0277", | 48 "main": "XEP_0277", |
49 "handler": "no", | 49 "handler": "no", |
50 "description": _("""Implementation of microblogging Protocol""") | 50 "description": _("""Implementation of microblogging Protocol""") |
51 } | 51 } |
52 | |
52 | 53 |
53 class NodeAccessChangeException(Exception): | 54 class NodeAccessChangeException(Exception): |
54 pass | 55 pass |
56 | |
55 | 57 |
56 class XEP_0277(object): | 58 class XEP_0277(object): |
57 | 59 |
58 def __init__(self, host): | 60 def __init__(self, host): |
59 info(_("Microblogging plugin initialization")) | 61 info(_("Microblogging plugin initialization")) |
60 self.host = host | 62 self.host = host |
61 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog) | 63 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog) |
62 host.bridge.addMethod("getLastMicroblogs", ".plugin", | 64 host.bridge.addMethod("getLastMicroblogs", ".plugin", |
63 in_sign='sis', out_sign='aa{ss}', | 65 in_sign='sis', out_sign='aa{ss}', |
64 method=self.getLastMicroblogs, | 66 method=self.getLastMicroblogs, |
65 async = True, | 67 async=True, |
66 doc = { 'summary':'retrieve items', | 68 doc={'summary': 'retrieve items', |
67 'param_0':'jid: publisher of wanted microblog', | 69 'param_0': 'jid: publisher of wanted microblog', |
68 'param_1':'max_items: see XEP-0060 #6.5.7', | 70 'param_1': 'max_items: see XEP-0060 #6.5.7', |
69 'param_2':'%(doc_profile)s', | 71 'param_2': '%(doc_profile)s', |
70 'return':'list of microblog data (dict)' | 72 'return': 'list of microblog data (dict)'}) |
71 }) | |
72 host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='', | 73 host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='', |
73 method=self.setMicroblogAccess, | 74 method=self.setMicroblogAccess, |
74 async = True, | 75 async=True, |
75 doc = { | 76 doc={}) |
76 }) | |
77 | 77 |
78 def item2mbdata(self, item): | 78 def item2mbdata(self, item): |
79 """Convert an XML Item to microblog data used in bridge API | 79 """Convert an XML Item to microblog data used in bridge API |
80 @param item: domish.Element of microblog item | 80 @param item: domish.Element of microblog item |
81 @return: microblog data (dictionary)""" | 81 @return: microblog data (dictionary)""" |
82 try: | 82 try: |
83 entry_elt = filter (lambda x:x.name == "entry", item.children)[0] | 83 entry_elt = filter(lambda x: x.name == "entry", item.children)[0] |
84 except KeyError: | 84 except KeyError: |
85 warning(_('No entry element in microblog item')) | 85 warning(_('No entry element in microblog item')) |
86 return | 86 return |
87 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8')) | 87 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8')) |
88 microblog_data={} | 88 microblog_data = {} |
89 try: | 89 try: |
90 microblog_data['content'] = _entry.title.text | 90 microblog_data['content'] = _entry.title.text |
91 if len(_entry.authors): | 91 if len(_entry.authors): |
92 microblog_data['author'] = _entry.authors[0].name.text | 92 microblog_data['author'] = _entry.authors[0].name.text |
93 microblog_data['timestamp'] = str(int(_entry.updated.tf)) | 93 microblog_data['timestamp'] = str(int(_entry.updated.tf)) |
98 | 98 |
99 ##XXX: workaround for Jappix behaviour | 99 ##XXX: workaround for Jappix behaviour |
100 if not 'author' in microblog_data: | 100 if not 'author' in microblog_data: |
101 from xe import NestElement | 101 from xe import NestElement |
102 try: | 102 try: |
103 author=NestElement('author') | 103 author = NestElement('author') |
104 author.import_xml(str(_entry)) | 104 author.import_xml(str(_entry)) |
105 microblog_data['author'] = author.nick.text | 105 microblog_data['author'] = author.nick.text |
106 except: | 106 except: |
107 error(_('Cannot find author')) | 107 error(_('Cannot find author')) |
108 ##end workaround Jappix | 108 ##end workaround Jappix |
121 content = data['content'] | 121 content = data['content'] |
122 _entry = Entry() | 122 _entry = Entry() |
123 #FIXME: need to escape html | 123 #FIXME: need to escape html |
124 _entry.title = unicode(content).encode('utf-8') | 124 _entry.title = unicode(content).encode('utf-8') |
125 _entry.author = Author() | 125 _entry.author = Author() |
126 _entry.author.name = data.get('author',self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') | 126 _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') |
127 _entry.updated = float(data.get('timestamp',time())) | 127 _entry.updated = float(data.get('timestamp', time())) |
128 _entry.id = str(_uuid) | 128 _entry.id = str(_uuid) |
129 _entry_elt = ElementParser()(str(_entry).decode('utf-8')) | 129 _entry_elt = ElementParser()(str(_entry).decode('utf-8')) |
130 item = pubsub.Item(payload=_entry_elt) | 130 item = pubsub.Item(payload=_entry_elt) |
131 item['id'] = _uuid | 131 item['id'] = _uuid |
132 return item | 132 return item |
133 | 133 |
134 def sendMicroblog(self, data, profile): | 134 def sendMicroblog(self, data, profile): |
135 """Send XEP-0277's microblog data | 135 """Send XEP-0277's microblog data |
136 @param data: must include content | 136 @param data: must include content |
137 @param profile: profile which send the mood""" | 137 @param profile: profile which send the mood""" |
138 if not data.has_key('content'): | 138 if 'content' not in data: |
139 error(_("Microblog data must contain at least 'content' key")) | 139 error(_("Microblog data must contain at least 'content' key")) |
140 return 3 | 140 return 3 |
141 content = data['content'] | 141 content = data['content'] |
142 if not content: | 142 if not content: |
143 error(_("Microblog data's content value must not be empty")) | 143 error(_("Microblog data's content value must not be empty")) |
144 return 3 | 144 return 3 |
145 item = self.data2entry(data, profile) | 145 item = self.data2entry(data, profile) |
146 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) | 146 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile) |
147 return 0 | 147 return 0 |
148 | 148 |
149 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): | 149 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): |
150 """Get the last published microblogs | 150 """Get the last published microblogs |
151 @param pub_jid: jid of the publisher | 151 @param pub_jid: jid of the publisher |
166 | 166 |
167 _jid, xmlstream = self.host.getJidNStream(profile_key) | 167 _jid, xmlstream = self.host.getJidNStream(profile_key) |
168 if not _jid: | 168 if not _jid: |
169 error(_("Can't find profile's jid")) | 169 error(_("Can't find profile's jid")) |
170 return | 170 return |
171 _options = {OPT_ACCESS_MODEL:access, OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1, OPT_DELIVER_PAYLOADS:1, OPT_SEND_ITEM_SUBSCRIBE: 1} | 171 _options = {OPT_ACCESS_MODEL: access, OPT_PERSIST_ITEMS: 1, OPT_MAX_ITEMS: -1, OPT_DELIVER_PAYLOADS: 1, OPT_SEND_ITEM_SUBSCRIBE: 1} |
172 | 172 |
173 def cb(result): | 173 def cb(result): |
174 #Node is created with right permission | 174 #Node is created with right permission |
175 debug(_("Microblog node has now access %s") % access) | 175 debug(_("Microblog node has now access %s") % access) |
176 | 176 |
180 raise NodeAccessChangeException() | 180 raise NodeAccessChangeException() |
181 | 181 |
182 def err_cb(s_error): | 182 def err_cb(s_error): |
183 #If the node already exists, the condition is "conflict", | 183 #If the node already exists, the condition is "conflict", |
184 #else we have an unmanaged error | 184 #else we have an unmanaged error |
185 if s_error.value.condition=='conflict': | 185 if s_error.value.condition == 'conflict': |
186 #d = self.host.plugins["XEP-0060"].deleteNode(_jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key) | 186 #d = self.host.plugins["XEP-0060"].deleteNode(_jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key) |
187 #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err)) | 187 #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err)) |
188 change_node_options().addCallback(cb).addErrback(fatal_err) | 188 change_node_options().addCallback(cb).addErrback(fatal_err) |
189 else: | 189 else: |
190 fatal_err(s_error) | 190 fatal_err(s_error) |
194 | 194 |
195 def change_node_options(): | 195 def change_node_options(): |
196 return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key) | 196 return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key) |
197 | 197 |
198 create_node().addCallback(cb).addErrback(err_cb) | 198 create_node().addCallback(cb).addErrback(err_cb) |
199 | |
200 | |
201 |