|
49 | 49 | import shlex |
50 | 50 | import stat |
51 | 51 | import time |
| 52 | +import errno |
52 | 53 | from os.path import abspath |
53 | 54 | from struct import pack, unpack |
54 | 55 | from subprocess import Popen, check_output, PIPE |
@@ -204,12 +205,31 @@ def _set_attribute(self, attribute, name, value): |
204 | 205 | attribute = self._attribute_file_open( name ) |
205 | 206 | else: |
206 | 207 | attribute.seek(0) |
207 | | - attribute.write(value.encode()) |
208 | | - attribute.flush() |
| 208 | + |
| 209 | + try: |
| 210 | + attribute.write(value.encode()) |
| 211 | + attribute.flush() |
| 212 | + except Exception as ex: |
| 213 | + self._raise_friendly_access_error(ex, name) |
209 | 214 | return attribute |
210 | 215 | else: |
211 | 216 | raise Exception('Device is not connected') |
212 | 217 |
|
| 218 | + def _raise_friendly_access_error(self, driver_error, attribute): |
| 219 | + if not isinstance(driver_error, OSError): |
| 220 | + raise driver_error |
| 221 | + |
| 222 | + if driver_error.errno == errno.EINVAL: |
| 223 | + if attribute == "speed_sp": |
| 224 | + try: |
| 225 | + max_speed = self.max_speed |
| 226 | + except (AttributeError, Exception): |
| 227 | + raise ValueError("The given speed value was out of range") from driver_error |
| 228 | + else: |
| 229 | + raise ValueError("The given speed value was out of range. Max speed: +/-" + str(max_speed)) from driver_error |
| 230 | + raise ValueError("One or more arguments were out of range or invalid") from driver_error |
| 231 | + raise driver_error |
| 232 | + |
213 | 233 | def get_attr_int(self, attribute, name): |
214 | 234 | attribute, value = self._get_attribute(attribute, name) |
215 | 235 | return attribute, int(value) |
|
0 commit comments