11import os
22
33try :
4- import tomlib as toml
4+ import tomllib as toml
55except ImportError :
66 try :
77 import toml
1515 )
1616
1717
18+ used_imports = []
19+ global_variable_names = []
20+
21+
1822class TOMLMeta (type ):
1923
2024 def __call__ (cls , name , parent = None , ** kwargs ):
@@ -36,25 +40,27 @@ def __call__(cls, name, parent=None, **kwargs):
3640class TOMLObject (metaclass = TOMLMeta ):
3741
3842 def __init__ (self , name , parent = None , ** kwargs ):
39-
4043 if parent is not None and parent .name == 'MCU' :
4144 self .build_args = kwargs
45+ self .mcu = name
46+
47+ paren = parent .parent
48+ while paren .parent is not None :
49+ paren = parent .parent
50+
51+ paren .mcu_obj = TOMLmcu (self )
52+ else :
53+ self .mcu = None
4254
55+ self .mcu_obj = None
4356 self .name = name
4457 self .parent = parent
4558 self .__kwargs = kwargs
4659 self .__children = []
4760 self .imports = []
48- self .mcu = None
4961
5062 def add_child (self , child ):
51- if self .name == 'MCU' :
52- self .__dict__ .update (child .__dict__ )
53- self .board = child .name
54- self .name = 'MCU'
55- elif self .parent is None and child .name == 'MCU' :
56- self .mcu = TOMLmcu (child )
57- else :
63+ if child .name != 'MCU' :
5864 self .__children .append (child )
5965
6066 def __getattr__ (self , item ):
@@ -74,9 +80,21 @@ def fqn(self):
7480
7581 return self .name + ' = ' + self .parent .fqn
7682
83+ if self .name == 'RGBDisplay' :
84+ return 'rgb_display.RGBDisplay'
85+
86+ if self .name == 'SDLDisplay' :
87+ return 'sdl_display.SDLDisplay'
88+
89+ if self .name == 'SDLPointer' :
90+ return 'sdl_pointer.SDLPointer'
91+
7792 if self .name == 'I2C' :
7893 return 'i2c.I2C'
7994
95+ if self .name == 'Spi3Wire' :
96+ return 'spi3wire.Spi3Wire'
97+
8098 if self .name == 'SPI' :
8199 return 'machine.SPI'
82100
@@ -145,16 +163,17 @@ def constants(self):
145163
146164 def __str__ (self ):
147165 if self .parent is None :
148- var_names = self .var_names
166+ global_variable_names . extend ( self .var_names )
149167
150168 output = []
151169 output .extend (self .constants )
152170
153171 for child in self .__children :
154- if child .name not in var_names :
172+ if child .name not in global_variable_names :
155173 module = child .fqn .split ('.' )[0 ]
156- if module not in self .imports :
174+ if module not in self .imports and module not in used_imports :
157175 self .imports .append (module )
176+ used_imports .append (module )
158177 output .extend (['' , f'import { module } ' , '' ])
159178
160179 output .append (str (child ))
@@ -178,16 +197,72 @@ def __str__(self):
178197 if len (self .__kwargs ) == 1 :
179198 key = list (self .__kwargs .keys ())[0 ]
180199 if key == 'params' :
200+ output = ''
201+ for param in self .__kwargs [key ]:
202+ if isinstance (param , str ) and '.' in param :
203+ mod = param .split ('.' , 1 )[0 ]
204+ if (
205+ mod not in used_imports and
206+ mod not in global_variable_names and (
207+ mod in display_drivers or
208+ mod in indev_drivers or
209+ mod in io_expanders
210+ )
211+ ):
212+ output += f'import { mod } \n \n '
213+ used_imports .append (mod )
214+
181215 params = ', ' .join (str (itm ) for itm in self .__kwargs [key ])
182- return f'{ fqn } ({ params } )'
183- elif key == 'value' :
184- return f'{ fqn } = ' + str (self .__kwargs [key ])
216+ output += f'{ fqn } ({ params } )'
217+ return output
185218 else :
186- return f'{ fqn } ({ key } ={ str (self .__kwargs [key ])} )'
219+ output = ''
220+ if (
221+ isinstance (self .__kwargs [key ], str ) and
222+ '.' in self .__kwargs [key ]
223+ ):
224+ mod = self .__kwargs [key ].split ('.' , 1 )[0 ]
225+ if (
226+ mod not in used_imports and
227+ mod not in global_variable_names and (
228+ mod in display_drivers or
229+ mod in indev_drivers or
230+ mod in io_expanders
231+ )
232+ ):
233+ output += f'import { mod } \n \n '
234+ used_imports .append (mod )
235+
236+ if key == 'value' :
237+ output += f'{ fqn } = ' + str (self .__kwargs [key ])
238+ else :
239+ output += f'{ fqn } ({ key } ={ str (self .__kwargs [key ])} )'
240+
241+ return output
187242 else :
243+ output = []
244+
245+ for v in self .__kwargs .values ():
246+ if not (isinstance (v , str ) and '.' in v ):
247+ continue
248+
249+ mod = v .split ('.' , 1 )[0 ]
250+ if (
251+ mod not in used_imports and
252+ mod not in global_variable_names and (
253+ mod in display_drivers or
254+ mod in indev_drivers or
255+ mod in io_expanders
256+ )
257+ ):
258+ output .append (f'import { mod } ' )
259+ used_imports .append (mod )
260+ if output :
261+ output .append ('' )
262+
188263 params = ',\n ' .join (f' { k } ={ str (v )} ' for k , v in self .__kwargs .items () if not isinstance (v , dict ))
189264 if params :
190- output = [ f'{ fqn } (\n { params } \n )' , '' ]
265+ output . append ( f'{ fqn } (\n { params } \n )\n ' )
191266 else :
192267 raise RuntimeError
193268
@@ -203,7 +278,7 @@ def __str__(self):
203278class TOMLmcu :
204279
205280 def __init__ (self , mcu ):
206- name = mcu .board
281+ name = mcu .mcu
207282 build_args = mcu .build_args
208283
209284 command = [name ]
@@ -259,10 +334,10 @@ def run(toml_path, output_file):
259334 indevs = [f'INDEV={ item } ' for item in toml_obj .imports if item in indev_drivers ]
260335 expanders = [f'EXPANDER={ item } ' for item in toml_obj .imports if item in io_expanders ]
261336
262- if toml_obj .mcu is None :
337+ if toml_obj .mcu_obj is None :
263338 build_command = []
264339 else :
265- build_command = toml_obj .build_command
340+ build_command = toml_obj .mcu_obj . build_command
266341
267342 for display in displays [:]:
268343 if display not in build_command :
0 commit comments