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

@ -49,7 +49,7 @@ PLUGIN_INFO = {
class Tarot(object):
def inheritFromRoomGame(self, host):
def inherit_from_room_game(self, host):
global RoomGame
RoomGame = host.plugins["ROOM-GAME"].__class__
self.__class__ = type(
@ -59,7 +59,7 @@ class Tarot(object):
def __init__(self, host):
log.info(_("Plugin Tarot initialization"))
self._sessions = memory.Sessions()
self.inheritFromRoomGame(host)
self.inherit_from_room_game(host)
RoomGame._init_(
self,
host,
@ -81,61 +81,61 @@ class Tarot(object):
_("Garde Sans"),
_("Garde Contre"),
]
host.bridge.addMethod(
"tarotGameLaunch",
host.bridge.add_method(
"tarot_game_launch",
".plugin",
in_sign="asss",
out_sign="",
method=self._prepareRoom,
method=self._prepare_room,
async_=True,
) # args: players, room_jid, profile
host.bridge.addMethod(
"tarotGameCreate",
host.bridge.add_method(
"tarot_game_create",
".plugin",
in_sign="sass",
out_sign="",
method=self._createGame,
method=self._create_game,
) # args: room_jid, players, profile
host.bridge.addMethod(
"tarotGameReady",
host.bridge.add_method(
"tarot_game_ready",
".plugin",
in_sign="sss",
out_sign="",
method=self._playerReady,
method=self._player_ready,
) # args: player, referee, profile
host.bridge.addMethod(
"tarotGamePlayCards",
host.bridge.add_method(
"tarot_game_play_cards",
".plugin",
in_sign="ssa(ss)s",
out_sign="",
method=self.play_cards,
) # args: player, referee, cards, profile
host.bridge.addSignal(
"tarotGamePlayers", ".plugin", signature="ssass"
host.bridge.add_signal(
"tarot_game_players", ".plugin", signature="ssass"
) # args: room_jid, referee, players, profile
host.bridge.addSignal(
"tarotGameStarted", ".plugin", signature="ssass"
host.bridge.add_signal(
"tarot_game_started", ".plugin", signature="ssass"
) # args: room_jid, referee, players, profile
host.bridge.addSignal(
"tarotGameNew", ".plugin", signature="sa(ss)s"
host.bridge.add_signal(
"tarot_game_new", ".plugin", signature="sa(ss)s"
) # args: room_jid, hand, profile
host.bridge.addSignal(
"tarotGameChooseContrat", ".plugin", signature="sss"
host.bridge.add_signal(
"tarot_game_choose_contrat", ".plugin", signature="sss"
) # args: room_jid, xml_data, profile
host.bridge.addSignal(
"tarotGameShowCards", ".plugin", signature="ssa(ss)a{ss}s"
host.bridge.add_signal(
"tarot_game_show_cards", ".plugin", signature="ssa(ss)a{ss}s"
) # args: room_jid, type ["chien", "poignée",...], cards, data[dict], profile
host.bridge.addSignal(
"tarotGameCardsPlayed", ".plugin", signature="ssa(ss)s"
host.bridge.add_signal(
"tarot_game_cards_played", ".plugin", signature="ssa(ss)s"
) # args: room_jid, player, type ["chien", "poignée",...], cards, data[dict], profile
host.bridge.addSignal(
"tarotGameYourTurn", ".plugin", signature="ss"
host.bridge.add_signal(
"tarot_game_your_turn", ".plugin", signature="ss"
) # args: room_jid, profile
host.bridge.addSignal(
"tarotGameScore", ".plugin", signature="ssasass"
host.bridge.add_signal(
"tarot_game_score", ".plugin", signature="ssasass"
) # args: room_jid, xml_data, winners (list of nicks), loosers (list of nicks), profile
host.bridge.addSignal(
"tarotGameInvalidCards", ".plugin", signature="ssa(ss)a(ss)s"
host.bridge.add_signal(
"tarot_game_invalid_cards", ".plugin", signature="ssa(ss)a(ss)s"
) # args: room_jid, game phase, played_cards, invalid_cards, profile
self.deck_ordered = []
for value in ["excuse"] + list(map(str, list(range(1, 22)))):
@ -143,10 +143,10 @@ class Tarot(object):
for suit in ["pique", "coeur", "carreau", "trefle"]:
for value in list(map(str, list(range(1, 11)))) + ["valet", "cavalier", "dame", "roi"]:
self.deck_ordered.append(TarotCard((suit, value)))
self.__choose_contrat_id = host.registerCallback(
self._contratChoosed, with_data=True
self.__choose_contrat_id = host.register_callback(
self._contrat_choosed, with_data=True
)
self.__score_id = host.registerCallback(self._scoreShowed, with_data=True)
self.__score_id = host.register_callback(self._score_showed, with_data=True)
def __card_list_to_xml(self, cards_list, elt_name):
"""Convert a card list to domish element"""
@ -519,13 +519,13 @@ class Tarot(object):
to_jid = jid.JID(room_jid.userhost() + "/" + next_player) # FIXME: gof:
self.send(to_jid, "your_turn", profile=profile)
def _contratChoosed(self, raw_data, profile):
def _contrat_choosed(self, raw_data, profile):
"""Will be called when the contrat is selected
@param raw_data: contains the choosed session id and the chosen contrat
@param profile_key: profile
"""
try:
session_data = self._sessions.profileGet(raw_data["session_id"], profile)
session_data = self._sessions.profile_get(raw_data["session_id"], profile)
except KeyError:
log.warning(_("session id doesn't exist, session has probably expired"))
# TODO: send error dialog
@ -533,8 +533,8 @@ class Tarot(object):
room_jid = session_data["room_jid"]
referee_jid = self.games[room_jid]["referee"]
player = self.host.plugins["XEP-0045"].getRoomNick(room_jid, profile)
data = xml_tools.XMLUIResult2DataFormResult(raw_data)
player = self.host.plugins["XEP-0045"].get_room_nick(room_jid, profile)
data = xml_tools.xmlui_result_2_data_form_result(raw_data)
contrat = data["contrat"]
log.debug(
_("contrat [%(contrat)s] choosed by %(profile)s")
@ -551,13 +551,13 @@ class Tarot(object):
del self._sessions[raw_data["session_id"]]
return d
def _scoreShowed(self, raw_data, profile):
def _score_showed(self, raw_data, profile):
"""Will be called when the player closes the score dialog
@param raw_data: nothing to retrieve from here but the session id
@param profile_key: profile
"""
try:
session_data = self._sessions.profileGet(raw_data["session_id"], profile)
session_data = self._sessions.profile_get(raw_data["session_id"], profile)
except KeyError:
log.warning(_("session id doesn't exist, session has probably expired"))
# TODO: send error dialog
@ -565,7 +565,7 @@ class Tarot(object):
room_jid_s = session_data["room_jid"].userhost()
# XXX: empty hand means to the frontend "reset the display"...
self.host.bridge.tarotGameNew(room_jid_s, [], profile)
self.host.bridge.tarot_game_new(room_jid_s, [], profile)
del self._sessions[raw_data["session_id"]]
return defer.succeed({})
@ -576,7 +576,7 @@ class Tarot(object):
@cards: cards played (list of tuples)
@profile_key: profile
"""
profile = self.host.memory.getProfileName(profile_key)
profile = self.host.memory.get_profile_name(profile_key)
if not profile:
log.error(_("profile %s is unknown") % profile_key)
return
@ -587,7 +587,7 @@ class Tarot(object):
elem = self.__card_list_to_xml(TarotCard.from_tuples(cards), "cards_played")
self.send(jid.JID(referee), elem, {"player": player}, profile=profile)
def newRound(self, room_jid, profile):
def new_round(self, room_jid, profile):
game_data = self.games[room_jid]
players = game_data["players"]
game_data["first_player"] = None # first player for the current trick
@ -613,7 +613,7 @@ class Tarot(object):
for player in players:
msg_elts[player] = self.__card_list_to_xml(hand[player], "hand")
RoomGame.newRound(self, room_jid, (common_data, msg_elts), profile)
RoomGame.new_round(self, room_jid, (common_data, msg_elts), profile)
pl_idx = game_data["current_player"] = (game_data["init_player"] + 1) % len(
players
@ -626,14 +626,14 @@ class Tarot(object):
"""
@param mess_elt: instance of twisted.words.xish.domish.Element
"""
client = self.host.getClient(profile)
client = self.host.get_client(profile)
from_jid = jid.JID(mess_elt["from"])
room_jid = jid.JID(from_jid.userhost())
nick = self.host.plugins["XEP-0045"].getRoomNick(client, room_jid)
nick = self.host.plugins["XEP-0045"].get_room_nick(client, room_jid)
game_elt = mess_elt.firstChildElement()
game_data = self.games[room_jid]
is_player = self.isPlayer(room_jid, nick)
is_player = self.is_player(room_jid, nick)
if "players_data" in game_data:
players_data = game_data["players_data"]
@ -649,9 +649,9 @@ class Tarot(object):
for player in elt.elements():
players.append(str(player))
signal = (
self.host.bridge.tarotGameStarted
self.host.bridge.tarot_game_started
if elt.name == "started"
else self.host.bridge.tarotGamePlayers
else self.host.bridge.tarot_game_players
)
signal(room_jid.userhost(), from_jid.full(), players, profile)
@ -667,21 +667,21 @@ class Tarot(object):
if (
list(status.values()).count("ready") == nb_players
): # everybody is ready, we can start the game
self.newRound(room_jid, profile)
self.new_round(room_jid, profile)
elif elt.name == "hand": # a new hand has been received
self.host.bridge.tarotGameNew(
self.host.bridge.tarot_game_new(
room_jid.userhost(), self.__xml_to_list(elt), profile
)
elif elt.name == "contrat": # it's time to choose contrat
form = data_form.Form.fromElement(elt.firstChildElement())
session_id, session_data = self._sessions.newSession(profile=profile)
session_id, session_data = self._sessions.new_session(profile=profile)
session_data["room_jid"] = room_jid
xml_data = xml_tools.dataForm2XMLUI(
xml_data = xml_tools.data_form_2_xmlui(
form, self.__choose_contrat_id, session_id
).toXml()
self.host.bridge.tarotGameChooseContrat(
self.host.bridge.tarot_game_choose_contrat(
room_jid.userhost(), xml_data, profile
)
@ -752,7 +752,7 @@ class Tarot(object):
data = {"attaquant": elt["attaquant"]}
game_data["stage"] = "ecart"
game_data["attaquant"] = elt["attaquant"]
self.host.bridge.tarotGameShowCards(
self.host.bridge.tarot_game_show_cards(
room_jid.userhost(), "chien", self.__xml_to_list(elt), data, profile
)
@ -790,7 +790,7 @@ class Tarot(object):
cards = TarotCard.from_tuples(self.__xml_to_list(elt))
if mess_elt["type"] == "groupchat":
self.host.bridge.tarotGameCardsPlayed(
self.host.bridge.tarot_game_cards_played(
room_jid.userhost(),
elt["player"],
self.__xml_to_list(elt),
@ -858,7 +858,7 @@ class Tarot(object):
self.send(to_jid, "your_turn", profile=profile)
elif elt.name == "your_turn":
self.host.bridge.tarotGameYourTurn(room_jid.userhost(), profile)
self.host.bridge.tarot_game_your_turn(room_jid.userhost(), profile)
elif elt.name == "score":
form_elt = next(elt.elements(name="x", uri="jabber:x:data"))
@ -869,12 +869,12 @@ class Tarot(object):
for looser in elt.elements(name="looser", uri=NS_CG):
loosers.append(str(looser))
form = data_form.Form.fromElement(form_elt)
session_id, session_data = self._sessions.newSession(profile=profile)
session_id, session_data = self._sessions.new_session(profile=profile)
session_data["room_jid"] = room_jid
xml_data = xml_tools.dataForm2XMLUI(
xml_data = xml_tools.data_form_2_xmlui(
form, self.__score_id, session_id
).toXml()
self.host.bridge.tarotGameScore(
self.host.bridge.tarot_game_score(
room_jid.userhost(), xml_data, winners, loosers, profile
)
elif elt.name == "error":
@ -885,7 +885,7 @@ class Tarot(object):
invalid_cards = self.__xml_to_list(
next(elt.elements(name="invalid", uri=NS_CG))
)
self.host.bridge.tarotGameInvalidCards(
self.host.bridge.tarot_game_invalid_cards(
room_jid.userhost(),
elt["phase"],
played_cards,
@ -897,5 +897,5 @@ class Tarot(object):
else:
log.error(_("Unmanaged card game element: %s") % elt.name)
def getSyncDataForPlayer(self, room_jid, nick):
def get_sync_data_for_player(self, room_jid, nick):
return []