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

@ -79,14 +79,14 @@ class XEP_0054(object):
log.info(_("Plugin XEP_0054 initialization"))
self.host = host
self._i = host.plugins['IDENTITY']
self._i.register(IMPORT_NAME, 'avatar', self.getAvatar, self.setAvatar)
self._i.register(IMPORT_NAME, 'nicknames', self.getNicknames, self.setNicknames)
host.trigger.add("presence_available", self.presenceAvailableTrigger)
self._i.register(IMPORT_NAME, 'avatar', self.get_avatar, self.set_avatar)
self._i.register(IMPORT_NAME, 'nicknames', self.get_nicknames, self.set_nicknames)
host.trigger.add("presence_available", self.presence_available_trigger)
def getHandler(self, client):
def get_handler(self, client):
return XEP_0054_handler(self)
def presenceAvailableTrigger(self, presence_elt, client):
def presence_available_trigger(self, presence_elt, client):
try:
avatar_hash = client._xep_0054_avatar_hashes[client.jid.userhost()]
except KeyError:
@ -99,12 +99,12 @@ class XEP_0054(object):
presence_elt.addChild(x_elt)
return True
async def profileConnecting(self, client):
async def profile_connecting(self, client):
client._xep_0054_avatar_hashes = persistent.PersistentDict(
NS_VCARD, client.profile)
await client._xep_0054_avatar_hashes.load()
def savePhoto(self, client, photo_elt, entity):
def save_photo(self, client, photo_elt, entity):
"""Parse a <PHOTO> photo_elt and save the picture"""
# XXX: this method is launched in a separate thread
try:
@ -149,7 +149,7 @@ class XEP_0054(object):
raise Failure(exceptions.DataError(msg))
image_hash = sha1(decoded).hexdigest()
with self.host.common_cache.cacheData(
with self.host.common_cache.cache_data(
PLUGIN_INFO["import_name"],
image_hash,
mime_type,
@ -157,7 +157,7 @@ class XEP_0054(object):
f.write(decoded)
return image_hash
async def vCard2Dict(self, client, vcard_elt, entity_jid):
async def v_card_2_dict(self, client, vcard_elt, entity_jid):
"""Convert a VCard_elt to a dict, and save binaries"""
log.debug(("parsing vcard_elt"))
vcard_dict = {}
@ -184,7 +184,7 @@ class XEP_0054(object):
# TODO: handle EXTVAL
try:
avatar_hash = await threads.deferToThread(
self.savePhoto, client, elem, entity_jid
self.save_photo, client, elem, entity_jid
)
except (exceptions.DataError, exceptions.NotFound):
avatar_hash = ""
@ -199,7 +199,7 @@ class XEP_0054(object):
entity_jid.full(), avatar_hash)
if avatar_hash:
avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
avatar_cache = self.host.common_cache.get_metadata(avatar_hash)
await self._i.update(
client,
IMPORT_NAME,
@ -220,7 +220,7 @@ class XEP_0054(object):
return vcard_dict
async def getVCardElement(self, client, entity_jid):
async def get_vcard_element(self, client, entity_jid):
"""Retrieve domish.Element of a VCard
@param entity_jid(jid.JID): entity from who we need the vCard
@ -239,14 +239,14 @@ class XEP_0054(object):
).format(entity_jid=entity_jid, xml=iq_ret_elt.toXml()))
raise exceptions.DataError(f"no vCard element found for {entity_jid}")
async def updateVCardElt(self, client, entity_jid, to_replace):
async def update_vcard_elt(self, client, entity_jid, to_replace):
"""Create a vcard element to replace some metadata
@param to_replace(list[str]): list of vcard element names to remove
"""
try:
# we first check if a vcard already exists, to keep data
vcard_elt = await self.getVCardElement(client, entity_jid)
vcard_elt = await self.get_vcard_element(client, entity_jid)
except error.StanzaError as e:
if e.condition == "item-not-found":
vcard_elt = domish.Element((NS_VCARD, "vCard"))
@ -266,16 +266,16 @@ class XEP_0054(object):
return vcard_elt
async def getCard(self, client, entity_jid):
async def get_card(self, client, entity_jid):
"""Ask server for VCard
@param entity_jid(jid.JID): jid from which we want the VCard
@result(dict): vCard data
"""
entity_jid = self._i.getIdentityJid(client, entity_jid)
entity_jid = self._i.get_identity_jid(client, entity_jid)
log.debug(f"Asking for {entity_jid}'s VCard")
try:
vcard_elt = await self.getVCardElement(client, entity_jid)
vcard_elt = await self.get_vcard_element(client, entity_jid)
except exceptions.DataError:
self._i.update(client, IMPORT_NAME, "avatar", None, entity_jid)
except Exception as e:
@ -284,9 +284,9 @@ class XEP_0054(object):
).format(entity_jid=entity_jid, e=e))
else:
log.debug(_("VCard found"))
return await self.vCard2Dict(client, vcard_elt, entity_jid)
return await self.v_card_2_dict(client, vcard_elt, entity_jid)
async def getAvatar(
async def get_avatar(
self,
client: SatXMPPEntity,
entity_jid: jid.JID
@ -296,9 +296,9 @@ class XEP_0054(object):
@param entity: entity to get avatar from
@return: avatar metadata, or None if no avatar has been found
"""
entity_jid = self._i.getIdentityJid(client, entity_jid)
entity_jid = self._i.get_identity_jid(client, entity_jid)
hashes_cache = client._xep_0054_avatar_hashes
vcard = await self.getCard(client, entity_jid)
vcard = await self.get_card(client, entity_jid)
if vcard is None:
return None
try:
@ -312,18 +312,18 @@ class XEP_0054(object):
if not avatar_hash:
return None
avatar_cache = self.host.common_cache.getMetadata(avatar_hash)
return self._i.avatarBuildMetadata(
avatar_cache = self.host.common_cache.get_metadata(avatar_hash)
return self._i.avatar_build_metadata(
avatar_cache['path'], avatar_cache['mime_type'], avatar_hash)
async def setAvatar(self, client, avatar_data, entity):
async def set_avatar(self, client, avatar_data, entity):
"""Set avatar of the profile
@param avatar_data(dict): data of the image to use as avatar, as built by
IDENTITY plugin.
@param entity(jid.JID): entity whose avatar must be changed
"""
vcard_elt = await self.updateVCardElt(client, entity, ['PHOTO'])
vcard_elt = await self.update_vcard_elt(client, entity, ['PHOTO'])
iq_elt = client.IQ()
iq_elt.addChild(vcard_elt)
@ -337,19 +337,19 @@ class XEP_0054(object):
# FIXME: should send the current presence, not always "available" !
await client.presence.available()
async def getNicknames(self, client, entity):
async def get_nicknames(self, client, entity):
"""get nick from cache, or check vCard
@param entity(jid.JID): entity to get nick from
@return(list[str]): nicknames found
"""
vcard_data = await self.getCard(client, entity)
vcard_data = await self.get_card(client, entity)
try:
return [vcard_data['nickname']]
except (KeyError, TypeError):
return []
async def setNicknames(self, client, nicknames, entity):
async def set_nicknames(self, client, nicknames, entity):
"""Update our vCard and set a nickname
@param nicknames(list[str]): new nicknames to use
@ -357,7 +357,7 @@ class XEP_0054(object):
"""
nick = nicknames[0].strip()
vcard_elt = await self.updateVCardElt(client, entity, ['NICKNAME'])
vcard_elt = await self.update_vcard_elt(client, entity, ['NICKNAME'])
if nick:
vcard_elt.addElement((NS_VCARD, "NICKNAME"), content=nick)
@ -389,7 +389,7 @@ class XEP_0054_handler(XMPPHandler):
@param presence(domish.Element): <presence/> stanza
"""
client = self.parent
entity_jid = self.plugin_parent._i.getIdentityJid(
entity_jid = self.plugin_parent._i.get_identity_jid(
client, jid.JID(presence["from"]))
try:
@ -414,14 +414,14 @@ class XEP_0054_handler(XMPPHandler):
# no change, we can return…
if given_hash:
# …but we double check that avatar is in cache
avatar_cache = self.host.common_cache.getMetadata(given_hash)
avatar_cache = self.host.common_cache.get_metadata(given_hash)
if avatar_cache is None:
log.debug(
f"Avatar for [{entity_jid}] is known but not in cache, we get "
f"it"
)
# getCard will put the avatar in cache
await self.plugin_parent.getCard(client, entity_jid)
# get_card will put the avatar in cache
await self.plugin_parent.get_card(client, entity_jid)
else:
log.debug(f"avatar for {entity_jid} is already in cache")
return
@ -438,7 +438,7 @@ class XEP_0054_handler(XMPPHandler):
# the avatar has been removed, no need to go further
return
avatar_cache = self.host.common_cache.getMetadata(given_hash)
avatar_cache = self.host.common_cache.get_metadata(given_hash)
if avatar_cache is not None:
log.debug(
f"New avatar found for [{entity_jid}], it's already in cache, we use it"
@ -458,7 +458,7 @@ class XEP_0054_handler(XMPPHandler):
log.debug(
"New avatar found for [{entity_jid}], requesting vcard"
)
vcard = await self.plugin_parent.getCard(client, entity_jid)
vcard = await self.plugin_parent.get_card(client, entity_jid)
if vcard is None:
log.warning(f"Unexpected empty vCard for {entity_jid}")
return