Skip to content

Commit 1a035a0

Browse files
committed
Consolidating calls to os.listdir
1 parent 944901c commit 1a035a0

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

mbed_lstools/lstools_base.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,28 +210,31 @@ def _update_device_from_fs(self, device, read_details_txt):
210210
device['device_type'] = 'unknown'
211211
return
212212

213-
device['device_type'] = self._detect_device_type(device['mount_point'])
213+
directory_entries = os.listdir(device['mount_point'])
214+
device['device_type'] = self._detect_device_type(directory_entries)
214215
device['target_id'] = device['target_id_usb_id']
215216

216217
{
217218
'daplink': self._update_device_details_daplink,
218219
'jlink': self._update_device_details_jlink
219-
}[device['device_type']](device, read_details_txt)
220+
}[device['device_type']](device, read_details_txt, directory_entries)
220221

221-
def _detect_device_type(self, mount_point):
222+
223+
def _detect_device_type(self, directory_entries):
222224
""" Returns a string of the device type
225+
@param directory_entries List of directories and files on the device
223226
@return 'daplink' or 'jlink'
224227
"""
225228

226-
files = [f.lower() for f in os.listdir(mount_point)]
227-
return 'jlink' if 'segger.html' in files else 'daplink'
229+
return 'jlink' if 'segger.html' in [e.lower() for e in directory_entries] else 'daplink'
228230

229231

230-
def _update_device_details_daplink(self, device, read_details_txt):
232+
def _update_device_details_daplink(self, device, read_details_txt, _):
231233
""" Updates the daplink-specific device information based on files from its 'mount_point'
232234
@param device Dictionary containing device information
233235
@param read_details_txt A boolean controlling the presense of the
234236
output dict attributes read from other files present on the 'mount_point'
237+
@param directory_entries List of directories and files on the device
235238
"""
236239
self._update_device_from_htm(device)
237240
if read_details_txt:
@@ -247,12 +250,12 @@ def _update_device_details_daplink(self, device, read_details_txt):
247250
else:
248251
device['platform_name'] = None
249252

250-
def _update_device_details_jlink(self, device, _):
253+
def _update_device_details_jlink(self, device, _, directory_entries):
251254
""" Updates the jlink-specific device information based on files from its 'mount_point'
252255
@param device Dictionary containing device information
256+
@param directory_entries List of directories and files on the device
253257
"""
254-
files = os.listdir(device['mount_point'])
255-
lower_case_map = {f.lower(): f for f in files}
258+
lower_case_map = {e.lower(): e for e in directory_entries}
256259

257260
if 'board.html' in lower_case_map:
258261
board_file_key = 'board.html'

0 commit comments

Comments
 (0)