1818import re
1919import os
2020import json
21+ import os .path
2122
2223class MbedLsToolsBase :
2324 """ Base class for mbed-lstools, defines mbed-ls tools interface for mbed-enabled devices detection for various hosts
@@ -29,6 +30,12 @@ def __init__(self):
2930 self .DEBUG_FLAG = False # Used to enable debug code / prints
3031 self .ERRORLEVEL_FLAG = 0 # Used to return success code to environment
3132
33+ # If there is a local mocking data use it and add / override manufacture_ids
34+ mock_ids = self .mock_read ()
35+ if mock_ids :
36+ for mid in mock_ids :
37+ self .manufacture_ids [mid ] = mock_ids [mid ]
38+
3239 # Which OSs are supported by this module
3340 # Note: more than one OS can be supported by mbed-lstools_* module
3441 os_supported = []
@@ -155,7 +162,52 @@ def __init__(self):
155162 "RIOT" : "RIOT" ,
156163 }
157164
158- #
165+ MOCK_FILE_NAME = '.mbedls-mock'
166+
167+ def mock_read (self ):
168+ """! Load mocking data from local file
169+ @return Curent mocking configuration (dictionary)
170+ """
171+ if os .path .isfile (self .MOCK_FILE_NAME ):
172+ if self .DEBUG_FLAG :
173+ self .debug (self .mock_read .__name__ , "reading %s" % self .MOCK_FILE_NAME )
174+ with open (self .MOCK_FILE_NAME , "r" ) as f :
175+ return json .load (f )
176+ return {}
177+
178+ def mock_write (self , mock_ids ):
179+ # Write current mocking structure
180+ if self .DEBUG_FLAG :
181+ self .debug (self .mock_write .__name__ , "writing %s" % self .MOCK_FILE_NAME )
182+ with open (self .MOCK_FILE_NAME , "w" ) as f :
183+ f .write (json .dumps (mock_ids , indent = 4 ))
184+
185+ def mock_manufacture_ids (self , mid , platform_name , oper = '+' ):
186+ """! Replace (or add if manufacture id doesn't exist) entry in self.manufacture_ids
187+ @param oper '+' add new mock / override existing entry
188+ '-' remove mid from mocking entry
189+ @return Mocked structure (json format)
190+ """
191+ mock_ids = self .mock_read ()
192+
193+ # Operations on mocked structure
194+ if oper == '+' :
195+ mock_ids [mid ] = platform_name
196+ if self .DEBUG_FLAG :
197+ self .debug (self .mock_manufacture_ids .__name__ , "mock_ids['%s'] = '%s'" % (mid , platform_name ))
198+ elif oper in ['-' , '!' ]:
199+ if mid in mock_ids :
200+ mock_ids .pop (mid )
201+ if self .DEBUG_FLAG :
202+ self .debug (self .mock_manufacture_ids .__name__ , "removing '%s' mock" % mid )
203+ elif mid == '*' :
204+ mock_ids = {} # Zero mocking set
205+ if self .DEBUG_FLAG :
206+ self .debug (self .mock_manufacture_ids .__name__ , "zero mocking set" )
207+
208+ self .mock_write (mock_ids )
209+ return mock_ids
210+
159211 # Note: 'Ven_SEGGER' - This is used to detect devices from EFM family, they use Segger J-LInk to wrap MSD and CDC
160212 usb_vendor_list = ['Ven_MBED' , 'Ven_SEGGER' ]
161213
@@ -205,7 +257,7 @@ def list_platforms(self):
205257 result = []
206258 mbeds = self .list_mbeds ()
207259 for i , val in enumerate (mbeds ):
208- platform_name = val ['platform_name' ]
260+ platform_name = str ( val ['platform_name' ])
209261 if platform_name not in result :
210262 result .append (platform_name )
211263 return result
@@ -217,7 +269,7 @@ def list_platforms_ext(self):
217269 result = {}
218270 mbeds = self .list_mbeds ()
219271 for i , val in enumerate (mbeds ):
220- platform_name = val ['platform_name' ]
272+ platform_name = str ( val ['platform_name' ])
221273 if platform_name not in result :
222274 result [platform_name ] = 1
223275 else :
0 commit comments