Skip to content

Commit 923080b

Browse files
WasabiFandwalton76
authored andcommitted
Remove "connected" attribute (#402)
* Remove "connected" attribute Fixes #327 * Fix things * I'm terrible at both staging and committing everything * Update error name and text * Fix things * Revert GyroBalancer changes * Strip exception context when device isn't connected
1 parent 623ff39 commit 923080b

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed

ev3dev/__init__.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,16 @@ def matches(attribute, pattern):
124124
yield f
125125

126126

127+
class DeviceNotFound(Exception):
128+
pass
129+
127130
# -----------------------------------------------------------------------------
128131
# Define the base class from which all other ev3dev classes are defined.
129132

130133
class Device(object):
131134
"""The ev3dev device base class"""
132135

133-
__slots__ = ['_path', 'connected', '_device_index', 'kwargs']
136+
__slots__ = ['_path', '_device_index', 'kwargs']
134137

135138
DEVICE_ROOT_PATH = '/sys/class'
136139

@@ -158,7 +161,7 @@ def __init__(self, class_name, name_pattern='*', name_exact=False, **kwargs):
158161
d = ev3dev.Device('tacho-motor', address='outA')
159162
s = ev3dev.Device('lego-sensor', driver_name=['lego-ev3-us', 'lego-nxt-us'])
160163
161-
When connected succesfully, the `connected` attribute is set to True.
164+
If there was no valid connected device, an error is thrown.
162165
"""
163166

164167
classpath = abspath(Device.DEVICE_ROOT_PATH + '/' + class_name)
@@ -174,23 +177,24 @@ def get_index(file):
174177
if name_exact:
175178
self._path = classpath + '/' + name_pattern
176179
self._device_index = get_index(name_pattern)
177-
self.connected = True
178180
else:
179181
try:
180182
name = next(list_device_names(classpath, name_pattern, **kwargs))
181183
self._path = classpath + '/' + name
182184
self._device_index = get_index(name)
183-
self.connected = True
184185
except StopIteration:
185186
self._path = None
186187
self._device_index = None
187-
self.connected = False
188+
raise DeviceNotFound("%s is not connected." % self) from None
188189

189190
def __str__(self):
190191
if 'address' in self.kwargs:
191192
return "%s(%s)" % (self.__class__.__name__, self.kwargs.get('address'))
192193
else:
193194
return self.__class__.__name__
195+
196+
def __repr__(self):
197+
return self.__str__()
194198

195199
def _attribute_file_open(self, name):
196200
path = os.path.join(self._path, name)
@@ -209,35 +213,27 @@ def _attribute_file_open(self, name):
209213

210214
def _get_attribute(self, attribute, name):
211215
"""Device attribute getter"""
212-
if self.connected:
216+
if attribute is None:
217+
attribute = self._attribute_file_open( name )
218+
else:
219+
attribute.seek(0)
220+
return attribute, attribute.read().strip().decode()
221+
222+
def _set_attribute(self, attribute, name, value):
223+
"""Device attribute setter"""
224+
try:
213225
if attribute is None:
214226
attribute = self._attribute_file_open( name )
215227
else:
216228
attribute.seek(0)
217-
return attribute, attribute.read().strip().decode()
218-
else:
219-
#log.info("%s: path %s, attribute %s" % (self, self._path, name))
220-
raise Exception("%s is not connected" % self)
221229

222-
def _set_attribute(self, attribute, name, value):
223-
"""Device attribute setter"""
224-
if self.connected:
225-
try:
226-
if attribute is None:
227-
attribute = self._attribute_file_open( name )
228-
else:
229-
attribute.seek(0)
230-
231-
if isinstance(value, str):
232-
value = value.encode()
233-
attribute.write(value)
234-
attribute.flush()
235-
except Exception as ex:
236-
self._raise_friendly_access_error(ex, name)
237-
return attribute
238-
else:
239-
#log.info("%s: path %s, attribute %s" % (self, self._path, name))
240-
raise Exception("%s is not connected" % self)
230+
if isinstance(value, str):
231+
value = value.encode()
232+
attribute.write(value)
233+
attribute.flush()
234+
except Exception as ex:
235+
self._raise_friendly_access_error(ex, name)
236+
return attribute
241237

242238
def _raise_friendly_access_error(self, driver_error, attribute):
243239
if not isinstance(driver_error, OSError):
@@ -256,7 +252,7 @@ def _raise_friendly_access_error(self, driver_error, attribute):
256252
# We will assume that a file-not-found error is the result of a disconnected device
257253
# rather than a library error. If that isn't the case, at a minimum the underlying
258254
# error info will be printed for debugging.
259-
raise Exception("%s is no longer connected" % self) from driver_error
255+
raise DeviceNotFound("%s is no longer connected" % self) from driver_error
260256
raise driver_error
261257

262258
def get_attr_int(self, attribute, name):

tests/api_tests.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,20 @@ def test_device(self):
2121
populate_arena({'medium_motor' : [0, 'outA'], 'infrared_sensor' : [0, 'in1']})
2222

2323
d = ev3.Device('tacho-motor', 'motor*')
24-
self.assertTrue(d.connected)
2524

2625
d = ev3.Device('tacho-motor', 'motor0')
27-
self.assertTrue(d.connected)
2826

2927
d = ev3.Device('tacho-motor', 'motor*', driver_name='lego-ev3-m-motor')
30-
self.assertTrue(d.connected)
3128

3229
d = ev3.Device('tacho-motor', 'motor*', address='outA')
33-
self.assertTrue(d.connected)
3430

35-
d = ev3.Device('tacho-motor', 'motor*', address='outA', driver_name='not-valid')
36-
self.assertTrue(not d.connected)
31+
with self.assertRaises(ev3.DeviceNotFound):
32+
d = ev3.Device('tacho-motor', 'motor*', address='outA', driver_name='not-valid')
3733

3834
d = ev3.Device('lego-sensor', 'sensor*')
39-
self.assertTrue(d.connected)
4035

41-
d = ev3.Device('this-does-not-exist')
42-
self.assertFalse(d.connected)
36+
with self.assertRaises(ev3.DeviceNotFound):
37+
d = ev3.Device('this-does-not-exist')
4338

4439
def test_medium_motor(self):
4540
def dummy(self):
@@ -53,8 +48,6 @@ def dummy(self):
5348

5449
m = MediumMotor()
5550

56-
self.assertTrue(m.connected);
57-
5851
self.assertEqual(m.device_index, 0)
5952

6053
# Check that reading twice works:
@@ -86,8 +79,6 @@ def test_infrared_sensor(self):
8679

8780
s = InfraredSensor()
8881

89-
self.assertTrue(s.connected)
90-
9182
self.assertEqual(s.device_index, 0)
9283
self.assertEqual(s.bin_data_format, 's8')
9384
self.assertEqual(s.bin_data('<b'), (16,))

utils/move_motor.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
else:
3434
raise Exception("%s is invalid, options are A, B, C, D")
3535

36-
if not motor.connected:
37-
log.error("%s is not connected" % motor)
38-
sys.exit(1)
39-
4036
if args.degrees:
4137
log.info("Motor %s, current position %d, move to position %d, max speed %d" %
4238
(args.motor, motor.position, args.degrees, motor.max_speed))

0 commit comments

Comments
 (0)