Fall back on regular deletion when trash folder unavailable

aa0cac1ed3 (commitcomment-69668166)
This commit is contained in:
Damien Elmes 2022-03-28 14:40:31 +10:00
parent 5dab7ed47e
commit 7506d5f06c
3 changed files with 28 additions and 11 deletions

View File

@ -12,6 +12,7 @@ from collections import defaultdict
from concurrent.futures import Future from concurrent.futures import Future
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from pathlib import Path
from typing import IO, Any, Callable, Iterable, Union from typing import IO, Any, Callable, Iterable, Union
from urllib.parse import parse_qs, urlparse from urllib.parse import parse_qs, urlparse
from zipfile import ZipFile from zipfile import ZipFile
@ -20,7 +21,6 @@ import jsonschema
import markdown import markdown
from jsonschema.exceptions import ValidationError from jsonschema.exceptions import ValidationError
from markdown.extensions import md_in_html from markdown.extensions import md_in_html
from send2trash import send2trash
import anki import anki
import anki.utils import anki.utils
@ -42,6 +42,7 @@ from aqt.utils import (
restoreSplitter, restoreSplitter,
saveGeom, saveGeom,
saveSplitter, saveSplitter,
send_to_trash,
showInfo, showInfo,
showWarning, showWarning,
tooltip, tooltip,
@ -452,7 +453,7 @@ class AddonManager:
# true on success # true on success
def deleteAddon(self, module: str) -> bool: def deleteAddon(self, module: str) -> bool:
try: try:
send2trash(self.addonsFolder(module)) send_to_trash(Path(self.addonsFolder(module)))
return True return True
except OSError as e: except OSError as e:
showWarning( showWarning(

View File

@ -9,10 +9,9 @@ import random
import shutil import shutil
import traceback import traceback
from enum import Enum from enum import Enum
from pathlib import Path
from typing import Any from typing import Any
from send2trash import send2trash
import anki.lang import anki.lang
import aqt.forms import aqt.forms
import aqt.sound import aqt.sound
@ -24,7 +23,7 @@ from anki.utils import int_time, is_mac, is_win, point_version
from aqt import appHelpSite from aqt import appHelpSite
from aqt.qt import * from aqt.qt import *
from aqt.theme import Theme from aqt.theme import Theme
from aqt.utils import disable_help_button, showWarning, tr from aqt.utils import disable_help_button, send_to_trash, showWarning, tr
# Profile handling # Profile handling
########################################################################## ##########################################################################
@ -233,16 +232,14 @@ class ProfileManager:
self.db.commit() self.db.commit()
def remove(self, name: str) -> None: def remove(self, name: str) -> None:
p = self.profileFolder() path = self.profileFolder(create=False)
if os.path.exists(p): send_to_trash(Path(path))
send2trash(p)
self.db.execute("delete from profiles where name = ?", name) self.db.execute("delete from profiles where name = ?", name)
self.db.commit() self.db.commit()
def trashCollection(self) -> None: def trashCollection(self) -> None:
p = self.collectionPath() path = self.collectionPath()
if os.path.exists(p): send_to_trash(Path(path))
send2trash(p)
def rename(self, name: str) -> None: def rename(self, name: str) -> None:
oldName = self.name oldName = self.name

View File

@ -4,11 +4,15 @@ from __future__ import annotations
import os import os
import re import re
import shutil
import subprocess import subprocess
import sys import sys
from functools import wraps from functools import wraps
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, Sequence, no_type_check from typing import TYPE_CHECKING, Any, Literal, Sequence, no_type_check
from send2trash import send2trash
import aqt import aqt
from anki._legacy import DeprecatedNamesMixinForModule from anki._legacy import DeprecatedNamesMixinForModule
from anki.collection import Collection, HelpPage from anki.collection import Collection, HelpPage
@ -703,6 +707,21 @@ def current_window() -> QWidget | None:
return None return None
def send_to_trash(path: Path) -> None:
"Place file/folder in recyling bin, or delete permanently on failure."
if not path.exists():
return
try:
send2trash(path)
except Exception as exc:
# Linux users may not have a trash folder set up
print("trash failure:", path, exc)
if path.is_dir:
shutil.rmtree(path)
else:
path.unlink()
# Tooltips # Tooltips
###################################################################### ######################################################################