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

@ -58,18 +58,18 @@ class XEP_0231(object):
def __init__(self, host):
log.info(_("plugin Bits of Binary initialization"))
self.host = host
host.registerNamespace("bob", NS_BOB)
host.trigger.add("xhtml_post_treat", self.XHTMLTrigger)
host.bridge.addMethod(
"bobGetFile",
host.register_namespace("bob", NS_BOB)
host.trigger.add("xhtml_post_treat", self.xhtml_trigger)
host.bridge.add_method(
"bob_get_file",
".plugin",
in_sign="sss",
out_sign="s",
method=self._getFile,
method=self._get_file,
async_=True,
)
def dumpData(self, cache, data_elt, cid):
def dump_data(self, cache, data_elt, cid):
"""save file encoded in data_elt to cache
@param cache(memory.cache.Cache): cache to use to store the data
@ -87,7 +87,7 @@ class XEP_0231(object):
log.warning("invalid max-age found")
max_age = None
with cache.cacheData(
with cache.cache_data(
PLUGIN_INFO[C.PI_IMPORT_NAME], cid, data_elt.getAttribute("type"), max_age
) as f:
@ -96,13 +96,13 @@ class XEP_0231(object):
return file_path
def getHandler(self, client):
def get_handler(self, client):
return XEP_0231_handler(self)
def _requestCb(self, iq_elt, cache, cid):
def _request_cb(self, iq_elt, cache, cid):
for data_elt in iq_elt.elements(NS_BOB, "data"):
if data_elt.getAttribute("cid") == cid:
file_path = self.dumpData(cache, data_elt, cid)
file_path = self.dump_data(cache, data_elt, cid)
return file_path
log.warning(
@ -112,12 +112,12 @@ class XEP_0231(object):
)
raise failure.Failure(exceptions.DataError("missing data"))
def _requestEb(self, failure_):
def _request_eb(self, failure_):
"""Log the error and continue errback chain"""
log.warning("Can't get requested data:\n{reason}".format(reason=failure_))
return failure_
def requestData(self, client, to_jid, cid, cache=None):
def request_data(self, client, to_jid, cid, cache=None):
"""Request data if we don't have it in cache
@param to_jid(jid.JID): jid to request the data to
@ -133,19 +133,19 @@ class XEP_0231(object):
data_elt = iq_elt.addElement((NS_BOB, "data"))
data_elt["cid"] = cid
d = iq_elt.send()
d.addCallback(self._requestCb, cache, cid)
d.addErrback(self._requestEb)
d.addCallback(self._request_cb, cache, cid)
d.addErrback(self._request_eb)
return d
def _setImgEltSrc(self, path, img_elt):
def _set_img_elt_src(self, path, img_elt):
img_elt["src"] = "file://{}".format(path)
def XHTMLTrigger(self, client, message_elt, body_elt, lang, treat_d):
for img_elt in xml_tools.findAll(body_elt, C.NS_XHTML, "img"):
def xhtml_trigger(self, client, message_elt, body_elt, lang, treat_d):
for img_elt in xml_tools.find_all(body_elt, C.NS_XHTML, "img"):
source = img_elt.getAttribute("src", "")
if source.startswith("cid:"):
cid = source[4:]
file_path = client.cache.getFilePath(cid)
file_path = client.cache.get_file_path(cid)
if file_path is not None:
#  image is in cache, we change the url
img_elt["src"] = "file://{}".format(file_path)
@ -154,17 +154,17 @@ class XEP_0231(object):
# image is not in cache, is it given locally?
for data_elt in message_elt.elements(NS_BOB, "data"):
if data_elt.getAttribute("cid") == cid:
file_path = self.dumpData(client.cache, data_elt, cid)
file_path = self.dump_data(client.cache, data_elt, cid)
img_elt["src"] = "file://{}".format(file_path)
break
else:
# cid not found locally, we need to request it
# so we use the deferred
d = self.requestData(client, jid.JID(message_elt["from"]), cid)
d.addCallback(partial(self._setImgEltSrc, img_elt=img_elt))
d = self.request_data(client, jid.JID(message_elt["from"]), cid)
d.addCallback(partial(self._set_img_elt_src, img_elt=img_elt))
treat_d.addCallback(lambda __: d)
def onComponentRequest(self, iq_elt, client):
def on_component_request(self, iq_elt, client):
"""cache data is retrieve from common cache for components"""
# FIXME: this is a security/privacy issue as no access check is done
# but this is mitigated by the fact that the cid must be known.
@ -179,7 +179,7 @@ class XEP_0231(object):
client.send(error_elt)
return
metadata = self.host.common_cache.getMetadata(cid)
metadata = self.host.common_cache.get_metadata(cid)
if metadata is None:
error_elt = jabber_error.StanzaError("item-not-found").toResponse(iq_elt)
client.send(error_elt)
@ -196,15 +196,15 @@ class XEP_0231(object):
data_elt["max-age"] = str(int(max(0, metadata["eol"] - time.time())))
client.send(result_elt)
def _getFile(self, peer_jid_s, cid, profile):
def _get_file(self, peer_jid_s, cid, profile):
peer_jid = jid.JID(peer_jid_s)
assert cid
client = self.host.getClient(profile)
d = self.getFile(client, peer_jid, cid)
client = self.host.get_client(profile)
d = self.get_file(client, peer_jid, cid)
d.addCallback(lambda path: str(path))
return d
def getFile(self, client, peer_jid, cid, parent_elt=None):
def get_file(self, client, peer_jid, cid, parent_elt=None):
"""Retrieve a file from it's content-id
@param peer_jid(jid.JID): jid of the entity offering the data
@ -214,7 +214,7 @@ class XEP_0231(object):
None to ignore
@return D(Path): path to cached data
"""
file_path = client.cache.getFilePath(cid)
file_path = client.cache.get_file_path(cid)
if file_path is not None:
#  file is in cache
return defer.succeed(file_path)
@ -223,11 +223,11 @@ class XEP_0231(object):
if parent_elt is not None:
for data_elt in parent_elt.elements(NS_BOB, "data"):
if data_elt.getAttribute("cid") == cid:
return defer.succeed(self.dumpData(client.cache, data_elt, cid))
return defer.succeed(self.dump_data(client.cache, data_elt, cid))
# cid not found locally, we need to request it
# so we use the deferred
return self.requestData(client, peer_jid, cid)
return self.request_data(client, peer_jid, cid)
@implementer(iwokkel.IDisco)
@ -240,7 +240,7 @@ class XEP_0231_handler(xmlstream.XMPPHandler):
def connectionInitialized(self):
if self.parent.is_component:
self.xmlstream.addObserver(
IQ_BOB_REQUEST, self.plugin_parent.onComponentRequest, client=self.parent
IQ_BOB_REQUEST, self.plugin_parent.on_component_request, client=self.parent
)
def getDiscoInfo(self, requestor, target, nodeIdentifier=""):