Skip to content

Commit ee0beae

Browse files
committed
WIP
1 parent 2daee2a commit ee0beae

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

mbed_lstools/lstools_base.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ def find_candidates(self):
9898

9999
@deprecated("Functionality has been moved into 'list_mbeds'. "
100100
"Please use list_mbeds with 'unique_names=True' and "
101-
"'read_details_txt=True'")
101+
"'include_extra_info=True'")
102102
def list_mbeds_ext(self):
103103
"""! Function adds extra information for each mbed device
104104
@return Returns list of mbed devices plus extended data like 'platform_name_unique'
105105
@details Get information about mbeds with extended parameters/info included
106106
"""
107107

108-
return self.list_mbeds(unique_names=True, read_details_txt=True)
108+
return self.list_mbeds(unique_names=True, include_extra_info=True)
109109

110110
def list_mbeds(
111111
self, fs_interaction=FSInteraction.BeforeFilter,
112112
filter_function=None, unique_names=False,
113-
read_details_txt=False):
113+
include_extra_info=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
@@ -120,8 +120,8 @@ def list_mbeds(
120120
Ex. mbeds = list_mbeds(filter_function=lambda m: m['platform_name'] == 'K64F')
121121
@param unique_names A boolean controlling the presence of the
122122
'platform_unique_name' member of the output dict
123-
@param read_details_text A boolean controlling the presense of the
124-
output dict attributes read from "DETAILS.TXT"
123+
@param include_extra_info A boolean controlling the presense of the
124+
output dict attributes read from other files present on the 'mount_point'
125125
@details Function returns list of dictionaries with mbed attributes 'mount_point', TargetID name etc.
126126
Function returns mbed list with platform names if possible
127127
"""
@@ -143,14 +143,15 @@ def list_mbeds(
143143
FSInteraction.BeforeFilter: self._fs_before_id_check,
144144
FSInteraction.AfterFilter: self._fs_after_id_check,
145145
FSInteraction.Never: self._fs_never
146-
}[fs_interaction](device, filter_function, read_details_txt)
146+
}[fs_interaction](device, filter_function, include_extra_info)
147147
if maybe_device:
148148
if unique_names:
149149
name = device['platform_name']
150150
platform_count.setdefault(name, -1)
151151
platform_count[name] += 1
152152
device['platform_name_unique'] = (
153153
"%s[%d]" % (name, platform_count[name]))
154+
154155
try:
155156
device.update(self.retarget_data[device['target_id']])
156157
logger.debug("retargeting %s with %r",
@@ -162,7 +163,7 @@ def list_mbeds(
162163

163164
return result
164165

165-
def _fs_never(self, device, filter_function, read_details_txt):
166+
def _fs_never(self, device, filter_function, include_extra_info):
166167
"""Filter device without touching the file system of the device"""
167168
device['target_id'] = device['target_id_usb_id']
168169
device['target_id_mbed_htm'] = None
@@ -172,41 +173,71 @@ def _fs_never(self, device, filter_function, read_details_txt):
172173
else:
173174
return None
174175

175-
def _fs_before_id_check(self, device, filter_function, read_details_txt):
176+
def _fs_before_id_check(self, device, filter_function, include_extra_info):
176177
"""Filter device after touching the file system of the device.
177178
Said another way: Touch the file system before filtering
178179
"""
179-
self._update_device_from_htm(device)
180-
181-
if read_details_txt:
182-
details_txt = self._details_txt(device['mount_point']) or {}
183-
device.update({"daplink_%s" % f.lower().replace(' ', '_'): v
184-
for f, v in details_txt.items()})
185-
180+
self._update_device_from_fs(device, include_extra_info)
186181
if not filter_function or filter_function(device):
187182
return device
188183
else:
189184
return None
190185

191-
def _fs_after_id_check(self, device, filter_function, read_details_txt):
186+
def _fs_after_id_check(self, device, filter_function, include_extra_info):
187+
192188
"""Filter device before touching the file system of the device.
193189
Said another way: Touch the file system after filtering
194190
"""
195191
device['target_id'] = device['target_id_usb_id']
196192
device['target_id_mbed_htm'] = None
197193
device['platform_name'] = self.plat_db.get(device['target_id'][0:4])
198194
if not filter_function or filter_function(device):
199-
self._update_device_from_htm(device)
200-
201-
if read_details_txt:
202-
details_txt = self._details_txt(device['mount_point']) or {}
203-
device.update({"daplink_%s" % f.lower().replace(' ', '_'): v
204-
for f, v in details_txt.items()})
205-
195+
self._update_device_from_fs(device, include_extra_info)
206196
return device
207197
else:
208198
return None
209199

200+
def _update_device_from_fs(self, device, include_extra_info):
201+
if not device.get('mount_point', None):
202+
device['device_type'] = 'unknown'
203+
return
204+
205+
device['device_type'] = self._detect_device_type(device['mount_point'])
206+
207+
# details.txt check happens in here now
208+
self._update_device_details(device, include_extra_info)
209+
210+
device['platform_name'] = self.plat_db.get(device['target_id'][0:4],
211+
device_type=device['device_type'])
212+
213+
def _detect_device_type(self, device):
214+
""" Returns a string of the device type
215+
@return 'daplink' or 'jlink'
216+
"""
217+
raise NotImplementedError
218+
219+
def _update_device_details(self, device, include_extra_info):
220+
""" Updates device dict with information from the filesystem
221+
@param device Device dictionary to update. Should contain a valid
222+
'mount_point' value
223+
@param include_extra_info Controls how much information (and by extension,
224+
the performance) is returned from the filesystem
225+
"""
226+
{
227+
'daplink': self._update_device_details_daplink,
228+
'jlink': self._update_device_details_jlink
229+
}[device['device_type']](device, include_extra_info)
230+
231+
def _update_device_details_daplink(self, device, include_extra_info):
232+
self._update_device_from_htm(device)
233+
if include_extra_info:
234+
details_txt = self._details_txt(device['mount_point']) or {}
235+
device.update({"daplink_%s" % f.lower().replace(' ', '_'): v
236+
for f, v in details_txt.items()})
237+
238+
def _update_device_details_jlink(self, device, include_extra_info):
239+
raise NotImplementedError
240+
210241
def _update_device_from_htm(self, device):
211242
"""Set the 'target_id', 'target_id_mbed_htm', 'platform_name' and
212243
'daplink_*' attributes by reading from mbed.htm on the device
@@ -372,7 +403,7 @@ def get_string(self, border=False, header=True, padding_width=1, sortby='platfor
372403
"""
373404
from prettytable import PrettyTable
374405
result = ''
375-
mbeds = self.list_mbeds(unique_names=True, read_details_txt=True)
406+
mbeds = self.list_mbeds(unique_names=True, include_extra_info=True)
376407
if mbeds:
377408
""" ['platform_name', 'mount_point', 'serial_port', 'target_id'] - columns generated from USB auto-detection
378409
['platform_name_unique', ...] - columns generated outside detection subsystem (OS dependent detection)

mbed_lstools/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def print_version(mbeds, args):
9494
print(get_version())
9595

9696
def print_mbeds(mbeds, args, simple):
97-
devices = mbeds.list_mbeds(unique_names=True, read_details_txt=True)
97+
devices = mbeds.list_mbeds(unique_names=True, include_extra_info=True)
9898
if devices:
9999
from prettytable import PrettyTable
100100
columns = ['platform_name', 'platform_name_unique', 'mount_point',
@@ -134,13 +134,13 @@ def list_platforms(mbeds, args):
134134

135135
def mbeds_as_json(mbeds, args):
136136
print(json.dumps(mbeds.list_mbeds(unique_names=True,
137-
read_details_txt=True),
137+
include_extra_info=True),
138138
indent=4, sort_keys=True))
139139

140140
def json_by_target_id(mbeds, args):
141141
print(json.dumps({m['target_id']: m for m
142142
in mbeds.list_mbeds(unique_names=True,
143-
read_details_txt=True)},
143+
include_extra_info=True)},
144144
indent=4, sort_keys=True))
145145

146146
def json_platforms(mbeds, args):

0 commit comments

Comments
 (0)