1
1
import os
2
2
3
3
try :
4
- import tomlib as toml
4
+ import tomllib as toml
5
5
except ImportError :
6
6
try :
7
7
import toml
15
15
)
16
16
17
17
18
+ used_imports = []
19
+ global_variable_names = []
20
+
21
+
18
22
class TOMLMeta (type ):
19
23
20
24
def __call__ (cls , name , parent = None , ** kwargs ):
@@ -36,25 +40,27 @@ def __call__(cls, name, parent=None, **kwargs):
36
40
class TOMLObject (metaclass = TOMLMeta ):
37
41
38
42
def __init__ (self , name , parent = None , ** kwargs ):
39
-
40
43
if parent is not None and parent .name == 'MCU' :
41
44
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
42
54
55
+ self .mcu_obj = None
43
56
self .name = name
44
57
self .parent = parent
45
58
self .__kwargs = kwargs
46
59
self .__children = []
47
60
self .imports = []
48
- self .mcu = None
49
61
50
62
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' :
58
64
self .__children .append (child )
59
65
60
66
def __getattr__ (self , item ):
@@ -74,9 +80,21 @@ def fqn(self):
74
80
75
81
return self .name + ' = ' + self .parent .fqn
76
82
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
+
77
92
if self .name == 'I2C' :
78
93
return 'i2c.I2C'
79
94
95
+ if self .name == 'Spi3Wire' :
96
+ return 'spi3wire.Spi3Wire'
97
+
80
98
if self .name == 'SPI' :
81
99
return 'machine.SPI'
82
100
@@ -145,16 +163,17 @@ def constants(self):
145
163
146
164
def __str__ (self ):
147
165
if self .parent is None :
148
- var_names = self .var_names
166
+ global_variable_names . extend ( self .var_names )
149
167
150
168
output = []
151
169
output .extend (self .constants )
152
170
153
171
for child in self .__children :
154
- if child .name not in var_names :
172
+ if child .name not in global_variable_names :
155
173
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 :
157
175
self .imports .append (module )
176
+ used_imports .append (module )
158
177
output .extend (['' , f'import { module } ' , '' ])
159
178
160
179
output .append (str (child ))
@@ -178,16 +197,72 @@ def __str__(self):
178
197
if len (self .__kwargs ) == 1 :
179
198
key = list (self .__kwargs .keys ())[0 ]
180
199
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
+
181
215
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
185
218
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
187
242
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
+
188
263
params = ',\n ' .join (f' { k } ={ str (v )} ' for k , v in self .__kwargs .items () if not isinstance (v , dict ))
189
264
if params :
190
- output = [ f'{ fqn } (\n { params } \n )' , '' ]
265
+ output . append ( f'{ fqn } (\n { params } \n )\n ' )
191
266
else :
192
267
raise RuntimeError
193
268
@@ -203,7 +278,7 @@ def __str__(self):
203
278
class TOMLmcu :
204
279
205
280
def __init__ (self , mcu ):
206
- name = mcu .board
281
+ name = mcu .mcu
207
282
build_args = mcu .build_args
208
283
209
284
command = [name ]
@@ -259,10 +334,10 @@ def run(toml_path, output_file):
259
334
indevs = [f'INDEV={ item } ' for item in toml_obj .imports if item in indev_drivers ]
260
335
expanders = [f'EXPANDER={ item } ' for item in toml_obj .imports if item in io_expanders ]
261
336
262
- if toml_obj .mcu is None :
337
+ if toml_obj .mcu_obj is None :
263
338
build_command = []
264
339
else :
265
- build_command = toml_obj .build_command
340
+ build_command = toml_obj .mcu_obj . build_command
266
341
267
342
for display in displays [:]:
268
343
if display not in build_command :
0 commit comments