Debian Codegraph @

gallery_dl/exception.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2015-2026 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Exception classes used by gallery-dl
10
11 Class Hierarchy:
12
13 Exception
14 └── GalleryDLException
15 ├── ExtractionError
16 │ ├── HttpError
17 │ │ └── ChallengeError
18 │ ├── AuthorizationError
19 │ │ └── AuthRequired
20 │ ├── AuthenticationError
21 │ └── NotFoundError
22 ├── InputError
23 │ ├── FormatError
24 │ │ ├── FilenameFormatError
25 │ │ └── DirectoryFormatError
26 │ ├── FilterError
27 │ ├── InputFileError
28 │ └── NoExtractorError
29 └── ControlException
30 ├── StopExtraction
31 ├── AbortExtraction
32 ├── TerminateExtraction
33 ├── RestartExtraction
34 └── StopDownload
35 """
36
37
38 class GalleryDLException(Exception):
39 """Base class for GalleryDL exceptions"""
40 default = None
41 msgfmt = None
42 code = 1
43
44 def __init__(self, message=None, fmt=True):
45 if not message:
46 message = self.default
47 elif isinstance(message, Exception):
48 message = f"{message.__class__.__name__}: {message}"
49 if fmt and self.msgfmt is not None:
50 message = self.msgfmt.replace("{}", message)
51 self.message = message
52 Exception.__init__(self, message)
53
54
55 ###############################################################################
56 # Extractor Errors ############################################################
57
58 class ExtractionError(GalleryDLException):
59 """Base class for exceptions during information extraction"""
60 code = 4
61
62
63 class HttpError(ExtractionError):
64 """HTTP request during data extraction failed"""
65 default = "HTTP request failed"
66
67 def __init__(self, message="", response=None):
68 self.response = response
69 if response is None:
70 self.status = 0
71 else:
72 self.status = response.status_code
73 if not message:
74 message = (f"'{response.status_code} {response.reason}' "
75 f"for '{response.url}'")
76 ExtractionError.__init__(self, message)
77
78
79 class ChallengeError(HttpError):
80 code = 8
81
82 def __init__(self, challenge, response):
83 message = (
84 f"{challenge} ({response.status_code} {response.reason}) "
85 f"for '{response.url}'")
86 HttpError.__init__(self, message, response)
87
88
89 class AuthenticationError(ExtractionError):
90 """Invalid or missing login credentials"""
91 default = "Invalid login credentials"
92 code = 16
93
94
95 class AuthorizationError(ExtractionError):
96 """Insufficient privileges to access a resource"""
97 default = "Insufficient privileges to access this resource"
98 code = 16
99
100
101 class AuthRequired(AuthorizationError):
102 default = "Account credentials required"
103
104 def __init__(self, auth=None, resource="resource", message=None):
105 if auth:
106 if not isinstance(auth, str):
107 auth = " or ".join(auth)
108
109 if resource:
110 if " " not in resource:
111 resource = f"this {resource}"
112 resource = f" to access {resource}"
113 else:
114 resource = ""
115
116 message = f" ('{message}')" if message else ""
117 message = f"{auth} needed{resource}{message}"
118 AuthorizationError.__init__(self, message)
119
120
121 class NotFoundError(ExtractionError):
122 """Requested resource (gallery/image) could not be found"""
123 msgfmt = "Requested {} could not be found"
124 default = "resource (gallery/image)"
125
126
127 ###############################################################################
128 # User Input ##################################################################
129
130 class InputError(GalleryDLException):
131 """Error caused by user input and config options"""
132 code = 32
133
134
135 class FormatError(InputError):
136 """Error while building output paths"""
137
138
139 class FilenameFormatError(FormatError):
140 """Error while building output filenames"""
141 msgfmt = "Applying filename format string failed ({})"
142
143
144 class DirectoryFormatError(FormatError):
145 """Error while building output directory paths"""
146 msgfmt = "Applying directory format string failed ({})"
147
148
149 class FilterError(InputError):
150 """Error while evaluating a filter expression"""
151 msgfmt = "Evaluating filter expression failed ({})"
152
153
154 class InputFileError(InputError):
155 """Error when parsing an input file"""
156
157
158 class NoExtractorError(InputError):
159 """No extractor can handle the given URL"""
160
161
162 ###############################################################################
163 # Control Flow ################################################################
164
165 class ControlException(GalleryDLException):
166 code = 0
167
168
169 class StopExtraction(ControlException):
170 """Stop data extraction"""
171
172 def __init__(self, target=None):
173 ControlException.__init__(self)
174
175 if target is None:
176 self.target = None
177 self.depth = 1
178 elif isinstance(target, int):
179 self.target = None
180 self.depth = target
181 elif target.isdecimal():
182 self.target = None
183 self.depth = int(target)
184 else:
185 self.target = target
186 self.depth = 128
187
188
189 class AbortExtraction(ExtractionError, ControlException):
190 """Abort data extraction due to an error"""
191
192
193 class TerminateExtraction(ControlException):
194 """Terminate data extraction"""
195
196
197 class RestartExtraction(ControlException):
198 """Restart data extraction"""
199
200
201 class StopDownload(ControlException):
202 """Cancel a file download"""