1
|
|
|
import os |
2
|
|
|
import errno |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class FileActionsException(Exception): |
6
|
|
|
''' |
7
|
|
|
Base class for file-actions exceptions |
8
|
|
|
''' |
9
|
|
|
code = None |
10
|
|
|
template = 'Unhandled error.' |
11
|
|
|
|
12
|
|
|
def __init__(self, message=None, path=None): |
13
|
|
|
self.path = path |
14
|
|
|
message = self.template.format(self) if message is None else message |
15
|
|
|
super(FileActionsException, self).__init__(message) |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class InvalidDirnameError(FileActionsException): |
19
|
|
|
''' |
20
|
|
|
Exception raised when a new directory name is invalid. |
21
|
|
|
|
22
|
|
|
:property name: name which raised this Exception |
23
|
|
|
''' |
24
|
|
|
code = 'directory-invalid-name' |
25
|
|
|
template = 'Clipboard item {0.name!r} is not valid.' |
26
|
|
|
|
27
|
|
|
def __init__(self, message=None, path=None, name=None): |
28
|
|
|
self.name = name |
29
|
|
|
super(InvalidDirnameError, self).__init__(message, path) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class DirectoryCreationError(FileActionsException): |
33
|
|
|
''' |
34
|
|
|
Exception raised when a new directory creation fails. |
35
|
|
|
|
36
|
|
|
:property name: name which raised this Exception |
37
|
|
|
''' |
38
|
|
|
code = 'directory-mkdir-error' |
39
|
|
|
template = 'Clipboard item {0.name!r} is not valid.' |
40
|
|
|
|
41
|
|
|
def __init__(self, message=None, path=None, name=None): |
42
|
|
|
self.name = name |
43
|
|
|
super(DirectoryCreationError, self).__init__(message, path) |
44
|
|
|
|
45
|
|
|
@property |
46
|
|
|
def message(self): |
47
|
|
|
return self.args[0] |
48
|
|
|
|
49
|
|
|
@classmethod |
50
|
|
|
def from_exception(cls, exception, *args, **kwargs): |
51
|
|
|
message = None |
52
|
|
|
if isinstance(exception, OSError): |
53
|
|
|
message = '%s (%s)' % ( |
54
|
|
|
os.strerror(exception.errno), |
55
|
|
|
errno.errorcode[exception.errno] |
56
|
|
|
) |
57
|
|
|
return cls(message, *args, **kwargs) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class ClipboardException(FileActionsException): |
61
|
|
|
''' |
62
|
|
|
Base class for clipboard exceptions. |
63
|
|
|
|
64
|
|
|
:property path: item path which raised this Exception |
65
|
|
|
:property clipboard: :class Clipboard: instance |
66
|
|
|
''' |
67
|
|
|
code = 'clipboard-invalid' |
68
|
|
|
template = 'Clipboard is invalid.' |
69
|
|
|
|
70
|
|
|
def __init__(self, message=None, path=None, clipboard=None): |
71
|
|
|
self.clipboard = clipboard |
72
|
|
|
super(ClipboardException, self).__init__(message, path) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class ItemIssue(tuple): |
76
|
|
|
''' |
77
|
|
|
Item/error issue |
78
|
|
|
''' |
79
|
|
|
@property |
80
|
|
|
def item(self): |
81
|
|
|
return self[0] |
82
|
|
|
|
83
|
|
|
@property |
84
|
|
|
def error(self): |
85
|
|
|
return self[1] |
86
|
|
|
|
87
|
|
|
@property |
88
|
|
|
def message(self): |
89
|
|
|
if isinstance(self.error, OSError): |
90
|
|
|
return '%s (%s)' % ( |
91
|
|
|
os.strerror(self.error.errno), |
92
|
|
|
errno.errorcode[self.error.errno] |
93
|
|
|
) |
94
|
|
|
|
95
|
|
|
# ensure full path is never returned |
96
|
|
|
text = str(self.error) |
97
|
|
|
text = text.replace(self.item.path, self.item.name) |
98
|
|
|
return text |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
class InvalidClipboardItemsError(ClipboardException): |
102
|
|
|
''' |
103
|
|
|
Exception raised when a clipboard item is not valid. |
104
|
|
|
|
105
|
|
|
:property path: item path which raised this Exception |
106
|
|
|
''' |
107
|
|
|
pair_class = ItemIssue |
108
|
|
|
code = 'clipboard-invalid-items' |
109
|
|
|
template = 'Clipboard has invalid items.' |
110
|
|
|
|
111
|
|
|
def __init__(self, message=None, path=None, clipboard=None, issues=()): |
112
|
|
|
self.issues = list(map(self.pair_class, issues)) |
113
|
|
|
supa = super(InvalidClipboardItemsError, self) |
114
|
|
|
supa.__init__(message, path, clipboard) |
115
|
|
|
|
116
|
|
|
def append(self, item, error): |
117
|
|
|
self.issues.append(self.pair_class((item, error))) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class InvalidClipboardModeError(ClipboardException): |
121
|
|
|
''' |
122
|
|
|
Exception raised when a clipboard mode is not valid. |
123
|
|
|
|
124
|
|
|
:property mode: mode which raised this Exception |
125
|
|
|
''' |
126
|
|
|
code = 'clipboard-invalid-mode' |
127
|
|
|
template = 'Clipboard mode {0.path!r} is not valid.' |
128
|
|
|
|
129
|
|
|
def __init__(self, message=None, path=None, clipboard=None, mode=None): |
130
|
|
|
self.mode = mode |
131
|
|
|
supa = super(InvalidClipboardModeError, self) |
132
|
|
|
supa.__init__(message, path, clipboard) |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
class InvalidClipboardSizeError(ClipboardException): |
136
|
|
|
''' |
137
|
|
|
Exception raised when a clipboard size exceeds cookie limit. |
138
|
|
|
|
139
|
|
|
:property max_cookies: maximum allowed size |
140
|
|
|
''' |
141
|
|
|
code = 'clipboard-invalid-size' |
142
|
|
|
template = 'Clipboard has too many items.' |
143
|
|
|
|
144
|
|
|
def __init__(self, message=None, path=None, clipboard=None, max_cookies=0): |
145
|
|
|
self.max_cookies = max_cookies |
146
|
|
|
supa = super(InvalidClipboardSizeError, self) |
147
|
|
|
supa.__init__(message, path, clipboard) |
148
|
|
|
|