@@ -57,7 +57,7 @@ def get_current_platform():
5757 """
5858 board_info_dir = '/sys/class/board-info/'
5959
60- if not os .path .exists (board_info_dir ):
60+ if not os .path .exists (board_info_dir ) or os . environ . get ( "FAKE_SYS" ) :
6161 return 'fake'
6262
6363 for board in os .listdir (board_info_dir ):
@@ -145,7 +145,12 @@ class DeviceNotFound(Exception):
145145class Device (object ):
146146 """The ev3dev device base class"""
147147
148- __slots__ = ['_path' , '_device_index' , 'kwargs' ]
148+ __slots__ = [
149+ '_path' ,
150+ '_device_index' ,
151+ '_attr_cache' ,
152+ 'kwargs' ,
153+ ]
149154
150155 DEVICE_ROOT_PATH = '/sys/class'
151156
@@ -178,6 +183,7 @@ def __init__(self, class_name, name_pattern='*', name_exact=False, **kwargs):
178183
179184 classpath = abspath (Device .DEVICE_ROOT_PATH + '/' + class_name )
180185 self .kwargs = kwargs
186+ self ._attr_cache = {}
181187
182188 def get_index (file ):
183189 match = Device ._DEVICE_INDEX .match (file )
@@ -281,6 +287,15 @@ def get_attr_int(self, attribute, name):
281287 attribute , value = self ._get_attribute (attribute , name )
282288 return attribute , int (value )
283289
290+ def get_cached_attr_int (self , filehandle , keyword ):
291+ value = self ._attr_cache .get (keyword )
292+
293+ if value is None :
294+ (filehandle , value ) = self .get_attr_int (filehandle , keyword )
295+ self ._attr_cache [keyword ] = value
296+
297+ return (filehandle , value )
298+
284299 def set_attr_int (self , attribute , name , value ):
285300 return self ._set_attribute (attribute , name , str (int (value )))
286301
@@ -290,6 +305,15 @@ def set_attr_raw(self, attribute, name, value):
290305 def get_attr_string (self , attribute , name ):
291306 return self ._get_attribute (attribute , name )
292307
308+ def get_cached_attr_string (self , filehandle , keyword ):
309+ value = self ._attr_cache .get (keyword )
310+
311+ if value is None :
312+ (filehandle , value ) = self .get_attr_string (filehandle , keyword )
313+ self ._attr_cache [keyword ] = value
314+
315+ return (filehandle , value )
316+
293317 def set_attr_string (self , attribute , name , value ):
294318 return self ._set_attribute (attribute , name , value )
295319
@@ -300,6 +324,15 @@ def get_attr_set(self, attribute, name):
300324 attribute , value = self .get_attr_line (attribute , name )
301325 return attribute , [v .strip ('[]' ) for v in value .split ()]
302326
327+ def get_cached_attr_set (self , filehandle , keyword ):
328+ value = self ._attr_cache .get (keyword )
329+
330+ if value is None :
331+ (filehandle , value ) = self .get_attr_set (filehandle , keyword )
332+ self ._attr_cache [keyword ] = value
333+
334+ return (filehandle , value )
335+
303336 def get_attr_from_set (self , attribute , name ):
304337 attribute , value = self .get_attr_line (attribute , name )
305338 for a in value .split ():
0 commit comments