| 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 |
ftp_error.py - exception classes and wrappers |
|---|
| 34 |
""" |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
import ftplib |
|---|
| 42 |
import sys |
|---|
| 43 |
|
|---|
| 44 |
import ftputil_version |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
class FTPError(Exception): |
|---|
| 48 |
"""General error class.""" |
|---|
| 49 |
|
|---|
| 50 |
def __init__(self, ftp_exception): |
|---|
| 51 |
super(FTPError, self).__init__(ftp_exception) |
|---|
| 52 |
|
|---|
| 53 |
self.strerror = self.message |
|---|
| 54 |
try: |
|---|
| 55 |
self.errno = int(self.strerror[:3]) |
|---|
| 56 |
except (TypeError, IndexError, ValueError): |
|---|
| 57 |
self.errno = None |
|---|
| 58 |
self.filename = None |
|---|
| 59 |
|
|---|
| 60 |
def __str__(self): |
|---|
| 61 |
return "%s\nDebugging info: %s" % \ |
|---|
| 62 |
(self.strerror, ftputil_version.version_info) |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
class InternalError(FTPError): |
|---|
| 67 |
"""Internal error.""" |
|---|
| 68 |
pass |
|---|
| 69 |
|
|---|
| 70 |
class RootDirError(InternalError): |
|---|
| 71 |
"""Raised for generic stat calls on the remote root directory.""" |
|---|
| 72 |
pass |
|---|
| 73 |
|
|---|
| 74 |
class InaccessibleLoginDirError(InternalError): |
|---|
| 75 |
"""May be raised if the login directory isn't accessible.""" |
|---|
| 76 |
pass |
|---|
| 77 |
|
|---|
| 78 |
class TimeShiftError(InternalError): |
|---|
| 79 |
"""Raised for invalid time shift values.""" |
|---|
| 80 |
pass |
|---|
| 81 |
|
|---|
| 82 |
class ParserError(InternalError): |
|---|
| 83 |
"""Raised if a line of a remote directory can't be parsed.""" |
|---|
| 84 |
pass |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
class KeepAliveError(InternalError): |
|---|
| 88 |
"""Raised if the keep-alive feature failed.""" |
|---|
| 89 |
pass |
|---|
| 90 |
|
|---|
| 91 |
class FTPOSError(FTPError, OSError): |
|---|
| 92 |
"""Generic FTP error related to `OSError`.""" |
|---|
| 93 |
pass |
|---|
| 94 |
|
|---|
| 95 |
class TemporaryError(FTPOSError): |
|---|
| 96 |
"""Raised for temporary FTP errors (4xx).""" |
|---|
| 97 |
pass |
|---|
| 98 |
|
|---|
| 99 |
class PermanentError(FTPOSError): |
|---|
| 100 |
"""Raised for permanent FTP errors (5xx).""" |
|---|
| 101 |
pass |
|---|
| 102 |
|
|---|
| 103 |
class CommandNotImplementedError(PermanentError): |
|---|
| 104 |
"""Raised if the server doesn't implement a certain feature (502).""" |
|---|
| 105 |
pass |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
class SyncError(PermanentError): |
|---|
| 109 |
"""Raised for problems specific to syncing directories.""" |
|---|
| 110 |
pass |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
def _try_with_oserror(callee, *args, **kwargs): |
|---|
| 115 |
""" |
|---|
| 116 |
Try the callee with the given arguments and map resulting |
|---|
| 117 |
exceptions from `ftplib.all_errors` to `FTPOSError` and its |
|---|
| 118 |
derived classes. |
|---|
| 119 |
""" |
|---|
| 120 |
try: |
|---|
| 121 |
return callee(*args, **kwargs) |
|---|
| 122 |
except ftplib.error_temp, obj: |
|---|
| 123 |
raise TemporaryError(obj) |
|---|
| 124 |
except ftplib.error_perm, obj: |
|---|
| 125 |
if str(obj).startswith("502"): |
|---|
| 126 |
raise CommandNotImplementedError(obj) |
|---|
| 127 |
else: |
|---|
| 128 |
raise PermanentError(obj) |
|---|
| 129 |
except ftplib.all_errors: |
|---|
| 130 |
ftp_error = sys.exc_info()[1] |
|---|
| 131 |
raise FTPOSError(ftp_error) |
|---|
| 132 |
|
|---|
| 133 |
class FTPIOError(FTPError, IOError): |
|---|
| 134 |
"""Generic FTP error related to `IOError`.""" |
|---|
| 135 |
pass |
|---|
| 136 |
|
|---|
| 137 |
def _try_with_ioerror(callee, *args, **kwargs): |
|---|
| 138 |
""" |
|---|
| 139 |
Try the callee with the given arguments and map resulting |
|---|
| 140 |
exceptions from `ftplib.all_errors` to `FTPIOError`. |
|---|
| 141 |
""" |
|---|
| 142 |
try: |
|---|
| 143 |
return callee(*args, **kwargs) |
|---|
| 144 |
except ftplib.all_errors: |
|---|
| 145 |
ftp_error = sys.exc_info()[1] |
|---|
| 146 |
raise FTPIOError(ftp_error) |
|---|
| 147 |
|
|---|