@@ -29,6 +29,7 @@ def __init__(self):
29
29
#extra flags
30
30
self .DEBUG_FLAG = False # Used to enable debug code / prints
31
31
self .ERRORLEVEL_FLAG = 0 # Used to return success code to environment
32
+ self .retarget_data = {} # Used to retarget mbed-enabled platform properties
32
33
33
34
# If there is a local mocking data use it and add / override manufacture_ids
34
35
mock_ids = self .mock_read ()
@@ -169,30 +170,71 @@ def __init__(self):
169
170
}
170
171
171
172
MOCK_FILE_NAME = '.mbedls-mock'
173
+ RETARGET_FILE_NAME = 'mbedls.json'
172
174
173
175
def mock_read (self ):
174
176
"""! Load mocking data from local file
175
177
@return Curent mocking configuration (dictionary)
176
178
"""
177
179
if os .path .isfile (self .MOCK_FILE_NAME ):
178
180
if self .DEBUG_FLAG :
179
- self .debug (self .mock_read .__name__ , "reading %s" % self .MOCK_FILE_NAME )
180
- with open (self .MOCK_FILE_NAME , "r" ) as f :
181
- return json .load (f )
181
+ self .debug (self .mock_read .__name__ , "reading mock file %s" % self .MOCK_FILE_NAME )
182
+ try :
183
+ with open (self .MOCK_FILE_NAME , "r" ) as f :
184
+ return json .load (f )
185
+ except IOError as e :
186
+ self .err ("reading file '%s' failed: %s" % (os .path .abspath (self .MOCK_FILE_NAME ),
187
+ str (e )))
188
+ except ValueError as e :
189
+ self .err ("reading file '%s' content failed: %s" % (os .path .abspath (self .MOCK_FILE_NAME ),
190
+ str (e )))
182
191
return {}
183
192
184
193
def mock_write (self , mock_ids ):
185
- # Write current mocking structure
194
+ """! Write current mocking structure
195
+ @param mock_ids JSON mock data to dump to file
196
+ """
186
197
if self .DEBUG_FLAG :
187
198
self .debug (self .mock_write .__name__ , "writing %s" % self .MOCK_FILE_NAME )
188
- with open (self .MOCK_FILE_NAME , "w" ) as f :
189
- f .write (json .dumps (mock_ids , indent = 4 ))
199
+ try :
200
+ with open (self .MOCK_FILE_NAME , "w" ) as f :
201
+ f .write (json .dumps (mock_ids , indent = 4 ))
202
+ except IOError as e :
203
+ self .err ("reading file '%s' failed: %s" % (os .path .abspath (self .MOCK_FILE_NAME ),
204
+ str (e )))
205
+ except ValueError as e :
206
+ self .err ("reading file '%s' content failed: %s" % (os .path .abspath (self .MOCK_FILE_NAME ),
207
+ str (e )))
208
+
209
+ def retarget_read (self ):
210
+ """! Load retarget data from local file
211
+ @return Curent retarget configuration (dictionary)
212
+ """
213
+ if os .path .isfile (self .RETARGET_FILE_NAME ):
214
+ if self .DEBUG_FLAG :
215
+ self .debug (self .retarget_read .__name__ , "reading retarget file %s" % self .RETARGET_FILE_NAME )
216
+ try :
217
+ with open (self .RETARGET_FILE_NAME , "r" ) as f :
218
+ return json .load (f )
219
+ except IOError as e :
220
+ self .err ("reading file '%s' failed: %s" % (os .path .abspath (self .RETARGET_FILE_NAME ),
221
+ str (e )))
222
+ except ValueError as e :
223
+ self .err ("reading file '%s' content failed: %s" % (os .path .abspath (self .RETARGET_FILE_NAME ),
224
+ str (e )))
225
+ return {}
226
+
227
+ def retarget (self ):
228
+ """! Enable retargeting
229
+ """
230
+ self .retarget_data = self .retarget_read ()
231
+ return self .retarget_data
190
232
191
233
def mock_manufacture_ids (self , mid , platform_name , oper = '+' ):
192
234
"""! Replace (or add if manufacture id doesn't exist) entry in self.manufacture_ids
193
- @param oper '+' add new mock / override existing entry
194
- '-' remove mid from mocking entry
195
- @return Mocked structure (json format)
235
+ @param oper '+' add new mock / override existing entry
236
+ '-' remove mid from mocking entry
237
+ @return Mocked structure (json format)
196
238
"""
197
239
mock_ids = self .mock_read ()
198
240
@@ -236,9 +278,7 @@ def list_mbeds(self):
236
278
237
279
def list_mbeds_ext (self ):
238
280
"""! Function adds extra information for each mbed device
239
-
240
281
@return Returns list of mbed devices plus extended data like 'platform_name_unique'
241
-
242
282
@details Get information about mbeds with extended parameters/info included
243
283
"""
244
284
platform_names = {} # Count existing platforms and assign unique number
@@ -252,13 +292,22 @@ def list_mbeds_ext(self):
252
292
platform_names [platform_name ] += 1
253
293
# Assign normalized, unique string at the end of target name: TARGET_NAME[x] where x is an ordinal integer
254
294
mbeds [i ]['platform_name_unique' ] = "%s[%d]" % (platform_name , platform_names [platform_name ])
295
+
296
+ # Retarget values from retarget (mbedls.json) file
297
+ if self .retarget_data and 'target_id' in val :
298
+ target_id = val ['target_id' ]
299
+ if target_id in self .retarget_data :
300
+ mbeds [i ].update (self .retarget_data [target_id ])
301
+ if self .DEBUG_FLAG :
302
+ self .debug (self .list_mbeds_ext .__name__ , ("retargeting" , target_id , mbed [i ]))
303
+
255
304
if self .DEBUG_FLAG :
256
305
self .debug (self .list_mbeds_ext .__name__ , (mbeds [i ]['platform_name_unique' ], val ['target_id' ]))
257
306
return mbeds
258
307
259
308
def list_platforms (self ):
260
- """ Useful if you just want to know which platforms are currently available on the system
261
- @return List of (unique values) available platforms
309
+ """! Useful if you just want to know which platforms are currently available on the system
310
+ @return List of (unique values) available platforms
262
311
"""
263
312
result = []
264
313
mbeds = self .list_mbeds ()
@@ -269,8 +318,8 @@ def list_platforms(self):
269
318
return result
270
319
271
320
def list_platforms_ext (self ):
272
- """ Useful if you just want to know how many platforms of each type are currently available on the system
273
- @return Dict of platform: platform_count
321
+ """! Useful if you just want to know how many platforms of each type are currently available on the system
322
+ @return Dict of platform: platform_count
274
323
"""
275
324
result = {}
276
325
mbeds = self .list_mbeds ()
@@ -283,12 +332,9 @@ def list_platforms_ext(self):
283
332
return result
284
333
285
334
def list_mbeds_by_targetid (self ):
286
- """ Get information about mbeds with extended parameters/info included
287
-
288
- @return Returns dictionary where keys are TargetIDs and values are mbed structures
289
-
290
- @details Ordered by target id (key: target_id).
291
-
335
+ """! Get information about mbeds with extended parameters/info included
336
+ @return Returns dictionary where keys are TargetIDs and values are mbed structures
337
+ @details Ordered by target id (key: target_id).
292
338
"""
293
339
result = {}
294
340
mbed_list = self .list_mbeds_ext ()
@@ -299,27 +345,23 @@ def list_mbeds_by_targetid(self):
299
345
300
346
# Private part, methods used to drive interface functions
301
347
def load_mbed_description (self , file_name ):
302
- """ Loads JSON file with mbeds' description (mapping between target id and platform name)
348
+ """! Load JSON file with mbeds' description (mapping between target id and platform name)
303
349
Sets self.manufacture_ids with mapping between manufacturers' ids and platform name.
304
350
"""
305
351
#self.manufacture_ids = {} # TODO: load this values from file
306
352
pass
307
353
308
354
def err (self , text ):
309
355
"""! Prints error messages
310
-
311
356
@param text Text to be included in error message
312
-
313
357
@details Function prints directly on console
314
358
"""
315
359
print 'error: %s' % text
316
360
317
361
def debug (self , name , text ):
318
362
"""! Prints error messages
319
-
320
363
@param name Called function name
321
364
@param text Text to be included in debug message
322
-
323
365
@details Function prints directly on console
324
366
"""
325
367
print 'debug @%s.%s: %s' % (self .__class__ .__name__ , name , text )
@@ -333,12 +375,10 @@ def __str__(self):
333
375
334
376
def get_string (self , border = False , header = True , padding_width = 0 , sortby = 'platform_name' ):
335
377
"""! Printing with some sql table like decorators
336
-
337
378
@param border Table border visibility
338
379
@param header Table header visibility
339
380
@param padding_width Table padding
340
381
@param sortby Column used to sort results
341
-
342
382
@return Returns string which can be printed on console
343
383
"""
344
384
from prettytable import PrettyTable
@@ -367,7 +407,6 @@ def get_string(self, border=False, header=True, padding_width=0, sortby='platfor
367
407
368
408
def get_json_data_from_file (self , json_spec_filename , verbose = False ):
369
409
"""! Loads from file JSON formatted string to data structure
370
-
371
410
@return None if JSON can be loaded
372
411
"""
373
412
result = None
@@ -386,9 +425,7 @@ def get_json_data_from_file(self, json_spec_filename, verbose=False):
386
425
387
426
def get_mbed_htm_target_id (self , mount_point ):
388
427
"""! Function scans mbed.htm to get information about TargetID.
389
-
390
428
@return Function returns targetID, in case of failure returns None.
391
-
392
429
@details Note: This function should be improved to scan variety of boards' mbed.htm files
393
430
"""
394
431
result = None
@@ -409,7 +446,6 @@ def get_mbed_htm_target_id(self, mount_point):
409
446
410
447
def scan_html_line_for_target_id (self , line ):
411
448
"""! Scan if given line contains target id encoded in URL.
412
-
413
449
@return Returns None when no target_id string in line
414
450
"""
415
451
# Detecting modern mbed.htm file format
0 commit comments