python: Simplify with str.removeprefix, str.removesuffix.

These are available in Python ≥ 3.9.
https://docs.python.org/3/library/stdtypes.html#str.removeprefix

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2024-09-03 10:42:14 -07:00
committed by Tim Abbott
parent 1ec4539550
commit 91ade25ba3
42 changed files with 79 additions and 92 deletions

View File

@@ -239,7 +239,7 @@ class LocalUploadBackend(ZulipUploadBackend):
def delete_export_tarball(self, export_path: str) -> str | None:
# Get the last element of a list in the form ['user_avatars', '<file_path>']
assert export_path.startswith("/")
file_path = export_path[1:].split("/", 1)[-1]
file_path = export_path.removeprefix("/").split("/", 1)[-1]
if delete_local_file("avatars", file_path):
return export_path
return None

View File

@@ -183,7 +183,7 @@ class S3UploadBackend(ZulipUploadBackend):
assert split_url.path.endswith(f"/{DUMMY_KEY}")
return urlunsplit(
(split_url.scheme, split_url.netloc, split_url.path[: -len(DUMMY_KEY)], "", "")
(split_url.scheme, split_url.netloc, split_url.path.removesuffix(DUMMY_KEY), "", "")
)
@override
@@ -395,7 +395,7 @@ class S3UploadBackend(ZulipUploadBackend):
@override
def get_export_tarball_url(self, realm: Realm, export_path: str) -> str:
# export_path has a leading /
return self.get_public_upload_url(export_path[1:])
return self.get_public_upload_url(export_path.removeprefix("/"))
@override
def upload_export_tarball(
@@ -420,7 +420,7 @@ class S3UploadBackend(ZulipUploadBackend):
@override
def delete_export_tarball(self, export_path: str) -> str | None:
assert export_path.startswith("/")
path_id = export_path[1:]
path_id = export_path.removeprefix("/")
if self.delete_file_from_s3(path_id, self.avatar_bucket):
return export_path
return None