diff sat/plugins/plugin_misc_radiocol.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents be6d91572633
children
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_radiocol.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_radiocol.py	Sat Apr 08 13:54:42 2023 +0200
@@ -65,7 +65,7 @@
 
 
 class Radiocol(object):
-    def inheritFromRoomGame(self, host):
+    def inherit_from_room_game(self, host):
         global RoomGame
         RoomGame = host.plugins["ROOM-GAME"].__class__
         self.__class__ = type(
@@ -74,7 +74,7 @@
 
     def __init__(self, host):
         log.info(_("Radio collective initialization"))
-        self.inheritFromRoomGame(host)
+        self.inherit_from_room_game(host)
         RoomGame._init_(
             self,
             host,
@@ -89,49 +89,49 @@
             },
         )
         self.host = host
-        host.bridge.addMethod(
-            "radiocolLaunch",
+        host.bridge.add_method(
+            "radiocol_launch",
             ".plugin",
             in_sign="asss",
             out_sign="",
-            method=self._prepareRoom,
+            method=self._prepare_room,
             async_=True,
         )
-        host.bridge.addMethod(
-            "radiocolCreate",
+        host.bridge.add_method(
+            "radiocol_create",
             ".plugin",
             in_sign="sass",
             out_sign="",
-            method=self._createGame,
+            method=self._create_game,
         )
-        host.bridge.addMethod(
-            "radiocolSongAdded",
+        host.bridge.add_method(
+            "radiocol_song_added",
             ".plugin",
             in_sign="sss",
             out_sign="",
-            method=self._radiocolSongAdded,
+            method=self._radiocol_song_added,
             async_=True,
         )
-        host.bridge.addSignal(
-            "radiocolPlayers", ".plugin", signature="ssass"
+        host.bridge.add_signal(
+            "radiocol_players", ".plugin", signature="ssass"
         )  # room_jid, referee, players, profile
-        host.bridge.addSignal(
-            "radiocolStarted", ".plugin", signature="ssasais"
+        host.bridge.add_signal(
+            "radiocol_started", ".plugin", signature="ssasais"
         )  # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile
-        host.bridge.addSignal(
-            "radiocolSongRejected", ".plugin", signature="sss"
+        host.bridge.add_signal(
+            "radiocol_song_rejected", ".plugin", signature="sss"
         )  # room_jid, reason, profile
-        host.bridge.addSignal(
-            "radiocolPreload", ".plugin", signature="ssssssss"
+        host.bridge.add_signal(
+            "radiocol_preload", ".plugin", signature="ssssssss"
         )  # room_jid, timestamp, filename, title, artist, album, profile
-        host.bridge.addSignal(
-            "radiocolPlay", ".plugin", signature="sss"
+        host.bridge.add_signal(
+            "radiocol_play", ".plugin", signature="sss"
         )  # room_jid, filename, profile
-        host.bridge.addSignal(
-            "radiocolNoUpload", ".plugin", signature="ss"
+        host.bridge.add_signal(
+            "radiocol_no_upload", ".plugin", signature="ss"
         )  # room_jid, profile
-        host.bridge.addSignal(
-            "radiocolUploadOk", ".plugin", signature="ss"
+        host.bridge.add_signal(
+            "radiocol_upload_ok", ".plugin", signature="ss"
         )  # room_jid, profile
 
     def __create_preload_elt(self, sender, song_added_elt):
@@ -143,10 +143,10 @@
         # XXX: the frontend should know the temporary directory where file is put
         return preload_elt
 
-    def _radiocolSongAdded(self, referee_s, song_path, profile):
-        return self.radiocolSongAdded(jid.JID(referee_s), song_path, profile)
+    def _radiocol_song_added(self, referee_s, song_path, profile):
+        return self.radiocol_song_added(jid.JID(referee_s), song_path, profile)
 
-    def radiocolSongAdded(self, referee, song_path, profile):
+    def radiocol_song_added(self, referee, song_path, profile):
         """This method is called by libervia when a song has been uploaded
         @param referee (jid.JID): JID of the referee in the room (room userhost + '/' + nick)
         @param song_path (unicode): absolute path of the song added
@@ -174,7 +174,7 @@
                 song = OggVorbis(song_path)
         except (OggVorbisHeaderError, HeaderNotFoundError):
             # this file is not ogg vorbis nor mp3, we reject it
-            self.deleteFile(song_path)  # FIXME: same host trick (see note above)
+            self.delete_file(song_path)  # FIXME: same host trick (see note above)
             return defer.fail(
                 exceptions.DataError(
                     D_(
@@ -200,7 +200,7 @@
         )  # FIXME: works only because of the same host trick, see the note under the docstring
         return self.send(referee, ("", "song_added"), attrs, profile=profile)
 
-    def playNext(self, room_jid, profile):
+    def play_next(self, room_jid, profile):
         """"Play next song in queue if exists, and put a timer
         which trigger after the song has been played to play next one"""
         # TODO: songs need to be erased once played or found invalids
@@ -210,7 +210,7 @@
             log.debug(_("No more participants in the radiocol: cleaning data"))
             radio_data["queue"] = []
             for filename in radio_data["to_delete"]:
-                self.deleteFile(filename, radio_data)
+                self.delete_file(filename, radio_data)
             radio_data["to_delete"] = {}
         queue = radio_data["queue"]
         if not queue:
@@ -228,13 +228,13 @@
             self.send(room_jid, ("", "upload_ok"), profile=profile)
             radio_data["upload"] = True
 
-        reactor.callLater(length, self.playNext, room_jid, profile)
+        reactor.callLater(length, self.play_next, room_jid, profile)
         # we wait more than the song length to delete the file, to manage poorly reactive networks/clients
         reactor.callLater(
-            length + 90, self.deleteFile, filename, radio_data
+            length + 90, self.delete_file, filename, radio_data
         )  # FIXME: same host trick (see above)
 
-    def deleteFile(self, filename, radio_data=None):
+    def delete_file(self, filename, radio_data=None):
         """
         Delete a previously uploaded file.
         @param filename: filename to delete, or full filepath if radio_data is None
@@ -263,16 +263,16 @@
     def room_game_cmd(self, mess_elt, profile):
         from_jid = jid.JID(mess_elt["from"])
         room_jid = from_jid.userhostJID()
-        nick = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile)
+        nick = self.host.plugins["XEP-0045"].get_room_nick(room_jid, profile)
 
         radio_elt = mess_elt.firstChildElement()
         radio_data = self.games[room_jid]
         if "queue" in radio_data:
             queue = radio_data["queue"]
 
-        from_referee = self.isReferee(room_jid, from_jid.resource)
-        to_referee = self.isReferee(room_jid, jid.JID(mess_elt["to"]).user)
-        is_player = self.isPlayer(room_jid, nick)
+        from_referee = self.is_referee(room_jid, from_jid.resource)
+        to_referee = self.is_referee(room_jid, jid.JID(mess_elt["to"]).user)
+        is_player = self.is_player(room_jid, nick)
         for elt in radio_elt.elements():
             if not from_referee and not (to_referee and elt.name == "song_added"):
                 continue  # sender must be referee, expect when a song is submitted
@@ -287,9 +287,9 @@
                 for player in elt.elements():
                     players.append(str(player))
                 signal = (
-                    self.host.bridge.radiocolStarted
+                    self.host.bridge.radiocol_started
                     if elt.name == "started"
-                    else self.host.bridge.radiocolPlayers
+                    else self.host.bridge.radiocol_players
                 )
                 signal(
                     room_jid.userhost(),
@@ -299,7 +299,7 @@
                     profile,
                 )
             elif elt.name == "preload":  # a song is in queue and must be preloaded
-                self.host.bridge.radiocolPreload(
+                self.host.bridge.radiocol_preload(
                     room_jid.userhost(),
                     elt["timestamp"],
                     elt["filename"],
@@ -310,17 +310,17 @@
                     profile,
                 )
             elif elt.name == "play":
-                self.host.bridge.radiocolPlay(
+                self.host.bridge.radiocol_play(
                     room_jid.userhost(), elt["filename"], profile
                 )
             elif elt.name == "song_rejected":  # a song has been refused
-                self.host.bridge.radiocolSongRejected(
+                self.host.bridge.radiocol_song_rejected(
                     room_jid.userhost(), elt["reason"], profile
                 )
             elif elt.name == "no_upload":
-                self.host.bridge.radiocolNoUpload(room_jid.userhost(), profile)
+                self.host.bridge.radiocol_no_upload(room_jid.userhost(), profile)
             elif elt.name == "upload_ok":
-                self.host.bridge.radiocolUploadOk(room_jid.userhost(), profile)
+                self.host.bridge.radiocol_upload_ok(room_jid.userhost(), profile)
             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
@@ -348,11 +348,11 @@
                 if not radio_data["playing"] and len(queue) == QUEUE_TO_START:
                     # We have not started playing yet, and we have QUEUE_TO_START
                     # songs in queue. We can now start the party :)
-                    self.playNext(room_jid, profile)
+                    self.play_next(room_jid, profile)
             else:
                 log.error(_("Unmanaged game element: %s") % elt.name)
 
-    def getSyncDataForPlayer(self, room_jid, nick):
+    def get_sync_data_for_player(self, room_jid, nick):
         game_data = self.games[room_jid]
         elements = []
         if game_data["playing"]: