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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
"""Exception classes thrown by filesystem operations.
Errors relating to the underlying filesystem are translated in
to one of the following exceptions.
All Exception classes are derived from `~fs.errors.FSError`
which may be used as a catch-all filesystem exception.
"""
from __future__ import print_function, unicode_literals
import typing
import functools
if typing.TYPE_CHECKING:
from typing import Optional, Text
__all__ = [
"BulkCopyFailed",
"CreateFailed",
"DestinationExists",
"DirectoryExists",
"DirectoryExpected",
"DirectoryNotEmpty",
"FileExists",
"FileExpected",
"FilesystemClosed",
"FSError",
"IllegalBackReference",
"InsufficientStorage",
"InvalidCharsInPath",
"InvalidPath",
"MissingInfoNamespace",
"NoSysPath",
"NoURL",
"OperationFailed",
"OperationTimeout",
"PathError",
"PermissionDenied",
"RemoteConnectionError",
"RemoveRootError",
"ResourceError",
"ResourceInvalid",
"ResourceLocked",
"ResourceNotFound",
"ResourceReadOnly",
"Unsupported",
"UnsupportedHash",
]
class MissingInfoNamespace(AttributeError):
"""An expected namespace is missing."""
def __init__(self, namespace): # noqa: D107
# type: (Text) -> None
self.namespace = namespace
msg = "namespace '{}' is required for this attribute"
super(MissingInfoNamespace, self).__init__(msg.format(namespace))
def __reduce__(self):
return type(self), (self.namespace,)
class FSError(Exception):
"""Base exception for the `fs` module."""
default_message = "Unspecified error"
def __init__(self, msg=None): # noqa: D107
# type: (Optional[Text]) -> None
self._msg = msg or self.default_message
super(FSError, self).__init__()
def __str__(self):
# type: () -> Text
"""Return the error message."""
msg = self._msg.format(**self.__dict__)
return msg
def __repr__(self):
# type: () -> Text
msg = self._msg.format(**self.__dict__)
return "{}({!r})".format(self.__class__.__name__, msg)
class FilesystemClosed(FSError):
"""Attempt to use a closed filesystem."""
default_message = "attempt to use closed filesystem"
class BulkCopyFailed(FSError):
"""A copy operation failed in worker threads."""
default_message = "One or more copy operations failed (see errors attribute)"
def __init__(self, errors): # noqa: D107
self.errors = errors
super(BulkCopyFailed, self).__init__()
class CreateFailed(FSError):
"""Filesystem could not be created."""
default_message = "unable to create filesystem, {details}"
def __init__(self, msg=None, exc=None): # noqa: D107
# type: (Optional[Text], Optional[Exception]) -> None
self._msg = msg or self.default_message
self.details = "" if exc is None else str(exc)
self.exc = exc
@classmethod
def catch_all(cls, func):
@functools.wraps(func)
def new_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except cls:
raise
except Exception as e:
raise cls(exc=e)
return new_func # type: ignore
def __reduce__(self):
return type(self), (self._msg, self.exc)
class PathError(FSError):
"""Base exception for errors to do with a path string."""
default_message = "path '{path}' is invalid"
def __init__(self, path, msg=None, exc=None): # noqa: D107
# type: (Text, Optional[Text], Optional[Exception]) -> None
self.path = path
self.exc = exc
super(PathError, self).__init__(msg=msg)
def __reduce__(self):
return type(self), (self.path, self._msg, self.exc)
class NoSysPath(PathError):
"""The filesystem does not provide *sys paths* to the resource."""
default_message = "path '{path}' does not map to the local filesystem"
class NoURL(PathError):
"""The filesystem does not provide an URL for the resource."""
default_message = "path '{path}' has no '{purpose}' URL"
def __init__(self, path, purpose, msg=None): # noqa: D107
# type: (Text, Text, Optional[Text]) -> None
self.purpose = purpose
super(NoURL, self).__init__(path, msg=msg)
def __reduce__(self):
return type(self), (self.path, self.purpose, self._msg)
class InvalidPath(PathError):
"""Path can't be mapped on to the underlaying filesystem."""
default_message = "path '{path}' is invalid on this filesystem "
class InvalidCharsInPath(InvalidPath):
"""Path contains characters that are invalid on this filesystem."""
default_message = "path '{path}' contains invalid characters"
class OperationFailed(FSError):
"""A specific operation failed."""
default_message = "operation failed, {details}"
def __init__(
self,
path=None, # type: Optional[Text]
exc=None, # type: Optional[Exception]
msg=None, # type: Optional[Text]
): # noqa: D107
# type: (...) -> None
self.path = path
self.exc = exc
self.details = "" if exc is None else str(exc)
self.errno = getattr(exc, "errno", None)
super(OperationFailed, self).__init__(msg=msg)
def __reduce__(self):
return type(self), (self.path, self.exc, self._msg)
class Unsupported(OperationFailed):
"""Operation not supported by the filesystem."""
default_message = "not supported"
class RemoteConnectionError(OperationFailed):
"""Operations encountered remote connection trouble."""
default_message = "remote connection error"
class InsufficientStorage(OperationFailed):
"""Storage is insufficient for requested operation."""
default_message = "insufficient storage space"
class PermissionDenied(OperationFailed):
"""Not enough permissions."""
default_message = "permission denied"
class OperationTimeout(OperationFailed):
"""Filesystem took too long."""
default_message = "operation timed out"
class RemoveRootError(OperationFailed):
"""Attempt to remove the root directory."""
default_message = "root directory may not be removed"
class ResourceError(FSError):
"""Base exception class for error associated with a specific resource."""
default_message = "failed on path {path}"
def __init__(self, path, exc=None, msg=None): # noqa: D107
# type: (Text, Optional[Exception], Optional[Text]) -> None
self.path = path
self.exc = exc
super(ResourceError, self).__init__(msg=msg)
def __reduce__(self):
return type(self), (self.path, self.exc, self._msg)
class ResourceNotFound(ResourceError):
"""Required resource not found."""
default_message = "resource '{path}' not found"
class ResourceInvalid(ResourceError):
"""Resource has the wrong type."""
default_message = "resource '{path}' is invalid for this operation"
class FileExists(ResourceError):
"""File already exists."""
default_message = "resource '{path}' exists"
class FileExpected(ResourceInvalid):
"""Operation only works on files."""
default_message = "path '{path}' should be a file"
class DirectoryExpected(ResourceInvalid):
"""Operation only works on directories."""
default_message = "path '{path}' should be a directory"
class DestinationExists(ResourceError):
"""Target destination already exists."""
default_message = "destination '{path}' exists"
class DirectoryExists(ResourceError):
"""Directory already exists."""
default_message = "directory '{path}' exists"
class DirectoryNotEmpty(ResourceError):
"""Attempt to remove a non-empty directory."""
default_message = "directory '{path}' is not empty"
class ResourceLocked(ResourceError):
"""Attempt to use a locked resource."""
default_message = "resource '{path}' is locked"
class ResourceReadOnly(ResourceError):
"""Attempting to modify a read-only resource."""
default_message = "resource '{path}' is read only"
class IllegalBackReference(ValueError):
"""Too many backrefs exist in a path.
This error will occur if the back references in a path would be
outside of the root. For example, ``"/foo/../../"``, contains two back
references which would reference a directory above the root.
Note:
This exception is a subclass of `ValueError` as it is not
strictly speaking an issue with a filesystem or resource.
"""
def __init__(self, path): # noqa: D107
# type: (Text) -> None
self.path = path
msg = ("path '{path}' contains back-references outside of filesystem").format(
path=path
)
super(IllegalBackReference, self).__init__(msg)
def __reduce__(self):
return type(self), (self.path,)
class UnsupportedHash(ValueError):
"""The requested hash algorithm is not supported.
This exception will be thrown if a hash algorithm is requested that is
not supported by hashlib.
"""
|