| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
pyudev.device._errors |
| 19 |
===================== |
| 20 |
|
| 21 |
Errors raised by Device methods. |
| 22 |
|
| 23 |
.. moduleauthor:: Sebastian Wiesner <[email protected]> |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
import abc |
| 28 |
|
| 29 |
|
| 30 |
class DeviceError(Exception): |
| 31 |
""" |
| 32 |
Any error raised when messing around w/ or trying to discover devices. |
| 33 |
""" |
| 34 |
|
| 35 |
__metaclass__ = abc.ABCMeta |
| 36 |
|
| 37 |
|
| 38 |
class DeviceNotFoundError(DeviceError): |
| 39 |
""" |
| 40 |
An exception indicating that no :class:`Device` was found. |
| 41 |
|
| 42 |
.. versionchanged:: 0.5 |
| 43 |
Rename from ``NoSuchDeviceError`` to its current name. |
| 44 |
""" |
| 45 |
|
| 46 |
__metaclass__ = abc.ABCMeta |
| 47 |
|
| 48 |
|
| 49 |
class DeviceNotFoundAtPathError(DeviceNotFoundError): |
| 50 |
""" |
| 51 |
A :exc:`DeviceNotFoundError` indicating that no :class:`Device` was |
| 52 |
found at a given path. |
| 53 |
""" |
| 54 |
|
| 55 |
def __init__(self, sys_path): |
| 56 |
DeviceNotFoundError.__init__(self, sys_path) |
| 57 |
|
| 58 |
@property |
| 59 |
def sys_path(self): |
| 60 |
""" |
| 61 |
The path that caused this error as string. |
| 62 |
""" |
| 63 |
return self.args[0] |
| 64 |
|
| 65 |
def __str__(self): |
| 66 |
return f"No device at {self.sys_path!r}" |
| 67 |
|
| 68 |
|
| 69 |
class DeviceNotFoundByFileError(DeviceNotFoundError): |
| 70 |
""" |
| 71 |
A :exc:`DeviceNotFoundError` indicating that no :class:`Device` was |
| 72 |
found from the given filename. |
| 73 |
""" |
| 74 |
|
| 75 |
|
| 76 |
class DeviceNotFoundByInterfaceIndexError(DeviceNotFoundError): |
| 77 |
""" |
| 78 |
A :exc:`DeviceNotFoundError` indicating that no :class:`Device` was found |
| 79 |
from the given interface index. |
| 80 |
""" |
| 81 |
|
| 82 |
|
| 83 |
class DeviceNotFoundByKernelDeviceError(DeviceNotFoundError): |
| 84 |
""" |
| 85 |
A :exc:`DeviceNotFoundError` indicating that no :class:`Device` was found |
| 86 |
from the given kernel device string. |
| 87 |
|
| 88 |
The format of the kernel device string is defined in the |
| 89 |
systemd.journal-fields man pages. |
| 90 |
""" |
| 91 |
|
| 92 |
|
| 93 |
class DeviceNotFoundByNameError(DeviceNotFoundError): |
| 94 |
""" |
| 95 |
A :exc:`DeviceNotFoundError` indicating that no :class:`Device` was |
| 96 |
found with a given name. |
| 97 |
""" |
| 98 |
|
| 99 |
def __init__(self, subsystem, sys_name): |
| 100 |
DeviceNotFoundError.__init__(self, subsystem, sys_name) |
| 101 |
|
| 102 |
@property |
| 103 |
def subsystem(self): |
| 104 |
""" |
| 105 |
The subsystem that caused this error as string. |
| 106 |
""" |
| 107 |
return self.args[0] |
| 108 |
|
| 109 |
@property |
| 110 |
def sys_name(self): |
| 111 |
""" |
| 112 |
The sys name that caused this error as string. |
| 113 |
""" |
| 114 |
return self.args[1] |
| 115 |
|
| 116 |
def __str__(self): |
| 117 |
return f"No device {self.sys_name!r} in {self.subsystem!r}" |
| 118 |
|
| 119 |
|
| 120 |
class DeviceNotFoundByNumberError(DeviceNotFoundError): |
| 121 |
""" |
| 122 |
A :exc:`DeviceNotFoundError` indicating, that no :class:`Device` was found |
| 123 |
for a given device number. |
| 124 |
""" |
| 125 |
|
| 126 |
def __init__(self, typ, number): |
| 127 |
DeviceNotFoundError.__init__(self, typ, number) |
| 128 |
|
| 129 |
@property |
| 130 |
def device_type(self): |
| 131 |
""" |
| 132 |
The device type causing this error as string. Either ``'char'`` or |
| 133 |
``'block'``. |
| 134 |
""" |
| 135 |
return self.args[0] |
| 136 |
|
| 137 |
@property |
| 138 |
def device_number(self): |
| 139 |
""" |
| 140 |
The device number causing this error as integer. |
| 141 |
""" |
| 142 |
return self.args[1] |
| 143 |
|
| 144 |
def __str__(self): |
| 145 |
return f"No {self.device_type} device with number {self.device_number}" |
| 146 |
|
| 147 |
|
| 148 |
class DeviceNotFoundInEnvironmentError(DeviceNotFoundError): |
| 149 |
""" |
| 150 |
A :exc:`DeviceNotFoundError` indicating, that no :class:`Device` could |
| 151 |
be constructed from the process environment. |
| 152 |
""" |
| 153 |
|
| 154 |
def __str__(self): |
| 155 |
return "No device found in environment" |
| 156 |
|
| 157 |
|
| 158 |
class DeviceValueError(DeviceError): |
| 159 |
""" |
| 160 |
Raised when a parameter has an unacceptable value. |
| 161 |
|
| 162 |
May also be raised when the parameter has an unacceptable type. |
| 163 |
""" |
| 164 |
|
| 165 |
_FMT_STR = "value '%s' for parameter %s is unacceptable" |
| 166 |
|
| 167 |
def __init__(self, value, param, msg=None): |
| 168 |
""" |
| 169 |
Initializer. |
| 170 |
|
| 171 |
:param object value: the value |
| 172 |
:param str param: the parameter |
| 173 |
:param str msg: an explanatory message |
| 174 |
""" |
| 175 |
|
| 176 |
self._value = value |
| 177 |
self._param = param |
| 178 |
self._msg = msg |
| 179 |
|
| 180 |
def __str__(self): |
| 181 |
if self._msg: |
| 182 |
fmt_str = self._FMT_STR + ": %s" |
| 183 |
return fmt_str % (self._value, self._param, self._msg) |
| 184 |
return self._FMT_STR % (self._value, self._param) |