core: fix and renamed getLocalPath -> get_local_path:

- renaming is following global methods renaming to use snake_case
- for reasons lost in the mists of time, a `profile` argument was used while checking if
  `client` is set is enough, and `client.profile` was escaped but actually not used in the
  path.
This commit is contained in:
Goffi 2022-10-10 15:23:59 +02:00
commit f160577a65
4 changed files with 23 additions and 25 deletions

View file

@ -23,6 +23,8 @@ import hashlib
import copy
from pathlib import Path
from typing import Optional, List, Tuple, Dict
from wokkel.data_form import Option
import sat
from sat.core.i18n import _, D_, languageSwitch
from sat.core import patches
@ -877,38 +879,34 @@ class SAT(service.Service):
# local dirs
def getLocalPath(
def get_local_path(
self,
client,
client: Optional[xmpp.SatXMPPEntity],
dir_name: str,
*extra_path,
**kwargs
*extra_path: str,
component: bool = False,
) -> Path:
"""Retrieve path for local data
if path doesn't exist, it will be created
@param client(SatXMPPClient, None): client instance
used when profile is set, can be None if profile is False
@param dir_name(unicode): name of the main path directory
@param component(bool): if True, path will be prefixed with C.COMPONENTS_DIR
@param profile(bool): if True, path will be suffixed by profile name
@param client: client instance
if not none, client.profile will be used as last path element
@param dir_name: name of the main path directory
@param *extra_path: extra path element(s) to use
@param component: if True, path will be prefixed with C.COMPONENTS_DIR
@return: path
"""
# FIXME: component and profile are parsed with **kwargs because of python 2
# limitations. Once moved to python 3, this can be fixed
component = kwargs.pop("component", False)
profile = kwargs.pop("profile", True)
assert not kwargs
path_elts = [self.memory.getConfig("", "local_dir")]
local_dir = self.memory.getConfig("", "local_dir")
if not local_dir:
raise exceptions.InternalError("local_dir must be set")
path_elts = []
if component:
path_elts.append(C.COMPONENTS_DIR)
path_elts.append(regex.pathEscape(dir_name))
if extra_path:
path_elts.extend([regex.pathEscape(p) for p in extra_path])
if profile:
regex.pathEscape(client.profile)
if client is not None:
path_elts.append(regex.pathEscape(client.profile))
local_path = Path(*path_elts)
local_path.mkdir(0o700, parents=True, exist_ok=True)
return local_path

View file

@ -1828,7 +1828,7 @@ class Memory:
file_data = files_data[0]
if file_data["type"] != C.FILE_TYPE_DIRECTORY and recursive:
raise ValueError("recursive can only be set for directories")
files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False)
files_path = self.host.get_local_path(None, C.FILES_DIR)
await self._deleteFile(client, peer_jid, recursive, files_path, file_data)
## Cache ##

View file

@ -322,8 +322,8 @@ class FileSharingSite(server.Site):
def __init__(self, file_sharing):
self.file_sharing = file_sharing
self.file_tmp_dir = file_sharing.host.getLocalPath(
None, C.FILES_TMP_DIR, TMP_BUFFER_DIR, component=True, profile=False
self.file_tmp_dir = file_sharing.host.get_local_path(
None, C.FILES_TMP_DIR, TMP_BUFFER_DIR, component=True
)
for old_file in self.file_tmp_dir.iterdir():
log.debug(f"purging old buffer file at {old_file}")
@ -369,7 +369,7 @@ class FileSharing:
self.host.trigger.add(
"XEP-0329_parseResult_directory",
self._getDirectoryMetadataElts)
self.files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False)
self.files_path = self.host.get_local_path(None, C.FILES_DIR)
self.http_port = int(self.host.memory.getConfig(
'component file-sharing', 'http_upload_port', 8888))
connection_type = self.host.memory.getConfig(
@ -532,8 +532,8 @@ class FileSharing:
file_size=utils.getHumanSize(file_data['size'])
)
)
file_tmp_dir = self.host.getLocalPath(
None, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False
file_tmp_dir = self.host.get_local_path(
None, C.FILES_TMP_DIR, peer_jid.userhost(), component=True
)
file_tmp_path = file_data['file_path'] = files_utils.get_unique_name(
file_tmp_dir/filename)

View file

@ -73,7 +73,7 @@ class FileSharingManagement(object):
self.host = host
self._c = host.plugins["XEP-0050"]
self._t = host.plugins["XEP-0264"]
self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
self.files_path = host.get_local_path(None, C.FILES_DIR)
host.bridge.addMethod(
"fileSharingDelete",
".plugin",