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.
This commit is contained in:
Goffi 2023-04-08 13:54:42 +02:00
commit 6a6f15707a

View file

@ -73,10 +73,10 @@ class RadiocolTest(helpers.SatTestCase):
self.host
)
for profile in Const.PROFILE:
self.host.getClient(profile) # init self.host.profiles[profile]
self.host.get_client(profile) # init self.host.profiles[profile]
self.songs = []
self.playlist = []
self.sound_dir = self.host.memory.getConfig("", "media_dir") + "/test/sound/"
self.sound_dir = self.host.memory.config_get("", "media_dir") + "/test/sound/"
try:
for filename in os.listdir(self.sound_dir):
if filename.endswith(".ogg") or filename.endswith(".mp3"):
@ -84,7 +84,7 @@ class RadiocolTest(helpers.SatTestCase):
except OSError:
raise SkipTest("The sound samples in sat_media/test/sound were not found")
def _buildPlayers(self, players=[]):
def _build_players(self, players=[]):
"""@return: the "started" content built with the given players"""
content = "<started"
if not players:
@ -96,7 +96,7 @@ class RadiocolTest(helpers.SatTestCase):
content += "</started>"
return content
def _expectedMessage(self, to_jid, type_, content):
def _expected_message(self, to_jid, type_, content):
"""
@param to_jid: recipient full jid
@param type_: message type ('normal' or 'groupchat')
@ -118,56 +118,56 @@ class RadiocolTest(helpers.SatTestCase):
plugin.RADIOC_TAG,
)
def _rejectSongCb(self, profile_index):
def _reject_song_cb(self, profile_index):
"""Check if the message "song_rejected" has been sent by the referee
and process the command with the profile of the uploader
@param profile_index: uploader's profile"""
sent = self.host.getSentMessage(0)
sent = self.host.get_sent_message(0)
content = "<song_rejected xmlns='' reason='Too many songs in queue'/>"
self.assertEqualXML(
self.assert_equal_xml(
sent.toXml(),
self._expectedMessage(
self._expected_message(
JID(
ROOM_JID.userhost()
+ "/"
+ self.plugin_0045.getNick(0, profile_index),
+ self.plugin_0045.get_nick(0, profile_index),
"normal",
content,
)
),
)
self._roomGameCmd(
sent, ["radiocolSongRejected", ROOM_JID.full(), "Too many songs in queue"]
self._room_game_cmd(
sent, ["radiocol_song_rejected", ROOM_JID.full(), "Too many songs in queue"]
)
def _noUploadCb(self):
def _no_upload_cb(self):
"""Check if the message "no_upload" has been sent by the referee
and process the command with the profiles of each room users"""
sent = self.host.getSentMessage(0)
sent = self.host.get_sent_message(0)
content = "<no_upload xmlns=''/>"
self.assertEqualXML(
sent.toXml(), self._expectedMessage(ROOM_JID, "groupchat", content)
self.assert_equal_xml(
sent.toXml(), self._expected_message(ROOM_JID, "groupchat", content)
)
self._roomGameCmd(sent, ["radiocolNoUpload", ROOM_JID.full()])
self._room_game_cmd(sent, ["radiocol_no_upload", ROOM_JID.full()])
def _uploadOkCb(self):
def _upload_ok_cb(self):
"""Check if the message "upload_ok" has been sent by the referee
and process the command with the profiles of each room users"""
sent = self.host.getSentMessage(0)
sent = self.host.get_sent_message(0)
content = "<upload_ok xmlns=''/>"
self.assertEqualXML(
sent.toXml(), self._expectedMessage(ROOM_JID, "groupchat", content)
self.assert_equal_xml(
sent.toXml(), self._expected_message(ROOM_JID, "groupchat", content)
)
self._roomGameCmd(sent, ["radiocolUploadOk", ROOM_JID.full()])
self._room_game_cmd(sent, ["radiocol_upload_ok", ROOM_JID.full()])
def _preloadCb(self, attrs, profile_index):
def _preload_cb(self, attrs, profile_index):
"""Check if the message "preload" has been sent by the referee
and process the command with the profiles of each room users
@param attrs: information dict about the song
@param profile_index: profile index of the uploader
"""
sent = self.host.getSentMessage(0)
attrs["sender"] = self.plugin_0045.getNick(0, profile_index)
sent = self.host.get_sent_message(0)
attrs["sender"] = self.plugin_0045.get_nick(0, profile_index)
radiocol_elt = next(domish.generateElementsNamed(sent.elements(), "radiocol"))
preload_elt = next(domish.generateElementsNamed(
radiocol_elt.elements(), "preload"
@ -178,13 +178,13 @@ class RadiocolTest(helpers.SatTestCase):
)
if sent.hasAttribute("from"):
del sent["from"]
self.assertEqualXML(
sent.toXml(), self._expectedMessage(ROOM_JID, "groupchat", content)
self.assert_equal_xml(
sent.toXml(), self._expected_message(ROOM_JID, "groupchat", content)
)
self._roomGameCmd(
self._room_game_cmd(
sent,
[
"radiocolPreload",
"radiocol_preload",
ROOM_JID.full(),
attrs["timestamp"],
attrs["filename"],
@ -195,25 +195,25 @@ class RadiocolTest(helpers.SatTestCase):
],
)
def _playNextSongCb(self):
def _play_next_song_cb(self):
"""Check if the message "play" has been sent by the referee
and process the command with the profiles of each room users"""
sent = self.host.getSentMessage(0)
sent = self.host.get_sent_message(0)
filename = self.playlist.pop(0)
content = "<play xmlns='' filename='%s' />" % filename
self.assertEqualXML(
sent.toXml(), self._expectedMessage(ROOM_JID, "groupchat", content)
self.assert_equal_xml(
sent.toXml(), self._expected_message(ROOM_JID, "groupchat", content)
)
self._roomGameCmd(sent, ["radiocolPlay", ROOM_JID.full(), filename])
self._room_game_cmd(sent, ["radiocol_play", ROOM_JID.full(), filename])
game_data = self.plugin.games[ROOM_JID]
if len(game_data["queue"]) == plugin.QUEUE_LIMIT - 1:
self._uploadOkCb()
self._upload_ok_cb()
def _addSongCb(self, d, filepath, profile_index):
def _add_song_cb(self, d, filepath, profile_index):
"""Check if the message "song_added" has been sent by the uploader
and process the command with the profile of the referee
@param d: deferred value or failure got from self.plugin.radiocolSongAdded
@param d: deferred value or failure got from self.plugin.radiocol_song_added
@param filepath: full path to the sound file
@param profile_index: the profile index of the uploader
"""
@ -250,9 +250,9 @@ class RadiocolTest(helpers.SatTestCase):
content = "<song_added xmlns='' %s/>" % " ".join(
["%s='%s'" % (attr, attrs[attr]) for attr in attrs]
)
sent = self.host.getSentMessage(profile_index)
self.assertEqualXML(
sent.toXml(), self._expectedMessage(REFEREE_FULL, "normal", content)
sent = self.host.get_sent_message(profile_index)
self.assert_equal_xml(
sent.toXml(), self._expected_message(REFEREE_FULL, "normal", content)
)
reject_song = len(game_data["queue"]) >= plugin.QUEUE_LIMIT
@ -262,20 +262,20 @@ class RadiocolTest(helpers.SatTestCase):
and len(game_data["queue"]) + 1 == plugin.QUEUE_TO_START
)
self._roomGameCmd(sent, profile_index) # queue unchanged or +1
self._room_game_cmd(sent, profile_index) # queue unchanged or +1
if reject_song:
self._rejectSongCb(profile_index)
self._reject_song_cb(profile_index)
return
if no_upload:
self._noUploadCb()
self._preloadCb(attrs, profile_index)
self._no_upload_cb()
self._preload_cb(attrs, profile_index)
self.playlist.append(attrs["filename"])
if play_next:
self._playNextSongCb() # queue -1
self._play_next_song_cb() # queue -1
def _roomGameCmd(self, sent, from_index=0, call=[]):
def _room_game_cmd(self, sent, from_index=0, call=[]):
"""Process a command. It is also possible to call this method as
_roomGameCmd(sent, call) instead of _roomGameCmd(sent, from_index, call).
_room_game_cmd(sent, call) instead of _room_game_cmd(sent, from_index, call).
If from index is a list, it is assumed that it is containing the value
for call and from_index will take its default value.
@param sent: the sent message that we need to process
@ -287,14 +287,14 @@ class RadiocolTest(helpers.SatTestCase):
call = from_index
from_index = 0
sent["from"] = ROOM_JID.full() + "/" + self.plugin_0045.getNick(0, from_index)
sent["from"] = ROOM_JID.full() + "/" + self.plugin_0045.get_nick(0, from_index)
recipient = JID(sent["to"]).resource
# The message could have been sent to a room user (room_jid + '/' + nick),
# but when it is received, the 'to' attribute of the message has been
# changed to the recipient own JID. We need to simulate that here.
if recipient:
room = self.plugin_0045.getRoom(0, 0)
room = self.plugin_0045.get_room(0, 0)
sent["to"] = (
Const.JID_STR[0]
if recipient == room.nick
@ -302,33 +302,33 @@ class RadiocolTest(helpers.SatTestCase):
)
for index in range(0, len(Const.PROFILE)):
nick = self.plugin_0045.getNick(0, index)
nick = self.plugin_0045.get_nick(0, index)
if nick:
if not recipient or nick == recipient:
if call and (
self.plugin.isPlayer(ROOM_JID, nick)
or call[0] == "radiocolStarted"
self.plugin.is_player(ROOM_JID, nick)
or call[0] == "radiocol_started"
):
args = copy.deepcopy(call)
args.append(Const.PROFILE[index])
self.host.bridge.expectCall(*args)
self.host.bridge.expect_call(*args)
self.plugin.room_game_cmd(sent, Const.PROFILE[index])
def _syncCb(self, sync_data, profile_index):
def _sync_cb(self, sync_data, profile_index):
"""Synchronize one player when he joins a running game.
@param sync_data: result from self.plugin.getSyncData
@param profile_index: index of the profile to be synchronized
"""
for nick in sync_data:
expected = self._expectedMessage(
expected = self._expected_message(
JID(ROOM_JID.userhost() + "/" + nick), "normal", sync_data[nick]
)
sent = self.host.getSentMessage(0)
self.assertEqualXML(sent.toXml(), expected)
sent = self.host.get_sent_message(0)
self.assert_equal_xml(sent.toXml(), expected)
for elt in sync_data[nick]:
if elt.name == "preload":
self.host.bridge.expectCall(
"radiocolPreload",
self.host.bridge.expect_call(
"radiocol_preload",
ROOM_JID.full(),
elt["timestamp"],
elt["filename"],
@ -339,48 +339,48 @@ class RadiocolTest(helpers.SatTestCase):
Const.PROFILE[profile_index],
)
elif elt.name == "play":
self.host.bridge.expectCall(
"radiocolPlay",
self.host.bridge.expect_call(
"radiocol_play",
ROOM_JID.full(),
elt["filename"],
Const.PROFILE[profile_index],
)
elif elt.name == "no_upload":
self.host.bridge.expectCall(
"radiocolNoUpload", ROOM_JID.full(), Const.PROFILE[profile_index]
self.host.bridge.expect_call(
"radiocol_no_upload", ROOM_JID.full(), Const.PROFILE[profile_index]
)
sync_data[nick]
self._roomGameCmd(sent, [])
self._room_game_cmd(sent, [])
def _joinRoom(self, room, nicks, player_index, sync=True):
def _join_room(self, room, nicks, player_index, sync=True):
"""Make a player join a room and update the list of nicks
@param room: wokkel.muc.Room instance from the referee perspective
@param nicks: list of the players which will be updated
@param player_index: profile index of the new player
@param sync: set to True to synchronize data
"""
user_nick = self.plugin_0045.joinRoom(0, player_index)
self.plugin.userJoinedTrigger(room, room.roster[user_nick], PROFILE)
user_nick = self.plugin_0045.join_room(0, player_index)
self.plugin.user_joined_trigger(room, room.roster[user_nick], PROFILE)
if player_index not in PLAYERS_INDICES:
# this user is actually not a player
self.assertFalse(self.plugin.isPlayer(ROOM_JID, user_nick))
self.assertFalse(self.plugin.is_player(ROOM_JID, user_nick))
to_jid, type_ = (JID(ROOM_JID.userhost() + "/" + user_nick), "normal")
else:
# this user is a player
self.assertTrue(self.plugin.isPlayer(ROOM_JID, user_nick))
self.assertTrue(self.plugin.is_player(ROOM_JID, user_nick))
nicks.append(user_nick)
to_jid, type_ = (ROOM_JID, "groupchat")
# Check that the message "players" has been sent by the referee
expected = self._expectedMessage(to_jid, type_, self._buildPlayers(nicks))
sent = self.host.getSentMessage(0)
self.assertEqualXML(sent.toXml(), expected)
expected = self._expected_message(to_jid, type_, self._build_players(nicks))
sent = self.host.get_sent_message(0)
self.assert_equal_xml(sent.toXml(), expected)
# Process the command with the profiles of each room users
self._roomGameCmd(
self._room_game_cmd(
sent,
[
"radiocolStarted",
"radiocol_started",
ROOM_JID.full(),
REFEREE_FULL.full(),
nicks,
@ -389,21 +389,21 @@ class RadiocolTest(helpers.SatTestCase):
)
if sync:
self._syncCb(self.plugin._getSyncData(ROOM_JID, [user_nick]), player_index)
self._sync_cb(self.plugin._get_sync_data(ROOM_JID, [user_nick]), player_index)
def _leaveRoom(self, room, nicks, player_index):
def _leave_room(self, room, nicks, player_index):
"""Make a player leave a room and update the list of nicks
@param room: wokkel.muc.Room instance from the referee perspective
@param nicks: list of the players which will be updated
@param player_index: profile index of the new player
"""
user_nick = self.plugin_0045.getNick(0, player_index)
user_nick = self.plugin_0045.get_nick(0, player_index)
user = room.roster[user_nick]
self.plugin_0045.leaveRoom(0, player_index)
self.plugin.userLeftTrigger(room, user, PROFILE)
self.plugin_0045.leave_room(0, player_index)
self.plugin.user_left_trigger(room, user, PROFILE)
nicks.remove(user_nick)
def _uploadSong(self, song_index, profile_index):
def _upload_song(self, song_index, profile_index):
"""Upload the song of index song_index (modulo self.songs size) from the profile of index profile_index.
@param song_index: index of the song or None to test with non existing file
@ -420,7 +420,7 @@ class RadiocolTest(helpers.SatTestCase):
expect_io_error = False
try:
d = self.plugin.radiocolSongAdded(
d = self.plugin.radiocol_song_added(
REFEREE_FULL, dst_filepath, Const.PROFILE[profile_index]
)
except IOError:
@ -428,7 +428,7 @@ class RadiocolTest(helpers.SatTestCase):
return
self.assertFalse(expect_io_error)
cb = lambda defer: self._addSongCb(defer, dst_filepath, profile_index)
cb = lambda defer: self._add_song_cb(defer, dst_filepath, profile_index)
def eb(failure):
if not isinstance(failure, Failure):
@ -451,20 +451,20 @@ class RadiocolTest(helpers.SatTestCase):
self.reinit()
# create game
self.plugin.prepareRoom(OTHER_PLAYERS, ROOM_JID, PROFILE)
self.assertTrue(self.plugin._gameExists(ROOM_JID, True))
room = self.plugin_0045.getRoom(0, 0)
nicks = [self.plugin_0045.getNick(0, 0)]
self.plugin.prepare_room(OTHER_PLAYERS, ROOM_JID, PROFILE)
self.assertTrue(self.plugin._game_exists(ROOM_JID, True))
room = self.plugin_0045.get_room(0, 0)
nicks = [self.plugin_0045.get_nick(0, 0)]
sent = self.host.getSentMessage(0)
self.assertEqualXML(
sent = self.host.get_sent_message(0)
self.assert_equal_xml(
sent.toXml(),
self._expectedMessage(ROOM_JID, "groupchat", self._buildPlayers(nicks)),
self._expected_message(ROOM_JID, "groupchat", self._build_players(nicks)),
)
self._roomGameCmd(
self._room_game_cmd(
sent,
[
"radiocolStarted",
"radiocol_started",
ROOM_JID.full(),
REFEREE_FULL.full(),
nicks,
@ -472,42 +472,42 @@ class RadiocolTest(helpers.SatTestCase):
],
)
self._joinRoom(room, nicks, 1) # player joins
self._joinRoom(room, nicks, 4) # user not playing joins
self._join_room(room, nicks, 1) # player joins
self._join_room(room, nicks, 4) # user not playing joins
song_index = 0
self._uploadSong(
self._upload_song(
song_index, 0
) # ogg or mp3 file should exist in sat_media/test/song
self._uploadSong(None, 0) # non existing file
self._upload_song(None, 0) # non existing file
# another songs are added by Const.JID[1] until the radio starts + 1 to fill the queue
# when the first song starts + 1 to be rejected because the queue is full
for song_index in range(1, plugin.QUEUE_TO_START + 1):
self._uploadSong(song_index, 1)
self._upload_song(song_index, 1)
self.plugin.playNext(Const.MUC[0], PROFILE) # simulate the end of the first song
self._playNextSongCb()
self._uploadSong(
self.plugin.play_next(Const.MUC[0], PROFILE) # simulate the end of the first song
self._play_next_song_cb()
self._upload_song(
song_index, 1
) # now the song is accepted and the queue is full again
self._joinRoom(room, nicks, 3) # new player joins
self._join_room(room, nicks, 3) # new player joins
self.plugin.playNext(Const.MUC[0], PROFILE) # the second song finishes
self._playNextSongCb()
self._uploadSong(0, 3) # the player who recently joined re-upload the first file
self.plugin.play_next(Const.MUC[0], PROFILE) # the second song finishes
self._play_next_song_cb()
self._upload_song(0, 3) # the player who recently joined re-upload the first file
self._leaveRoom(room, nicks, 1) # one player leaves
self._joinRoom(room, nicks, 1) # and join again
self._leave_room(room, nicks, 1) # one player leaves
self._join_room(room, nicks, 1) # and join again
self.plugin.playNext(Const.MUC[0], PROFILE) # empty the queue
self._playNextSongCb()
self.plugin.playNext(Const.MUC[0], PROFILE)
self._playNextSongCb()
self.plugin.play_next(Const.MUC[0], PROFILE) # empty the queue
self._play_next_song_cb()
self.plugin.play_next(Const.MUC[0], PROFILE)
self._play_next_song_cb()
for filename in self.playlist:
self.plugin.deleteFile("/tmp/" + filename)
self.plugin.delete_file("/tmp/" + filename)
return defer.succeed(None)