Skip to content

Commit b3e345f

Browse files
committed
Replacing platform_name_filter with filter function
1 parent 29e9179 commit b3e345f

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

mbed_lstools/lstools_base.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ 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
120120
@param unique_names A boolean controlling the presence of the
121121
'platform_unique_name' member of the output dict
122122
@param read_details_text A boolean controlling the presense of the
@@ -128,8 +128,6 @@ def list_mbeds(
128128
candidates = list(self.find_candidates())
129129
logger.debug("Candidates for display %r", candidates)
130130
result = []
131-
platform_name_matcher = re.compile("|".join("({})".format(pf) for pf
132-
in platform_name_filters))
133131
for device in candidates:
134132
if ((not device['mount_point'] or
135133
not self.mount_point_ready(device['mount_point'])) and
@@ -144,7 +142,7 @@ def list_mbeds(
144142
FSInteraction.BeforeFilter: self._fs_before_id_check,
145143
FSInteraction.AfterFilter: self._fs_after_id_check,
146144
FSInteraction.Never: self._fs_never
147-
}[fs_interaction](device, platform_name_matcher)
145+
}[fs_interaction](device, filter_function)
148146
if maybe_device:
149147
if unique_names:
150148
name = device['platform_name']
@@ -167,36 +165,38 @@ def list_mbeds(
167165

168166
return result
169167

170-
def _fs_never(self, device, pn):
168+
def _fs_never(self, device, filter_function):
171169
"""Filter device without touching the file system of the device"""
172170
device['target_id'] = device['target_id_usb_id']
173171
device['target_id_mbed_htm'] = None
174172
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:
173+
if not filter_function or filter_function(device):
178174
return device
175+
else:
176+
return None
179177

180-
def _fs_before_id_check(self, device, pn):
178+
def _fs_before_id_check(self, device, filter_function):
181179
"""Filter device after touching the file system of the device.
182180
Said another way: Touch the file system before filtering
183181
"""
184182
self._update_device_from_htm(device)
185-
if device['platform_name'] and not pn.match(device['platform_name']):
186-
return None
187-
else:
183+
if not filter_function or filter_function(device):
188184
return device
185+
else:
186+
return None
189187

190-
def _fs_after_id_check(self, device, pn):
188+
def _fs_after_id_check(self, device, filter_function):
191189
"""Filter device before touching the file system of the device.
192190
Said another way: Touch the file system after filtering
193191
"""
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:
192+
device['target_id'] = device['target_id_usb_id']
193+
device['target_id_mbed_htm'] = None
194+
device['platform_name'] = self.plat_db.get(device['target_id'][0:4])
195+
if not filter_function or filter_function(device):
198196
self._update_device_from_htm(device)
199197
return device
198+
else:
199+
return None
200200

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

0 commit comments

Comments
 (0)