Skip to content

Commit b47cb13

Browse files
committed
utils: add function to move a list of files to trash or delete thme as fallback
1 parent 1439b72 commit b47cb13

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#! python3 # noqa: E265
2+
3+
"""
4+
QDT autocleaner.
5+
6+
Author: Julien Moura (https://github.com/guts)
7+
"""
8+
9+
10+
# #############################################################################
11+
# ########## Libraries #############
12+
# ##################################
13+
14+
# Standard library
15+
import logging
16+
from pathlib import Path
17+
18+
# 3rd party library
19+
from send2trash import TrashPermissionError, send2trash
20+
21+
# #############################################################################
22+
# ########## Globals ###############
23+
# ##################################
24+
25+
# logs
26+
logger = logging.getLogger(__name__)
27+
28+
29+
# #############################################################################
30+
# ########## Functions #############
31+
# ##################################
32+
33+
34+
def move_files_to_trash_or_delete(
35+
files_to_trash: list[Path] | Path,
36+
attempt: int = 1,
37+
) -> None:
38+
"""Move files to the trash or directly delete them if it's not possible.
39+
40+
Args:
41+
files_to_trash (list[Path] | Path): list of file paths to move to the trash
42+
attempt (int, optional): attempt (int): attempt count. If attempt < 2, it
43+
tries a single batch operation. If attempt == 2, it works file per file.
44+
Defaults to 1.
45+
"""
46+
# make sure it's a list
47+
if isinstance(files_to_trash, Path):
48+
files_to_trash = [
49+
files_to_trash,
50+
]
51+
52+
# first try a batch
53+
if attempt < 2:
54+
try:
55+
send2trash(paths=files_to_trash)
56+
logger.info(f"{len(files_to_trash)} files have been moved to the trash.")
57+
except Exception as err:
58+
logger.error(
59+
f"Moving {len(files_to_trash)} files to the trash in a single batch "
60+
f"operation failed. Let's try it file per file. Trace: {err}"
61+
)
62+
move_files_to_trash_or_delete(files_to_trash=files_to_trash, attempt=2)
63+
else:
64+
logger.debug(
65+
f"Moving (or deleting) {len(files_to_trash)} files to trash: " "attempt 2"
66+
)
67+
for file_to_trash in files_to_trash:
68+
try:
69+
send2trash(paths=file_to_trash)
70+
logger.info(f"{file_to_trash} has been moved to the trash.")
71+
except TrashPermissionError as err:
72+
logger.warning(
73+
f"Unable to move {file_to_trash} to the trash. "
74+
f"Trace: {err}. Let's try to delete it directly."
75+
)
76+
try:
77+
file_to_trash.unlink(missing_ok=True)
78+
logger.info(f"Deleting directly {file_to_trash} succeeded.")
79+
except Exception as err:
80+
logger.error(
81+
f"An error occurred trying to delete {file_to_trash}. "
82+
f"Trace: {err}"
83+
)
84+
except Exception as err:
85+
logger.error(
86+
f"An error occurred trying to move {file_to_trash} to trash. "
87+
f"Trace: {err}"
88+
)

0 commit comments

Comments
 (0)