@@ -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
130133class 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 ):
0 commit comments