comparison src/plugins/plugin_xep_0163.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
24 from twisted.words.protocols.jabber import client, jid 24 from twisted.words.protocols.jabber import client, jid
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 28
29 from wokkel import disco,pubsub 29 from wokkel import disco, pubsub
30 from wokkel.formats import Mood 30 from wokkel.formats import Mood
31 31
32 NS_USER_MOOD = 'http://jabber.org/protocol/mood' 32 NS_USER_MOOD = 'http://jabber.org/protocol/mood'
33 33
34 PLUGIN_INFO = { 34 PLUGIN_INFO = {
35 "name": "Personal Eventing Protocol Plugin", 35 "name": "Personal Eventing Protocol Plugin",
36 "import_name": "XEP-0163", 36 "import_name": "XEP-0163",
37 "type": "XEP", 37 "type": "XEP",
38 "protocols": ["XEP-0163", "XEP-0107"], 38 "protocols": ["XEP-0163", "XEP-0107"],
39 "dependencies": ["XEP-0060"], 39 "dependencies": ["XEP-0060"],
40 "main": "XEP_0163", 40 "main": "XEP_0163",
41 "handler": "no", 41 "handler": "no",
42 "description": _("""Implementation of Personal Eventing Protocol""") 42 "description": _("""Implementation of Personal Eventing Protocol""")
43 } 43 }
44
44 45
45 class XEP_0163(object): 46 class XEP_0163(object):
46 47
47 def __init__(self, host): 48 def __init__(self, host):
48 info(_("PEP plugin initialization")) 49 info(_("PEP plugin initialization"))
49 self.host = host 50 self.host = host
50 self.pep_events=set() 51 self.pep_events = set()
51 self.pep_out_cb={} 52 self.pep_out_cb = {}
52 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger) 53 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
53 host.bridge.addSignal("personalEvent", ".plugin", signature='ssa{ss}s') #args: from (jid), type(MOOD, TUNE, etc), data, profile 54 host.bridge.addSignal("personalEvent", ".plugin", signature='ssa{ss}s') # args: from (jid), type(MOOD, TUNE, etc), data, profile
54 host.bridge.addMethod("sendPersonalEvent", ".plugin", in_sign='sa{ss}s', out_sign='i', method=self.sendPersonalEvent) #args: type(MOOD, TUNE, etc), data, profile_key; return 0 or error_code 55 host.bridge.addMethod("sendPersonalEvent", ".plugin", in_sign='sa{ss}s', out_sign='i', method=self.sendPersonalEvent) # args: type(MOOD, TUNE, etc), data, profile_key; return 0 or error_code
55 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood) 56 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
56 57
57 def disoInfoTrigger(self, disco_info, profile): 58 def disoInfoTrigger(self, disco_info, profile):
58 """Add info from managed PEP 59 """Add info from managed PEP
59 @param disco_info: list of disco feature as returned by PubSub, 60 @param disco_info: list of disco feature as returned by PubSub,
60 will be filled with PEP features 61 will be filled with PEP features
61 @param profile: profile we are handling""" 62 @param profile: profile we are handling"""
62 disco_info.extend(map(disco.DiscoFeature, self.pep_events)) 63 disco_info.extend(map(disco.DiscoFeature, self.pep_events))
63 return True 64 return True
64 65
65 def addPEPEvent(self, event_type, name, in_callback, out_callback = None, notify = True): 66 def addPEPEvent(self, event_type, name, in_callback, out_callback=None, notify=True):
66 """Add a Personal Eventing Protocol event manager 67 """Add a Personal Eventing Protocol event manager
67 @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc 68 @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc
68 @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood) 69 @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
69 @param in_callback: method to call when this event occur 70 @param in_callback: method to call when this event occur
70 @param out_callback: method to call when we want to publish this event 71 @param out_callback: method to call when we want to publish this event
71 @param notify: add autosubscribe (+notify) if True""" 72 @param notify: add autosubscribe (+notify) if True"""
72 if out_callback: 73 if out_callback:
73 self.pep_out_cb[event_type] = out_callback 74 self.pep_out_cb[event_type] = out_callback
74 self.pep_events.add(name) 75 self.pep_events.add(name)
75 if notify: 76 if notify:
76 self.pep_events.add(name+"+notify") 77 self.pep_events.add(name + "+notify")
77 self.host.plugins["XEP-0060"].addManagedNode(name, in_callback) 78 self.host.plugins["XEP-0060"].addManagedNode(name, in_callback)
78 79
79 def sendPEPEvent(self, namespace, data, profile): 80 def sendPEPEvent(self, namespace, data, profile):
80 """Publish the event data 81 """Publish the event data
81 @param namespace: node namespace 82 @param namespace: node namespace
82 @param data: domish.Element to use as payload 83 @param data: domish.Element to use as payload
83 @param profile: profile which send the data""" 84 @param profile: profile which send the data"""
84 85
85 item = pubsub.Item(payload=data) 86 item = pubsub.Item(payload=data)
86 self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key = profile) 87 self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key=profile)
87 88
88 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'): 89 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
89 """Send personal event after checking the data is alright 90 """Send personal event after checking the data is alright
90 @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys() 91 @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
91 @param data: dict of {string:string} of event_type dependant data 92 @param data: dict of {string:string} of event_type dependant data
94 profile = self.host.memory.getProfileName(profile_key) 95 profile = self.host.memory.getProfileName(profile_key)
95 if not profile: 96 if not profile:
96 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key) 97 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key)
97 return 1 98 return 1
98 if not event_type in self.pep_out_cb.keys(): 99 if not event_type in self.pep_out_cb.keys():
99 error (_('Trying to send personal event for an unknown type')) 100 error(_('Trying to send personal event for an unknown type'))
100 return 2 101 return 2
101 return self.pep_out_cb[event_type](data, profile) 102 return self.pep_out_cb[event_type](data, profile)
102 103
103 def userMoodCB(self, itemsEvent, profile): 104 def userMoodCB(self, itemsEvent, profile):
104 if not itemsEvent.items: 105 if not itemsEvent.items:
105 debug(_("No item found")) 106 debug(_("No item found"))
106 return 107 return
107 try: 108 try:
108 mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0] 109 mood_elem = filter(lambda x: x.name == "mood", itemsEvent.items[0].children)[0]
109 except KeyError: 110 except KeyError:
110 error(_("Can't find mood element in mood event")) 111 error(_("Can't find mood element in mood event"))
111 return 112 return
112 _mood = Mood.fromXml(mood_elem) 113 _mood = Mood.fromXml(mood_elem)
113 if not _mood: 114 if not _mood:
114 debug(_("No mood found")) 115 debug(_("No mood found"))
115 return 116 return
116 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MOOD", {"mood":_mood.value or "", "text":_mood.text or ""}, profile) 117 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MOOD", {"mood": _mood.value or "", "text": _mood.text or ""}, profile)
117 118
118 def sendMood(self, data, profile): 119 def sendMood(self, data, profile):
119 """Send XEP-0107's User Mood 120 """Send XEP-0107's User Mood
120 @param data: must include mood and text 121 @param data: must include mood and text
121 @param profile: profile which send the mood""" 122 @param profile: profile which send the mood"""
122 try: 123 try:
123 value = data['mood'].lower() 124 value = data['mood'].lower()
124 text = data['text'] if data.has_key('text') else '' 125 text = data['text'] if 'text' in data else ''
125 except KeyError: 126 except KeyError:
126 error(_("Mood data must contain at least 'mood' key")) 127 error(_("Mood data must contain at least 'mood' key"))
127 return 3 128 return 3
128 _mood = UserMood(value, text) 129 _mood = UserMood(value, text)
129 self.sendPEPEvent(NS_USER_MOOD, _mood, profile) 130 self.sendPEPEvent(NS_USER_MOOD, _mood, profile)
130 return 0 131 return 0
132
131 133
132 class UserMood(Mood, domish.Element): 134 class UserMood(Mood, domish.Element):
133 """Improved wokkel Mood which is also a domish.Element""" 135 """Improved wokkel Mood which is also a domish.Element"""
134 136
135 def __init__(self, value, text=None): 137 def __init__(self, value, text=None):
136 Mood.__init__(self, value, text) 138 Mood.__init__(self, value, text)
137 domish.Element.__init__(self, (NS_USER_MOOD, 'mood')) 139 domish.Element.__init__(self, (NS_USER_MOOD, 'mood'))
138 self.addElement(value) 140 self.addElement(value)
139 if text: 141 if text:
140 self.addElement('text', content=text) 142 self.addElement('text', content=text)
141