Skip to content

Commit ffbcec8

Browse files
Merge pull request #280 from bridadan/replace_name_filter_with_function
Replace name filter with function
2 parents 0aaa009 + b88e1f6 commit ffbcec8

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

mbed_lstools/lstools_base.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ def list_mbeds_ext(self):
109109

110110
def list_mbeds(
111111
self, fs_interaction=FSInteraction.BeforeFilter,
112-
platform_name_filters=[], unique_names=False,
112+
filter_function=None, unique_names=False,
113113
read_details_txt=False):
114114
""" List details of connected devices
115115
@return Returns list of structures with detailed info about each mbed
116116
@param fs_interaction A member of the FSInteraction class that picks the
117117
trade of between quality of service and speed
118-
@param platform_name_filters A series of regular expressions to filter
119-
filter targets by
118+
@param filter_function Function that is passed each mbed candidate,
119+
should return True if it should be included in the result
120+
Ex. mbeds = list_mbeds(filter_function=lambda m: m['platform_name'] == 'K64F')
120121
@param unique_names A boolean controlling the presence of the
121122
'platform_unique_name' member of the output dict
122123
@param read_details_text A boolean controlling the presense of the
@@ -128,8 +129,6 @@ def list_mbeds(
128129
candidates = list(self.find_candidates())
129130
logger.debug("Candidates for display %r", candidates)
130131
result = []
131-
platform_name_matcher = re.compile("|".join("({})".format(pf) for pf
132-
in platform_name_filters))
133132
for device in candidates:
134133
if ((not device['mount_point'] or
135134
not self.mount_point_ready(device['mount_point'])) and
@@ -144,7 +143,7 @@ def list_mbeds(
144143
FSInteraction.BeforeFilter: self._fs_before_id_check,
145144
FSInteraction.AfterFilter: self._fs_after_id_check,
146145
FSInteraction.Never: self._fs_never
147-
}[fs_interaction](device, platform_name_matcher)
146+
}[fs_interaction](device, filter_function)
148147
if maybe_device:
149148
if unique_names:
150149
name = device['platform_name']
@@ -167,36 +166,38 @@ def list_mbeds(
167166

168167
return result
169168

170-
def _fs_never(self, device, pn):
169+
def _fs_never(self, device, filter_function):
171170
"""Filter device without touching the file system of the device"""
172171
device['target_id'] = device['target_id_usb_id']
173172
device['target_id_mbed_htm'] = None
174173
device['platform_name'] = self.plat_db.get(device['target_id'][0:4])
175-
if device['platform_name'] and not pn.match(device['platform_name']):
176-
return None
177-
else:
174+
if not filter_function or filter_function(device):
178175
return device
176+
else:
177+
return None
179178

180-
def _fs_before_id_check(self, device, pn):
179+
def _fs_before_id_check(self, device, filter_function):
181180
"""Filter device after touching the file system of the device.
182181
Said another way: Touch the file system before filtering
183182
"""
184183
self._update_device_from_htm(device)
185-
if device['platform_name'] and not pn.match(device['platform_name']):
186-
return None
187-
else:
184+
if not filter_function or filter_function(device):
188185
return device
186+
else:
187+
return None
189188

190-
def _fs_after_id_check(self, device, pn):
189+
def _fs_after_id_check(self, device, filter_function):
191190
"""Filter device before touching the file system of the device.
192191
Said another way: Touch the file system after filtering
193192
"""
194-
plat_name = self.plat_db.get(device['target_id_usb_id'][0:4])
195-
if plat_name and not pn.match(plat_name):
196-
return None
197-
else:
193+
device['target_id'] = device['target_id_usb_id']
194+
device['target_id_mbed_htm'] = None
195+
device['platform_name'] = self.plat_db.get(device['target_id'][0:4])
196+
if not filter_function or filter_function(device):
198197
self._update_device_from_htm(device)
199198
return device
199+
else:
200+
return None
200201

201202
def _update_device_from_htm(self, device):
202203
"""Set the 'target_id', 'target_id_mbed_htm', 'platform_name' and

