Fix pep8 support in src/plugins.

This commit is contained in:
Emmanuel Gil Peyrot 2013-01-18 17:55:35 +01:00
commit 0dca274360

View file

@ -43,14 +43,14 @@ RADIOC_TAG = 'radiocol'
RADIOC_REQUEST = MESSAGE + '/' + RADIOC_TAG + '[@xmlns="' + NC_RADIOCOL + '"]'
PLUGIN_INFO = {
"name": "Radio collective plugin",
"import_name": "Radiocol",
"type": "Exp",
"protocols": [],
"dependencies": ["XEP-0045", "XEP-0249"],
"main": "Radiocol",
"handler": "yes",
"description": _("""Implementation of radio collective""")
"name": "Radio collective plugin",
"import_name": "Radiocol",
"type": "Exp",
"protocols": [],
"dependencies": ["XEP-0045", "XEP-0249"],
"main": "Radiocol",
"handler": "yes",
"description": _("""Implementation of radio collective""")
}
QUEUE_LIMIT = 2
@ -61,21 +61,21 @@ class Radiocol(object):
def __init__(self, host):
info(_("Radio collective initialization"))
self.host = host
self.radios={}
self.radios = {}
host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='ass', out_sign='', method=self.radiocolLaunch)
host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='ss', out_sign='', method=self.radiocolCreate)
host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') #room_jid, referee, profile
host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') #room_jid, reason, profile
host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') #room_jid, filename, title, artist, album, profile
host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') #room_jid, filename, profile
host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss') #room_jid, profile
host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') #room_jid, profile
host.bridge.addSignal("radiocolStarted", ".plugin", signature='sss') # room_jid, referee, profile
host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss') # room_jid, reason, profile
host.bridge.addSignal("radiocolPreload", ".plugin", signature='ssssss') # room_jid, filename, title, artist, album, profile
host.bridge.addSignal("radiocolPlay", ".plugin", signature='sss') # room_jid, filename, profile
host.bridge.addSignal("radiocolNoUpload", ".plugin", signature='ss') # room_jid, profile
host.bridge.addSignal("radiocolUploadOk", ".plugin", signature='ss') # room_jid, profile
host.trigger.add("MUC user joined", self.userJoinedTrigger)
def createRadiocolElt(self, to_jid, type="normal"):
type = "normal" if to_jid.resource else "groupchat"
elt = domish.Element((None,'message'))
elt = domish.Element((None, 'message'))
elt["to"] = to_jid.full()
elt["type"] = type
elt.addElement((NC_RADIOCOL, RADIOC_TAG))
@ -83,26 +83,25 @@ class Radiocol(object):
def __create_started_elt(self):
"""Create a game_started domish element"""
started_elt = domish.Element((None,'started'))
started_elt = domish.Element((None, 'started'))
return started_elt
def __create_preload_elt(self, sender, filename, title, artist, album):
preload_elt = domish.Element((None,'preload'))
preload_elt = domish.Element((None, 'preload'))
preload_elt['sender'] = sender
preload_elt['filename'] = filename #XXX: the frontend should know the temporary directory where file is put
preload_elt['filename'] = filename # XXX: the frontend should know the temporary directory where file is put
preload_elt['title'] = title
preload_elt['artist'] = artist
preload_elt['album'] = album
return preload_elt
def userJoinedTrigger(self, room, user, profile):
"""This trigger is used to check if we are waiting people in this room,
and to create a game if everybody is here"""
room_jid = room.occupantJID.userhost()
if self.radios.has_key(room_jid) and self.radios[room_jid]["referee"] == room.occupantJID.full():
if room_jid in self.radios and self.radios[room_jid]["referee"] == room.occupantJID.full():
#we are in a radiocol room, let's start the party !
mess = self.createRadiocolElt(jid.JID(room_jid+'/'+user.nick))
mess = self.createRadiocolElt(jid.JID(room_jid + '/' + user.nick))
mess.firstChildElement().addChild(self.__create_started_elt())
self.host.profiles[profile].xmlstream.send(mess)
return True
@ -121,7 +120,7 @@ class Radiocol(object):
_room_jid = room.occupantJID.userhostJID()
self.radiocolCreate(_room_jid.userhost(), profile_key=profile)
for occupant in occupants:
self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game":"Radiocol"}, profile)
self.host.plugins["XEP-0249"].invite(jid.JID(occupant), room.occupantJID.userhostJID(), {"game": "Radiocol"}, profile)
def after_init(ignore):
room_name = "sat_radiocol_%s" % self.host.plugins["XEP-0045"].getUniqueName(profile_key)
@ -153,21 +152,21 @@ class Radiocol(object):
"""Create a new game
@param room_jid_param: jid of the room
@param profile_key: %(doc_profile_key)s"""
debug (_("Creating Radiocol"))
debug(_("Creating Radiocol"))
room_jid = jid.JID(room_jid_param)
profile = self.host.memory.getProfileName(profile_key)
if not profile:
error (_("profile %s is unknown") % profile_key)
error(_("profile %s is unknown") % profile_key)
return
if self.radios.has_key(room_jid):
warning (_("Radiocol already started in room %s") % room_jid.userhost())
if room_jid in self.radios:
warning(_("Radiocol already started in room %s") % room_jid.userhost())
else:
room_nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid.userhost(), profile)
if not room_nick:
error ('Internal error')
error('Internal error')
return
referee = room_jid.userhost() + '/' + room_nick
self.radios[room_jid.userhost()] = {'referee':referee, 'queue':[], 'upload':True, 'playing': False, 'occupants_data':{}, 'to_delete':{}}
self.radios[room_jid.userhost()] = {'referee': referee, 'queue': [], 'upload': True, 'playing': False, 'occupants_data': {}, 'to_delete': {}}
mess = self.createRadiocolElt(jid.JID(room_jid.userhost()))
mess.firstChildElement().addChild(self.__create_started_elt())
self.host.profiles[profile].xmlstream.send(mess)
@ -189,8 +188,8 @@ class Radiocol(object):
song = OggVorbis(song_path)
except OggVorbisHeaderError:
#this file is not ogg vorbis, we reject it
unlink(song_path) # FIXME: same host trick (see note above)
self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(), \
unlink(song_path) # FIXME: same host trick (see note above)
self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(),
"Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
"""mess = self.createRadiocolElt(jid.JID(referee))
reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
@ -204,7 +203,7 @@ class Radiocol(object):
album = song.get("album", ["Unknown"])[0]
length = song.info.length
mess = self.createRadiocolElt(jid.JID(referee))
added_elt = mess.firstChildElement().addElement(('','song_added'))
added_elt = mess.firstChildElement().addElement(('', 'song_added'))
added_elt['filename'] = filename = os.path.basename(song_path)
added_elt['title'] = title
added_elt['artist'] = artist
@ -212,8 +211,8 @@ class Radiocol(object):
added_elt['length'] = str(length)
self.host.profiles[profile].xmlstream.send(mess)
radio_data = self.radios[jid.JID(referee).userhost()] #FIXME: referee comes from Libervia's client side, it's unsecure
radio_data['to_delete'][filename] = song_path #FIXME: works only because of the same host trick, see the note under the docstring
radio_data = self.radios[jid.JID(referee).userhost()] # FIXME: referee comes from Libervia's client side, it's unsecure
radio_data['to_delete'][filename] = song_path # FIXME: works only because of the same host trick, see the note under the docstring
def playNext(self, room_jid, profile):
""""Play next sont in queue if exists, and put a timer
@ -231,14 +230,14 @@ class Radiocol(object):
filename, length = queue.pop(0)
mess = self.createRadiocolElt(room_jid)
play_elt = mess.firstChildElement().addElement(('','play'))
play_elt = mess.firstChildElement().addElement(('', 'play'))
play_elt['filename'] = filename
self.host.profiles[profile].xmlstream.send(mess)
if not radio_data['upload'] and len(queue) < QUEUE_LIMIT:
#upload is blocked and we now have resources to get more, we reactivate it
mess = self.createRadiocolElt(room_jid)
no_upload_elt = mess.firstChildElement().addElement(('','upload_ok'))
no_upload_elt = mess.firstChildElement().addElement(('', 'upload_ok'))
self.host.profiles[profile].xmlstream.send(mess)
radio_data['upload'] = True
@ -250,8 +249,7 @@ class Radiocol(object):
return
#we wait more than the song length to delete the file, to manage poorly reactive networks/clients
reactor.callLater(length + 90, unlink, file_to_delete) #FIXME: same host trick (see above)
reactor.callLater(length + 90, unlink, file_to_delete) # FIXME: same host trick (see above)
def radiocol_game_cmd(self, mess_elt, profile):
#FIXME: we should check sender (is it referee ?) here before accepting commands
@ -264,26 +262,26 @@ class Radiocol(object):
for elt in radio_elt.elements():
if elt.name == 'started': #new game created
if elt.name == 'started': # new game created
self.host.bridge.radiocolStarted(room_jid.userhost(), from_jid.full(), profile)
elif elt.name == 'preload': #a song is in queue and must be preloaded
elif elt.name == 'preload': # a song is in queue and must be preloaded
self.host.bridge.radiocolPreload(room_jid.userhost(), elt['filename'], elt['title'], elt['artist'], elt['album'], profile)
elif elt.name == 'play':
self.host.bridge.radiocolPlay(room_jid.userhost(), elt['filename'], profile)
elif elt.name == 'song_rejected': #a song has been refused
elif elt.name == 'song_rejected': # a song has been refused
self.host.bridge.radiocolSongRejected(room_jid.userhost(), elt['reason'], profile)
elif elt.name == 'no_upload':
self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile)
elif elt.name == 'upload_ok':
self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
elif elt.name == 'song_added': #a song has been added
elif elt.name == 'song_added': # a song has been added
#FIXME: we are KISS for the proof of concept: every song is added, to a limit of 3 in queue.
# Need to manage some sort of rules to allow peoples to send songs
if len(queue) >= QUEUE_LIMIT:
#there are already too many songs in queue, we reject this one
mess = self.createRadiocolElt(room_jid)
reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
reject_elt = mess.firstChildElement().addElement(('', 'song_rejected'))
reject_elt['sender'] = from_jid.resource
reject_elt['reason'] = "Too many songs in queue"
#FIXME: add an error code
@ -296,12 +294,11 @@ class Radiocol(object):
if len(queue) >= QUEUE_LIMIT:
#We are at the limit, we refuse new upload until next play
mess = self.createRadiocolElt(room_jid)
no_upload_elt = mess.firstChildElement().addElement(('','no_upload'))
no_upload_elt = mess.firstChildElement().addElement(('', 'no_upload'))
#FIXME: add an error code
self.host.profiles[profile].xmlstream.send(mess)
radio_data['upload'] = False
mess = self.createRadiocolElt(room_jid)
preload_elt = self.__create_preload_elt(from_jid.resource,
elt['filename'],
@ -316,10 +313,11 @@ class Radiocol(object):
radio_data['playing'] = True
self.playNext(room_jid, profile)
else:
error (_('Unmanaged game element: %s') % elt.name)
error(_('Unmanaged game element: %s') % elt.name)
def getHandler(self, profile):
return RadiocolHandler(self)
return RadiocolHandler(self)
class RadiocolHandler (XMPPHandler):
implements(iwokkel.IDisco)
@ -329,11 +327,10 @@ class RadiocolHandler (XMPPHandler):
self.host = plugin_parent.host
def connectionInitialized(self):
self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile = self.parent.profile)
self.xmlstream.addObserver(RADIOC_REQUEST, self.plugin_parent.radiocol_game_cmd, profile=self.parent.profile)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
return [disco.DiscoFeature(NC_RADIOCOL)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
return []