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

@ -74,8 +74,8 @@ class FileSharingManagement(object):
self._c = host.plugins["XEP-0050"]
self._t = host.plugins["XEP-0264"]
self.files_path = host.get_local_path(None, C.FILES_DIR)
host.bridge.addMethod(
"fileSharingDelete",
host.bridge.add_method(
"file_sharing_delete",
".plugin",
in_sign="ssss",
out_sign="",
@ -83,30 +83,30 @@ class FileSharingManagement(object):
async_=True,
)
def profileConnected(self, client):
self._c.addAdHocCommand(
client, self._onChangeFile, "Change Permissions of File(s)",
def profile_connected(self, client):
self._c.add_ad_hoc_command(
client, self._on_change_file, "Change Permissions of File(s)",
node=NS_FILE_MANAGEMENT_PERM,
allowed_magics=C.ENTITY_ALL,
)
self._c.addAdHocCommand(
client, self._onDeleteFile, "Delete File(s)",
self._c.add_ad_hoc_command(
client, self._on_delete_file, "Delete File(s)",
node=NS_FILE_MANAGEMENT_DELETE,
allowed_magics=C.ENTITY_ALL,
)
self._c.addAdHocCommand(
client, self._onGenThumbnails, "Generate Thumbnails",
self._c.add_ad_hoc_command(
client, self._on_gen_thumbnails, "Generate Thumbnails",
node=NS_FILE_MANAGEMENT_THUMB,
allowed_magics=C.ENTITY_ALL,
)
self._c.addAdHocCommand(
client, self._onQuota, "Get Quota",
self._c.add_ad_hoc_command(
client, self._on_quota, "Get Quota",
node=NS_FILE_MANAGEMENT_QUOTA,
allowed_magics=C.ENTITY_ALL,
)
def _delete(self, service_jid_s, path, namespace, profile):
client = self.host.getClient(profile)
client = self.host.get_client(profile)
service_jid = jid.JID(service_jid_s) if service_jid_s else None
return defer.ensureDeferred(self._c.sequence(
client,
@ -127,7 +127,7 @@ class FileSharingManagement(object):
note = (self._c.NOTE.ERROR, reason)
return payload, status, None, note
def _getRootArgs(self):
def _get_root_args(self):
"""Create the form to select the file to use
@return (tuple): arguments to use in defer.returnValue
@ -149,7 +149,7 @@ class FileSharingManagement(object):
payload = form.toElement()
return payload, status, None, None
async def _getFileData(self, client, session_data, command_form):
async def _get_file_data(self, client, session_data, command_form):
"""Retrieve field requested in root form
"found_file" will also be set in session_data
@ -162,10 +162,10 @@ class FileSharingManagement(object):
path = fields['path'].value.strip()
namespace = fields['namespace'].value or None
except KeyError:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if not path:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
requestor = session_data['requestor']
requestor_bare = requestor.userhostJID()
@ -176,7 +176,7 @@ class FileSharingManagement(object):
# this must be managed
try:
found_files = await self.host.memory.getFiles(
found_files = await self.host.memory.get_files(
client, requestor_bare, path=parent_path, name=basename,
namespace=namespace)
found_file = found_files[0]
@ -194,7 +194,7 @@ class FileSharingManagement(object):
session_data['namespace'] = namespace
return found_file
def _updateReadPermission(self, access, allowed_jids):
def _update_read_permission(self, access, allowed_jids):
if not allowed_jids:
if C.ACCESS_PERM_READ in access:
del access[C.ACCESS_PERM_READ]
@ -208,27 +208,27 @@ class FileSharingManagement(object):
"jids": [j.full() for j in allowed_jids]
}
async def _updateDir(self, client, requestor, namespace, file_data, allowed_jids):
async def _update_dir(self, client, requestor, namespace, file_data, allowed_jids):
"""Recursively update permission of a directory and all subdirectories
@param file_data(dict): metadata of the file
@param allowed_jids(list[jid.JID]): list of entities allowed to read the file
"""
assert file_data['type'] == C.FILE_TYPE_DIRECTORY
files_data = await self.host.memory.getFiles(
files_data = await self.host.memory.get_files(
client, requestor, parent=file_data['id'], namespace=namespace)
for file_data in files_data:
if not file_data['access'].get(C.ACCESS_PERM_READ, {}):
log.debug("setting {perm} read permission for {name}".format(
perm=allowed_jids, name=file_data['name']))
await self.host.memory.fileUpdate(
await self.host.memory.file_update(
file_data['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
partial(self._update_read_permission, allowed_jids=allowed_jids))
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
await self._updateDir(client, requestor, namespace, file_data, 'PUBLIC')
await self._update_dir(client, requestor, namespace, file_data, 'PUBLIC')
async def _onChangeFile(self, client, command_elt, session_data, action, node):
async def _on_change_file(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -241,12 +241,12 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
return self._getRootArgs()
return self._get_root_args()
elif found_file is None:
# file selected, we retrieve it and ask for permissions
try:
found_file = await self._getFileData(client, session_data, command_form)
found_file = await self._get_file_data(client, session_data, command_form)
except WorkflowError as e:
return e.err_args
@ -288,7 +288,7 @@ class FileSharingManagement(object):
try:
read_allowed = command_form.fields['read_allowed']
except KeyError:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if read_allowed.value == 'PUBLIC':
allowed_jids = 'PUBLIC'
@ -301,26 +301,26 @@ class FileSharingManagement(object):
except RuntimeError as e:
log.warning(_("Can't use read_allowed values: {reason}").format(
reason=e))
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if found_file['type'] == C.FILE_TYPE_FILE:
await self.host.memory.fileUpdate(
await self.host.memory.file_update(
found_file['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
partial(self._update_read_permission, allowed_jids=allowed_jids))
else:
try:
recursive = command_form.fields['recursive']
except KeyError:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
await self.host.memory.fileUpdate(
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
await self.host.memory.file_update(
found_file['id'], 'access',
partial(self._updateReadPermission, allowed_jids=allowed_jids))
partial(self._update_read_permission, allowed_jids=allowed_jids))
if recursive:
# we set all file under the directory as public (if they haven't
# already a permission set), so allowed entities of root directory
# can read them.
namespace = session_data['namespace']
await self._updateDir(
await self._update_dir(
client, requestor_bare, namespace, found_file, 'PUBLIC')
# job done, we can end the session
@ -329,7 +329,7 @@ class FileSharingManagement(object):
note = (self._c.NOTE.INFO, _("management session done"))
return (payload, status, None, note)
async def _onDeleteFile(self, client, command_elt, session_data, action, node):
async def _on_delete_file(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -342,12 +342,12 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
return self._getRootArgs()
return self._get_root_args()
elif found_file is None:
# file selected, we need confirmation before actually deleting
try:
found_file = await self._getFileData(client, session_data, command_form)
found_file = await self._get_file_data(client, session_data, command_form)
except WorkflowError as e:
return e.err_args
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
@ -373,31 +373,31 @@ class FileSharingManagement(object):
try:
confirmed = C.bool(command_form.fields['confirm'].value)
except KeyError:
self._c.adHocError(self._c.ERROR.BAD_PAYLOAD)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if not confirmed:
note = None
else:
recursive = found_file['type'] == C.FILE_TYPE_DIRECTORY
await self.host.memory.fileDelete(
await self.host.memory.file_delete(
client, requestor_bare, found_file['id'], recursive)
note = (self._c.NOTE.INFO, _("file deleted"))
status = self._c.STATUS.COMPLETED
payload = None
return (payload, status, None, note)
def _updateThumbs(self, extra, thumbnails):
def _update_thumbs(self, extra, thumbnails):
extra[C.KEY_THUMBNAILS] = thumbnails
async def _genThumbs(self, client, requestor, namespace, file_data):
async def _gen_thumbs(self, client, requestor, namespace, file_data):
"""Recursively generate thumbnails
@param file_data(dict): metadata of the file
"""
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
sub_files_data = await self.host.memory.getFiles(
sub_files_data = await self.host.memory.get_files(
client, requestor, parent=file_data['id'], namespace=namespace)
for sub_file_data in sub_files_data:
await self._genThumbs(client, requestor, namespace, sub_file_data)
await self._gen_thumbs(client, requestor, namespace, sub_file_data)
elif file_data['type'] == C.FILE_TYPE_FILE:
media_type = file_data['media_type']
@ -407,7 +407,7 @@ class FileSharingManagement(object):
for max_thumb_size in self._t.SIZES:
try:
thumb_size, thumb_id = await self._t.generateThumbnail(
thumb_size, thumb_id = await self._t.generate_thumbnail(
file_path,
max_thumb_size,
#  we keep thumbnails for 6 months
@ -419,9 +419,9 @@ class FileSharingManagement(object):
break
thumbnails.append({"id": thumb_id, "size": thumb_size})
await self.host.memory.fileUpdate(
await self.host.memory.file_update(
file_data['id'], 'extra',
partial(self._updateThumbs, thumbnails=thumbnails))
partial(self._update_thumbs, thumbnails=thumbnails))
log.info("thumbnails for [{file_name}] generated"
.format(file_name=file_data['name']))
@ -429,7 +429,7 @@ class FileSharingManagement(object):
else:
log.warning("unmanaged file type: {type_}".format(type_=file_data['type']))
async def _onGenThumbnails(self, client, command_elt, session_data, action, node):
async def _on_gen_thumbnails(self, client, command_elt, session_data, action, node):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
command_form = data_form.Form.fromElement(x_elt)
@ -441,17 +441,17 @@ class FileSharingManagement(object):
if command_form is None or len(command_form.fields) == 0:
# root request
return self._getRootArgs()
return self._get_root_args()
elif found_file is None:
# file selected, we retrieve it and ask for permissions
try:
found_file = await self._getFileData(client, session_data, command_form)
found_file = await self._get_file_data(client, session_data, command_form)
except WorkflowError as e:
return e.err_args
log.info("Generating thumbnails as requested")
await self._genThumbs(client, requestor, found_file['namespace'], found_file)
await self._gen_thumbs(client, requestor, found_file['namespace'], found_file)
# job done, we can end the session
status = self._c.STATUS.COMPLETED
@ -459,11 +459,11 @@ class FileSharingManagement(object):
note = (self._c.NOTE.INFO, _("thumbnails generated"))
return (payload, status, None, note)
async def _onQuota(self, client, command_elt, session_data, action, node):
async def _on_quota(self, client, command_elt, session_data, action, node):
requestor = session_data['requestor']
quota = self.host.plugins["file_sharing"].getQuota(client, requestor)
quota = self.host.plugins["file_sharing"].get_quota(client, requestor)
try:
size_used = await self.host.memory.fileGetUsedSpace(client, requestor)
size_used = await self.host.memory.file_get_used_space(client, requestor)
except exceptions.PermissionError:
raise WorkflowError(self._err(_("forbidden")))
status = self._c.STATUS.COMPLETED
@ -473,10 +473,10 @@ class FileSharingManagement(object):
note = (
self._c.NOTE.INFO,
_("You are currently using {size_used} on {size_quota}").format(
size_used = utils.getHumanSize(size_used),
size_used = utils.get_human_size(size_used),
size_quota = (
_("unlimited quota") if quota is None
else utils.getHumanSize(quota)
else utils.get_human_size(quota)
)
)
)