test/mbedls_toolsbase.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,19 @@ def test_fs_never(self):
156156
'serial_port': 'invalid_serial_port'
157157
}
158158
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._update_device_from_htm") as _up_htm:
159-
filter = re.compile("")
160-
ret = self.base._fs_never(device, filter)
159+
filter = None
160+
ret = self.base._fs_never(deepcopy(device), filter)
161161
self.assertIsNotNone(ret)
162162
self.assertEqual(ret['target_id'], ret['target_id_usb_id'])
163163
_up_htm.assert_not_called()
164164

165-
filter_out = re.compile("NOT-K64F")
166-
ret = self.base._fs_never(device, filter_out)
165+
filter_in = lambda m: m['platform_name'] == 'K64F'
166+
ret = self.base._fs_never(deepcopy(device), filter_in)
167+
self.assertIsNotNone(ret)
168+
_up_htm.assert_not_called()
169+
170+
filter_out = lambda m: m['platform_name'] != 'K64F'
171+
ret = self.base._fs_never(deepcopy(device), filter_out)
167172
self.assertIsNone(ret)
168173
_up_htm.assert_not_called()
169174

@@ -176,16 +181,24 @@ def test_fs_after(self):
176181
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm:
177182
new_device_id = "00017531642046"
178183
_read_htm.return_value = (new_device_id, {})
179-
filter = re.compile("")
180-
ret = self.base._fs_after_id_check(device, filter)
184+
filter = None
185+
ret = self.base._fs_after_id_check(deepcopy(device), filter)
181186
self.assertIsNotNone(ret)
182187
self.assertEqual(ret['target_id'], new_device_id)
183188
_read_htm.assert_called_with(device['mount_point'])
184189

185190
_read_htm.reset_mock()
186191

187-
filter_out = re.compile("NOT-K64F")
188-
ret = self.base._fs_after_id_check(device, filter_out)
192+
filter_in = lambda m: m['target_id'] == device['target_id_usb_id']
193+
ret = self.base._fs_after_id_check(deepcopy(device), filter_in)
194+
self.assertIsNotNone(ret)
195+
self.assertEqual(ret['target_id'], new_device_id)
196+
_read_htm.assert_called_with(device['mount_point'])
197+
198+
_read_htm.reset_mock()
199+
200+
filter_out = lambda m: m['target_id'] == new_device_id
201+
ret = self.base._fs_after_id_check(deepcopy(device), filter_out)
189202
self.assertIsNone(ret)
190203
_read_htm.assert_not_called()
191204

@@ -198,16 +211,24 @@ def test_fs_before(self):
198211
with patch("mbed_lstools.lstools_base.MbedLsToolsBase._read_htm_ids") as _read_htm:
199212
new_device_id = u'00017575430420'
200213
_read_htm.return_value = (new_device_id, {})
201-
filter = re.compile(u'')
202-
ret = self.base._fs_before_id_check(device, filter)
214+
filter = None
215+
ret = self.base._fs_before_id_check(deepcopy(device), filter)
216+
self.assertIsNotNone(ret)
217+
self.assertEqual(ret['target_id'], new_device_id)
218+
_read_htm.assert_called_with(device['mount_point'])
219+
220+
_read_htm.reset_mock()
221+
222+
filter_in = lambda m: m['target_id'] == '00017575430420'
223+
ret = self.base._fs_before_id_check(deepcopy(device), filter_in)
203224
self.assertIsNotNone(ret)
204225
self.assertEqual(ret['target_id'], new_device_id)
205226
_read_htm.assert_called_with(device['mount_point'])
206227

207228
_read_htm.reset_mock()
208229

209-
filter_out = re.compile("NOT-LPC2368")
210-
ret = self.base._fs_before_id_check(device, filter_out)
230+
filter_out = lambda m: m['target_id'] == '024075309420ABCE'
231+
ret = self.base._fs_before_id_check(deepcopy(device), filter_out)
211232
self.assertIsNone(ret)
212233
_read_htm.assert_called_with(device['mount_point'])
213234

0 commit comments

Comments
 (0)