comparison src/plugins/plugin_xep_0277.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents 1a759096ccbd
children 0b87d029f0a3
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from logging import debug, info, warning, error 22 from sat.core.log import getLogger
23 log = getLogger(__name__)
23 from twisted.words.protocols.jabber import jid 24 from twisted.words.protocols.jabber import jid
24 from twisted.internet import defer 25 from twisted.internet import defer
25 from sat.core import exceptions 26 from sat.core import exceptions
26 from sat.tools.xml_tools import ElementParser 27 from sat.tools.xml_tools import ElementParser
27 28
54 55
55 56
56 class XEP_0277(object): 57 class XEP_0277(object):
57 58
58 def __init__(self, host): 59 def __init__(self, host):
59 info(_("Microblogging plugin initialization")) 60 log.info(_("Microblogging plugin initialization"))
60 self.host = host 61 self.host = host
61 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog) 62 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)
62 host.bridge.addMethod("getLastMicroblogs", ".plugin", 63 host.bridge.addMethod("getLastMicroblogs", ".plugin",
63 in_sign='sis', out_sign='aa{ss}', 64 in_sign='sis', out_sign='aa{ss}',
64 method=self.getLastMicroblogs, 65 method=self.getLastMicroblogs,
151 try: # check for mandatory elements 152 try: # check for mandatory elements
152 microblog_data['id'] = xpath(entry_elt, 'id')[0].text 153 microblog_data['id'] = xpath(entry_elt, 'id')[0].text
153 microblog_data['updated'] = date2float(entry_elt, 'updated') 154 microblog_data['updated'] = date2float(entry_elt, 'updated')
154 assert('title' in microblog_data) # has been processed already 155 assert('title' in microblog_data) # has been processed already
155 except IndexError: 156 except IndexError:
156 error(_("Atom entry %s misses a required element") % item_elt.get('id', '')) 157 log.error(_("Atom entry %s misses a required element") % item_elt.get('id', ''))
157 raise exceptions.DataError 158 raise exceptions.DataError
158 159
159 if 'content' not in microblog_data: # use the atom title data as the microblog body content 160 if 'content' not in microblog_data: # use the atom title data as the microblog body content
160 microblog_data['content'] = microblog_data['title'] 161 microblog_data['content'] = microblog_data['title']
161 del microblog_data['title'] 162 del microblog_data['title']
177 microblog_data['comments'] = link_elt.attrib['href'] 178 microblog_data['comments'] = link_elt.attrib['href']
178 service, node = self.parseCommentUrl(microblog_data["comments"]) 179 service, node = self.parseCommentUrl(microblog_data["comments"])
179 microblog_data['comments_service'] = service.full() 180 microblog_data['comments_service'] = service.full()
180 microblog_data['comments_node'] = node 181 microblog_data['comments_node'] = node
181 except (exceptions.DataError, RuntimeError, KeyError): 182 except (exceptions.DataError, RuntimeError, KeyError):
182 warning(_("Can't parse the link element of pubsub entry %s") % microblog_data['id']) 183 log.warning(_("Can't parse the link element of pubsub entry %s") % microblog_data['id'])
183 except: 184 except:
184 pass 185 pass
185 try: 186 try:
186 microblog_data['author'] = xpath(entry_elt, 'author/name')[0].text 187 microblog_data['author'] = xpath(entry_elt, 'author/name')[0].text
187 except IndexError: 188 except IndexError:
188 try: # XXX: workaround for Jappix behaviour 189 try: # XXX: workaround for Jappix behaviour
189 microblog_data['author'] = xpath(entry_elt, 'author/nick')[0].text 190 microblog_data['author'] = xpath(entry_elt, 'author/nick')[0].text
190 except IndexError: 191 except IndexError:
191 warning(_("Can't find author element in pubsub entry %s") % microblog_data['id']) 192 log.warning(_("Can't find author element in pubsub entry %s") % microblog_data['id'])
192 193
193 defer.returnValue(microblog_data) 194 defer.returnValue(microblog_data)
194 195
195 def __getLXMLInnerContent(self, elt): 196 def __getLXMLInnerContent(self, elt):
196 """Return the inner content of a lxml.etree.Element. It is not 197 """Return the inner content of a lxml.etree.Element. It is not
298 def sendMicroblog(self, data, profile): 299 def sendMicroblog(self, data, profile):
299 """Send XEP-0277's microblog data 300 """Send XEP-0277's microblog data
300 @param data: must include content 301 @param data: must include content
301 @param profile: profile which send the mood""" 302 @param profile: profile which send the mood"""
302 if 'content' not in data: 303 if 'content' not in data:
303 error(_("Microblog data must contain at least 'content' key")) 304 log.error(_("Microblog data must contain at least 'content' key"))
304 raise exceptions.DataError('no "content" key found') 305 raise exceptions.DataError('no "content" key found')
305 content = data['content'] 306 content = data['content']
306 if not content: 307 if not content:
307 error(_("Microblog data's content value must not be empty")) 308 log.error(_("Microblog data's content value must not be empty"))
308 raise exceptions.DataError('empty content') 309 raise exceptions.DataError('empty content')
309 item = yield self.data2entry(data, profile) 310 item = yield self.data2entry(data, profile)
310 ret = yield self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile) 311 ret = yield self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile)
311 defer.returnValue(ret) 312 defer.returnValue(ret)
312 313
320 ret = [] 321 ret = []
321 for (success, value) in result: 322 for (success, value) in result:
322 if success: 323 if success:
323 ret.append(value) 324 ret.append(value)
324 else: 325 else:
325 error('Error while getting last microblog') 326 log.error('Error while getting last microblog')
326 return ret 327 return ret
327 328
328 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) 329 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
329 d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items))) 330 d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items)))
330 d.addCallback(resultToArray) 331 d.addCallback(resultToArray)
336 @param access: Node access model, according to xep-0060 #4.5 337 @param access: Node access model, according to xep-0060 #4.5
337 @param profile_key: profile key""" 338 @param profile_key: profile key"""
338 339
339 _jid, xmlstream = self.host.getJidNStream(profile_key) 340 _jid, xmlstream = self.host.getJidNStream(profile_key)
340 if not _jid: 341 if not _jid:
341 error(_("Can't find profile's jid")) 342 log.error(_("Can't find profile's jid"))
342 return 343 return
343 C = self.host.plugins["XEP-0060"] 344 C = self.host.plugins["XEP-0060"]
344 _options = {C.OPT_ACCESS_MODEL: access, C.OPT_PERSIST_ITEMS: 1, C.OPT_MAX_ITEMS: -1, C.OPT_DELIVER_PAYLOADS: 1, C.OPT_SEND_ITEM_SUBSCRIBE: 1} 345 _options = {C.OPT_ACCESS_MODEL: access, C.OPT_PERSIST_ITEMS: 1, C.OPT_MAX_ITEMS: -1, C.OPT_DELIVER_PAYLOADS: 1, C.OPT_SEND_ITEM_SUBSCRIBE: 1}
345 346
346 def cb(result): 347 def cb(result):
347 #Node is created with right permission 348 #Node is created with right permission
348 debug(_("Microblog node has now access %s") % access) 349 log.debug(_("Microblog node has now access %s") % access)
349 350
350 def fatal_err(s_error): 351 def fatal_err(s_error):
351 #Something went wrong 352 #Something went wrong
352 error(_("Can't set microblog access")) 353 log.error(_("Can't set microblog access"))
353 raise NodeAccessChangeException() 354 raise NodeAccessChangeException()
354 355
355 def err_cb(s_error): 356 def err_cb(s_error):
356 #If the node already exists, the condition is "conflict", 357 #If the node already exists, the condition is "conflict",
357 #else we have an unmanaged error 358 #else we have an unmanaged error