4545INPUT_AUTO = ''
4646OUTPUT_AUTO = ''
4747
48- # remove# -----------------------------------------------------------------------------
49- # remove# Attribute reader/writer with cached file access
50- # removeclass FileCache(object):
51- # remove def __init__(self):
52- # remove self._cache = {}
53- # remove
54- # remove def __del__(self):
55- # remove for f in self._cache.values():
56- # remove f.close()
57- # remove
58- # remove def file_handle(self, path):
59- # remove """Manages the file handle cache and opening the files in the correct mode"""
60- # remove
61- # remove if path not in self._cache:
62- # remove mode = stat.S_IMODE(os.stat(path)[stat.ST_MODE])
63- # remove
64- # remove r_ok = mode & stat.S_IRGRP
65- # remove w_ok = mode & stat.S_IWGRP
66- # remove
67- # remove if r_ok and w_ok:
68- # remove mode = 'ab+'
69- # remove elif w_ok:
70- # remove mode = 'ab'
71- # remove else:
72- # remove mode = 'rb'
73- # remove
74- # remove f = open(path, mode, 0)
75- # remove self._cache[path] = f
76- # remove else:
77- # remove f = self._cache[path]
78- # remove
79- # remove return f
80- # remove
81- # remove def read(self, path):
82- # remove f = self.file_handle(path)
83- # remove
84- # remove f.seek(0)
85- # remove return f.read().decode().strip()
86- # remove
87- # remove def write(self, path, value):
88- # remove f = self.file_handle(path)
89- # remove
90- # remove f.seek(0)
91- # remove f.write(value.encode())
92- # remove
93- # remove
9448# -----------------------------------------------------------------------------
9549def list_device_names (class_path , name_pattern , ** kwargs ):
9650 """
@@ -162,7 +116,6 @@ def __init__(self, class_name, name_pattern='*', name_exact=False, **kwargs):
162116 """
163117
164118 classpath = abspath (Device .DEVICE_ROOT_PATH + '/' + class_name )
165- # remove self._attribute_cache = FileCache()
166119 self .kwargs = kwargs
167120
168121 def get_index (file ):
@@ -193,17 +146,6 @@ def __str__(self):
193146 else :
194147 return self .__class__ .__name__
195148
196- # def _matches(self, attribute, pattern):
197- # """Test if attribute value matches pattern (that is, if pattern is a
198- # substring of attribute value). If pattern is a list, then a match with
199- # any one entry is enough.
200- # """
201- # value = self._get_attribute(attribute)
202- # if isinstance(pattern, list):
203- # return any([value.find(pat) >= 0 for pat in pattern])
204- # else:
205- # return value.find(pattern) >= 0
206-
207149 def _attribute_file_open ( self , name ):
208150 path = self ._path + '/' + name
209151 mode = stat .S_IMODE (os .stat (path )[stat .ST_MODE ])
@@ -230,7 +172,7 @@ def _get_attribute(self, attribute, name):
230172 raise Exception ('Device is not connected' )
231173
232174 def _set_attribute (self , attribute , name , value ):
233- """Device attribute getter """
175+ """Device attribute setter """
234176 if self .connected :
235177 if None == attribute :
236178 attribute = self ._attribute_file_open ( name )
@@ -314,9 +256,9 @@ class Motor(Device):
314256 SYSTEM_DEVICE_NAME_CONVENTION = '*'
315257
316258 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
259+
317260 if address is not None :
318261 kwargs ['address' ] = address
319-
320262 super (Motor , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
321263
322264 self ._address = None
@@ -875,8 +817,6 @@ class LargeMotor(Motor):
875817 SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
876818
877819 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
878- if address is not None :
879- kwargs ['address' ] = address
880820
881821 super (LargeMotor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
882822
@@ -894,8 +834,6 @@ class MediumMotor(Motor):
894834 SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
895835
896836 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
897- if address is not None :
898- kwargs ['address' ] = address
899837
900838 super (MediumMotor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
901839
@@ -913,8 +851,6 @@ class NxtMotor(Motor):
913851 SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
914852
915853 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
916- if address is not None :
917- kwargs ['address' ] = address
918854
919855 super (NxtMotor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
920856
@@ -932,8 +868,6 @@ class FirgelliL1250Motor(Motor):
932868 SYSTEM_DEVICE_NAME_CONVENTION = 'linear*'
933869
934870 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
935- if address is not None :
936- kwargs ['address' ] = address
937871
938872 super (FirgelliL1250Motor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
939873
@@ -951,8 +885,6 @@ class FirgelliL12100Motor(Motor):
951885 SYSTEM_DEVICE_NAME_CONVENTION = 'linear*'
952886
953887 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
954- if address is not None :
955- kwargs ['address' ] = address
956888
957889 super (FirgelliL12100Motor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
958890
@@ -972,9 +904,9 @@ class DcMotor(Device):
972904 SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
973905
974906 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
907+
975908 if address is not None :
976909 kwargs ['address' ] = address
977-
978910 super (DcMotor , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
979911
980912 self ._address = None
@@ -1229,9 +1161,9 @@ class ServoMotor(Device):
12291161 SYSTEM_DEVICE_NAME_CONVENTION = 'motor*'
12301162
12311163 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1164+
12321165 if address is not None :
12331166 kwargs ['address' ] = address
1234-
12351167 super (ServoMotor , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
12361168
12371169 self ._address = None
@@ -1446,9 +1378,9 @@ class Sensor(Device):
14461378 SYSTEM_DEVICE_NAME_CONVENTION = 'sensor*'
14471379
14481380 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1381+
14491382 if address is not None :
14501383 kwargs ['address' ] = address
1451-
14521384 super (Sensor , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
14531385
14541386 self ._address = None
@@ -1646,8 +1578,6 @@ class I2cSensor(Sensor):
16461578 SYSTEM_DEVICE_NAME_CONVENTION = 'sensor*'
16471579
16481580 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1649- if address is not None :
1650- kwargs ['address' ] = address
16511581
16521582 super (I2cSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
16531583
@@ -1695,9 +1625,7 @@ class TouchSensor(Sensor):
16951625 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
16961626
16971627 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1698- if address is not None :
1699- kwargs ['address' ] = address
1700- super (TouchSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1628+ super (TouchSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-ev3-touch' , 'lego-nxt-touch' ], ** kwargs )
17011629
17021630
17031631 # Button state
@@ -1723,9 +1651,7 @@ class ColorSensor(Sensor):
17231651 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
17241652
17251653 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1726- if address is not None :
1727- kwargs ['address' ] = address
1728- super (ColorSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1654+ super (ColorSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-ev3-color' ], ** kwargs )
17291655
17301656
17311657 # Reflected light. Red LED on.
@@ -1805,9 +1731,7 @@ class UltrasonicSensor(Sensor):
18051731 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
18061732
18071733 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1808- if address is not None :
1809- kwargs ['address' ] = address
1810- super (UltrasonicSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1734+ super (UltrasonicSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-ev3-us' , 'lego-nxt-us' ], ** kwargs )
18111735
18121736
18131737 # Continuous measurement in centimeters.
@@ -1861,9 +1785,7 @@ class GyroSensor(Sensor):
18611785 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
18621786
18631787 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1864- if address is not None :
1865- kwargs ['address' ] = address
1866- super (GyroSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1788+ super (GyroSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-ev3-gyro' ], ** kwargs )
18671789
18681790
18691791 # Angle
@@ -1908,9 +1830,7 @@ class InfraredSensor(Sensor):
19081830 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
19091831
19101832 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1911- if address is not None :
1912- kwargs ['address' ] = address
1913- super (InfraredSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1833+ super (InfraredSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-ev3-ir' ], ** kwargs )
19141834
19151835
19161836 # Proximity
@@ -1948,9 +1868,7 @@ class SoundSensor(Sensor):
19481868 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
19491869
19501870 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1951- if address is not None :
1952- kwargs ['address' ] = address
1953- super (SoundSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1871+ super (SoundSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-nxt-sound' ], ** kwargs )
19541872
19551873
19561874 # Sound pressure level. Flat weighting
@@ -1987,9 +1905,7 @@ class LightSensor(Sensor):
19871905 SYSTEM_DEVICE_NAME_CONVENTION = Sensor .SYSTEM_DEVICE_NAME_CONVENTION
19881906
19891907 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1990- if address is not None :
1991- kwargs ['address' ] = address
1992- super (LightSensor , self ).__init__ (address , name_pattern , name_exact , ** kwargs )
1908+ super (LightSensor , self ).__init__ (address , name_pattern , name_exact , driver_name = ['lego-nxt-light' ], ** kwargs )
19931909
19941910
19951911 # Reflected light. LED on
@@ -2031,9 +1947,9 @@ class Led(Device):
20311947 SYSTEM_DEVICE_NAME_CONVENTION = '*'
20321948
20331949 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
1950+
20341951 if address is not None :
20351952 kwargs ['address' ] = address
2036-
20371953 super (Led , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
20381954
20391955 self ._max_brightness = None
@@ -2346,9 +2262,9 @@ class PowerSupply(Device):
23462262 SYSTEM_DEVICE_NAME_CONVENTION = '*'
23472263
23482264 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
2265+
23492266 if address is not None :
23502267 kwargs ['address' ] = address
2351-
23522268 super (PowerSupply , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
23532269
23542270 self ._measured_current = None
@@ -2459,9 +2375,9 @@ class LegoPort(Device):
24592375 SYSTEM_DEVICE_NAME_CONVENTION = '*'
24602376
24612377 def __init__ (self , address = None , name_pattern = SYSTEM_DEVICE_NAME_CONVENTION , name_exact = False , ** kwargs ):
2378+
24622379 if address is not None :
24632380 kwargs ['address' ] = address
2464-
24652381 super (LegoPort , self ).__init__ (self .SYSTEM_CLASS_NAME , name_pattern , name_exact , ** kwargs )
24662382
24672383 self ._address = None
0 commit comments