@@ -109,14 +109,15 @@ def list_mbeds_ext(self):
109
109
110
110
def list_mbeds (
111
111
self , fs_interaction = FSInteraction .BeforeFilter ,
112
- platform_name_filters = [] , unique_names = False ,
112
+ filter_function = None , unique_names = False ,
113
113
read_details_txt = False ):
114
114
""" List details of connected devices
115
115
@return Returns list of structures with detailed info about each mbed
116
116
@param fs_interaction A member of the FSInteraction class that picks the
117
117
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')
120
121
@param unique_names A boolean controlling the presence of the
121
122
'platform_unique_name' member of the output dict
122
123
@param read_details_text A boolean controlling the presense of the
@@ -128,8 +129,6 @@ def list_mbeds(
128
129
candidates = list (self .find_candidates ())
129
130
logger .debug ("Candidates for display %r" , candidates )
130
131
result = []
131
- platform_name_matcher = re .compile ("|" .join ("({})" .format (pf ) for pf
132
- in platform_name_filters ))
133
132
for device in candidates :
134
133
if ((not device ['mount_point' ] or
135
134
not self .mount_point_ready (device ['mount_point' ])) and
@@ -144,7 +143,7 @@ def list_mbeds(
144
143
FSInteraction .BeforeFilter : self ._fs_before_id_check ,
145
144
FSInteraction .AfterFilter : self ._fs_after_id_check ,
146
145
FSInteraction .Never : self ._fs_never
147
- }[fs_interaction ](device , platform_name_matcher )
146
+ }[fs_interaction ](device , filter_function )
148
147
if maybe_device :
149
148
if unique_names :
150
149
name = device ['platform_name' ]
@@ -167,36 +166,38 @@ def list_mbeds(
167
166
168
167
return result
169
168
170
- def _fs_never (self , device , pn ):
169
+ def _fs_never (self , device , filter_function ):
171
170
"""Filter device without touching the file system of the device"""
172
171
device ['target_id' ] = device ['target_id_usb_id' ]
173
172
device ['target_id_mbed_htm' ] = None
174
173
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 ):
178
175
return device
176
+ else :
177
+ return None
179
178
180
- def _fs_before_id_check (self , device , pn ):
179
+ def _fs_before_id_check (self , device , filter_function ):
181
180
"""Filter device after touching the file system of the device.
182
181
Said another way: Touch the file system before filtering
183
182
"""
184
183
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 ):
188
185
return device
186
+ else :
187
+ return None
189
188
190
- def _fs_after_id_check (self , device , pn ):
189
+ def _fs_after_id_check (self , device , filter_function ):
191
190
"""Filter device before touching the file system of the device.
192
191
Said another way: Touch the file system after filtering
193
192
"""
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 ) :
198
197
self ._update_device_from_htm (device )
199
198
return device
199
+ else :
200
+ return None
200
201
201
202
def _update_device_from_htm (self , device ):
202
203
"""Set the 'target_id', 'target_id_mbed_htm', 'platform_name' and
0 commit comments