File: trasher.py

package info (click to toggle)
trash-cli 0.24.5.26-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,972 kB
  • sloc: python: 9,789; sh: 121; makefile: 11
file content (68 lines) | stat: -rw-r--r-- 2,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from trashcli.put.my_logger import LoggerBackend

from trashcli.put.context import Context
from trashcli.put.context import SingleTrasher
from trashcli.put.core.trash_result import TrashResult
from trashcli.put.core.trashee import should_skipped_by_specs
from trashcli.put.file_trasher import FileTrasher
from trashcli.put.fs.fs import Fs
from trashcli.put.my_logger import MyLogger
from trashcli.put.reporting.trash_put_reporter import TrashPutReporter
from trashcli.put.user import User
from trashcli.put.user import user_replied_no


class Trasher(SingleTrasher):
    def __init__(self,
                 file_trasher,  # type: FileTrasher
                 user,  # type: User
                 fs,  # type: Fs
                 backend,  # type: LoggerBackend
                 ):
        self.file_trasher = file_trasher
        self.user = user
        self.logger = MyLogger(backend)
        self.reporter = TrashPutReporter(fs)
        self.fs = fs

    def trash_single(self,
                     path,  # type: str
                     context,  # type: Context
                     ):
        """
        Trash a file in the appropriate trash directory.
        If the file belong to the same volume of the trash home directory it
        will be trashed in the home trash directory.
        Otherwise it will be trashed in one of the relevant volume trash
        directories.

        Each volume can have two trash directories, they are
            - $volume/.Trash/$uid
            - $volume/.Trash-$uid

        Firstly the software attempt to trash the file in the first directory
        then try to trash in the second trash directory.
        """

        if should_skipped_by_specs(path):
            self.logger.log_put(
                self.reporter.unable_to_trash_dot_entries(path),
                context.log_data)
            return TrashResult.Failure

        if not self.fs.lexists(path):
            if context.mode.can_ignore_not_existent_path():
                return TrashResult.Success
            else:
                self.logger.log_put(
                    self.reporter.unable_to_trash_file_non_existent(path),
                    context.log_data)
                return TrashResult.Failure

        if context.mode.should_we_ask_to_the_user(self.fs.is_accessible(path)):
            reply = self.user.ask_user_about_deleting_file(context.program_name,
                                                           path)
            if reply == user_replied_no:
                return TrashResult.Success

        return self.file_trasher.trash_file(path, context)