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

@ -82,19 +82,19 @@ class Send(base.CommandBase):
help=_("end-to-end encrypt the file transfer")
)
async def onProgressStarted(self, metadata):
async def on_progress_started(self, metadata):
self.disp(_("File copy started"), 2)
async def onProgressFinished(self, metadata):
async def on_progress_finished(self, metadata):
self.disp(_("File sent successfully"), 2)
async def onProgressError(self, error_msg):
async def on_progress_error(self, error_msg):
if error_msg == C.PROGRESS_ERROR_DECLINED:
self.disp(_("The file has been refused by your contact"))
else:
self.disp(_("Error while sending file: {}").format(error_msg), error=True)
async def gotId(self, data, file_):
async def got_id(self, data, file_):
"""Called when a progress id has been received
@param pid(unicode): progress id
@ -134,7 +134,7 @@ class Send(base.CommandBase):
if self.args.bz2:
with tempfile.NamedTemporaryFile("wb", delete=False) as buf:
self.host.addOnQuitCallback(os.unlink, buf.name)
self.host.add_on_quit_callback(os.unlink, buf.name)
self.disp(_("bz2 is an experimental option, use with caution"))
# FIXME: check free space
self.disp(_("Starting compression, please wait..."))
@ -150,7 +150,7 @@ class Send(base.CommandBase):
self.disp(_("Done !"), 1)
try:
send_data = await self.host.bridge.fileSend(
send_data = await self.host.bridge.file_send(
self.args.jid,
buf.name,
self.args.name or archive_name,
@ -162,12 +162,12 @@ class Send(base.CommandBase):
self.disp(f"can't send file: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
await self.gotId(send_data, file_)
await self.got_id(send_data, file_)
else:
for file_ in self.args.files:
path = os.path.abspath(file_)
try:
send_data = await self.host.bridge.fileSend(
send_data = await self.host.bridge.file_send(
self.args.jid,
path,
self.args.name,
@ -179,7 +179,7 @@ class Send(base.CommandBase):
self.disp(f"can't send file {file_!r}: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
await self.gotId(send_data, file_)
await self.got_id(send_data, file_)
class Request(base.CommandBase):
@ -241,13 +241,13 @@ class Request(base.CommandBase):
help=_("overwrite existing file without confirmation"),
)
async def onProgressStarted(self, metadata):
async def on_progress_started(self, metadata):
self.disp(_("File copy started"), 2)
async def onProgressFinished(self, metadata):
async def on_progress_finished(self, metadata):
self.disp(_("File received successfully"), 2)
async def onProgressError(self, error_msg):
async def on_progress_error(self, error_msg):
if error_msg == C.PROGRESS_ERROR_DECLINED:
self.disp(_("The file request has been refused"))
else:
@ -267,7 +267,7 @@ class Request(base.CommandBase):
message = _("File {path} already exists! Do you want to overwrite?").format(
path=path
)
await self.host.confirmOrQuit(message, _("file request cancelled"))
await self.host.confirm_or_quit(message, _("file request cancelled"))
self.full_dest_jid = await self.host.get_full_jid(self.args.jid)
extra = {}
@ -276,7 +276,7 @@ class Request(base.CommandBase):
if self.args.namespace:
extra["namespace"] = self.args.namespace
try:
progress_id = await self.host.bridge.fileJingleRequest(
progress_id = await self.host.bridge.file_jingle_request(
self.full_dest_jid,
path,
self.args.name,
@ -303,9 +303,9 @@ class Receive(base.CommandAnswering):
)
self._overwrite_refused = False # True when one overwrite as already been refused
self.action_callbacks = {
C.META_TYPE_FILE: self.onFileAction,
C.META_TYPE_OVERWRITE: self.onOverwriteAction,
C.META_TYPE_NOT_IN_ROSTER_LEAK: self.onNotInRosterAction,
C.META_TYPE_FILE: self.on_file_action,
C.META_TYPE_OVERWRITE: self.on_overwrite_action,
C.META_TYPE_NOT_IN_ROSTER_LEAK: self.on_not_in_roster_action,
}
def add_parser_options(self):
@ -335,10 +335,10 @@ class Receive(base.CommandAnswering):
help=_("destination path (default: working directory)"),
)
async def onProgressStarted(self, metadata):
async def on_progress_started(self, metadata):
self.disp(_("File copy started"), 2)
async def onProgressFinished(self, metadata):
async def on_progress_finished(self, metadata):
self.disp(_("File received successfully"), 2)
if metadata.get("hash_verified", False):
try:
@ -350,10 +350,10 @@ class Receive(base.CommandAnswering):
else:
self.disp(_("hash can't be verified"), 1)
async def onProgressError(self, e):
async def on_progress_error(self, e):
self.disp(_("Error while receiving file: {e}").format(e=e), error=True)
def getXmluiId(self, action_data):
def get_xmlui_id(self, action_data):
# FIXME: we temporarily use ElementTree, but a real XMLUI managing module
# should be available in the futur
# TODO: XMLUI module
@ -368,10 +368,10 @@ class Receive(base.CommandAnswering):
self.disp(_("Invalid XMLUI received"), error=True)
return xmlui_id
async def onFileAction(self, action_data, action_id, security_limit, profile):
xmlui_id = self.getXmluiId(action_data)
async def on_file_action(self, action_data, action_id, security_limit, profile):
xmlui_id = self.get_xmlui_id(action_data)
if xmlui_id is None:
return self.host.quitFromSignal(1)
return self.host.quit_from_signal(1)
try:
from_jid = jid.JID(action_data["meta_from_jid"])
except KeyError:
@ -386,18 +386,18 @@ class Receive(base.CommandAnswering):
if not self.bare_jids or from_jid.bare in self.bare_jids:
if self._overwrite_refused:
self.disp(_("File refused because overwrite is needed"), error=True)
await self.host.bridge.launchAction(
await self.host.bridge.action_launch(
xmlui_id, {"cancelled": C.BOOL_TRUE}, profile_key=profile
)
return self.host.quitFromSignal(2)
return self.host.quit_from_signal(2)
await self.set_progress_id(progress_id)
xmlui_data = {"path": self.path}
await self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
await self.host.bridge.action_launch(xmlui_id, xmlui_data, profile_key=profile)
async def onOverwriteAction(self, action_data, action_id, security_limit, profile):
xmlui_id = self.getXmluiId(action_data)
async def on_overwrite_action(self, action_data, action_id, security_limit, profile):
xmlui_id = self.get_xmlui_id(action_data)
if xmlui_id is None:
return self.host.quitFromSignal(1)
return self.host.quit_from_signal(1)
try:
progress_id = action_data["meta_progress_id"]
except KeyError:
@ -412,13 +412,13 @@ class Receive(base.CommandAnswering):
self.disp(_("Refused to overwrite"), 2)
self._overwrite_refused = True
xmlui_data = {"answer": C.boolConst(self.args.force)}
await self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
xmlui_data = {"answer": C.bool_const(self.args.force)}
await self.host.bridge.action_launch(xmlui_id, xmlui_data, profile_key=profile)
async def onNotInRosterAction(self, action_data, action_id, security_limit, profile):
xmlui_id = self.getXmluiId(action_data)
async def on_not_in_roster_action(self, action_data, action_id, security_limit, profile):
xmlui_id = self.get_xmlui_id(action_data)
if xmlui_id is None:
return self.host.quitFromSignal(1)
return self.host.quit_from_signal(1)
try:
from_jid = jid.JID(action_data["meta_from_jid"])
except ValueError:
@ -442,11 +442,11 @@ class Receive(base.CommandAnswering):
xmlui = xmlui_manager.create(self.host, action_data["xmlui"])
confirmed = await self.host.confirm(xmlui.dlg.message)
xmlui_data = {"answer": C.boolConst(confirmed)}
await self.host.bridge.launchAction(xmlui_id, xmlui_data, profile_key=profile)
xmlui_data = {"answer": C.bool_const(confirmed)}
await self.host.bridge.action_launch(xmlui_id, xmlui_data, profile_key=profile)
if not confirmed and not self.args.multiple:
self.disp(_("Session refused for {from_jid}").format(from_jid=from_jid))
self.host.quitFromSignal(0)
self.host.quit_from_signal(0)
async def start(self):
self.bare_jids = [jid.JID(jid_).bare for jid_ in self.args.jids]
@ -489,16 +489,16 @@ class Get(base.CommandBase):
help=_("URI of the file to retrieve or JSON of the whole attachment")
)
async def onProgressStarted(self, metadata):
async def on_progress_started(self, metadata):
self.disp(_("File download started"), 2)
async def onProgressFinished(self, metadata):
async def on_progress_finished(self, metadata):
self.disp(_("File downloaded successfully"), 2)
async def onProgressError(self, error_msg):
async def on_progress_error(self, error_msg):
self.disp(_("Error while downloading file: {}").format(error_msg), error=True)
async def gotId(self, data):
async def got_id(self, data):
"""Called when a progress id has been received"""
try:
await self.set_progress_id(data["progress"])
@ -532,12 +532,12 @@ class Get(base.CommandBase):
message = _("File {path} already exists! Do you want to overwrite?").format(
path=dest_file
)
await self.host.confirmOrQuit(message, _("file download cancelled"))
await self.host.confirm_or_quit(message, _("file download cancelled"))
options = {}
try:
download_data_s = await self.host.bridge.fileDownload(
download_data_s = await self.host.bridge.file_download(
data_format.serialise(attachment),
str(dest_file),
data_format.serialise(options),
@ -548,7 +548,7 @@ class Get(base.CommandBase):
self.disp(f"error while trying to download a file: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
await self.gotId(download_data)
await self.got_id(download_data)
class Upload(base.CommandBase):
@ -576,10 +576,10 @@ class Upload(base.CommandBase):
help=_(r"ignore invalide TLS certificate (/!\ Dangerous /!\)"),
)
async def onProgressStarted(self, metadata):
async def on_progress_started(self, metadata):
self.disp(_("File upload started"), 2)
async def onProgressFinished(self, metadata):
async def on_progress_finished(self, metadata):
self.disp(_("File uploaded successfully"), 2)
try:
url = metadata["url"]
@ -590,10 +590,10 @@ class Upload(base.CommandBase):
# XXX: url is displayed alone on a line to make parsing easier
self.disp(url)
async def onProgressError(self, error_msg):
async def on_progress_error(self, error_msg):
self.disp(_("Error while uploading file: {}").format(error_msg), error=True)
async def gotId(self, data, file_):
async def got_id(self, data, file_):
"""Called when a progress id has been received
@param pid(unicode): progress id
@ -633,7 +633,7 @@ class Upload(base.CommandBase):
path = os.path.abspath(file_)
try:
upload_data = await self.host.bridge.fileUpload(
upload_data = await self.host.bridge.file_upload(
path,
"",
self.full_dest_jid,
@ -644,7 +644,7 @@ class Upload(base.CommandBase):
self.disp(f"error while trying to upload a file: {e}", error=True)
self.host.quit(C.EXIT_BRIDGE_ERRBACK)
else:
await self.gotId(upload_data, file_)
await self.got_id(upload_data, file_)
class ShareAffiliationsSet(base.CommandBase):
@ -687,7 +687,7 @@ class ShareAffiliationsSet(base.CommandBase):
async def start(self):
affiliations = dict(self.args.affiliations)
try:
affiliations = await self.host.bridge.FISAffiliationsSet(
affiliations = await self.host.bridge.fis_affiliations_set(
self.args.jid,
self.args.namespace,
self.args.path,
@ -730,7 +730,7 @@ class ShareAffiliationsGet(base.CommandBase):
async def start(self):
try:
affiliations = await self.host.bridge.FISAffiliationsGet(
affiliations = await self.host.bridge.fis_affiliations_get(
self.args.jid,
self.args.namespace,
self.args.path,
@ -793,7 +793,7 @@ class ShareConfigurationSet(base.CommandBase):
async def start(self):
configuration = dict(self.args.fields)
try:
configuration = await self.host.bridge.FISConfigurationSet(
configuration = await self.host.bridge.fis_configuration_set(
self.args.jid,
self.args.namespace,
self.args.path,
@ -836,7 +836,7 @@ class ShareConfigurationGet(base.CommandBase):
async def start(self):
try:
configuration = await self.host.bridge.FISConfigurationGet(
configuration = await self.host.bridge.fis_configuration_get(
self.args.jid,
self.args.namespace,
self.args.path,
@ -900,7 +900,7 @@ class ShareList(base.CommandBase):
def _size_filter(self, size, row):
if not size:
return ""
return A.color(A.BOLD, utils.getHumanSize(size))
return A.color(A.BOLD, utils.get_human_size(size))
def default_output(self, files_data):
"""display files a way similar to ls"""
@ -914,7 +914,7 @@ class ShareList(base.CommandBase):
show_header = True
keys = ("name", "type", "size", "file_hash")
headers = ("name", "type", "size", "hash")
table = common.Table.fromListDict(
table = common.Table.from_list_dict(
self.host,
files_data,
keys=keys,
@ -926,7 +926,7 @@ class ShareList(base.CommandBase):
async def start(self):
try:
files_data = await self.host.bridge.FISList(
files_data = await self.host.bridge.fis_list(
self.args.jid,
self.args.path,
{},
@ -987,7 +987,7 @@ class SharePath(base.CommandBase):
else:
access = {}
try:
name = await self.host.bridge.FISSharePath(
name = await self.host.bridge.fis_share_path(
self.args.name,
self.path,
json.dumps(access, ensure_ascii=False),
@ -1059,7 +1059,7 @@ class ShareInvite(base.CommandBase):
else:
extra["thumb_url"] = self.args.thumbnail
try:
await self.host.bridge.FISInvite(
await self.host.bridge.fis_invite(
self.args.jid,
self.args.service,
self.args.type,