11
11
# been extended to evaluate conditions and can determine what options are actually enabled, not just which
12
12
# options are uncommented. That will be migrated to this script for standalone migration.
13
13
#
14
- import re ,json
14
+ import re , json
15
15
from pathlib import Path
16
16
17
17
def extend_dict (d :dict , k :tuple ):
@@ -120,8 +120,6 @@ class Parse:
120
120
defgrep = re .compile (r'^(//)?\s*(#define)\s+([A-Za-z0-9_]+)\s*(.*?)\s*(//.+)?$' )
121
121
# Pattern to match a float value
122
122
flt = r'[-+]?\s*(\d+\.|\d*\.\d+)([eE][-+]?\d+)?[fF]?'
123
- # Defines to ignore
124
- ignore = ('CONFIGURATION_H_VERSION' , 'CONFIGURATION_ADV_H_VERSION' , 'CONFIG_EXAMPLES_DIR' , 'CONFIG_EXPORT' )
125
123
# Start with unknown state
126
124
state = Parse .NORMAL
127
125
# Serial ID
@@ -138,7 +136,7 @@ class Parse:
138
136
eol_options = False # The options came from end of line, so only apply once
139
137
join_line = False # A flag that the line should be joined with the previous one
140
138
line = '' # A line buffer to handle \ continuation
141
- last_added_ref = None # Reference to the last added item
139
+ last_added_ref = {} # Reference to the last added item
142
140
# Loop through the lines in the file
143
141
for the_line in fileobj .readlines ():
144
142
line_number += 1
@@ -175,7 +173,8 @@ class Parse:
175
173
comment_buff = []
176
174
if cline != '' :
177
175
# A (block or slash) comment was already added
178
- cfield = 'notes' if 'comment' in last_added_ref else 'comment'
176
+ if 'comment' in last_added_ref :
177
+ cfield = 'notes'
179
178
last_added_ref [cfield ] = cline
180
179
181
180
#
@@ -220,7 +219,6 @@ def use_comment(c, opt, sec, bufref):
220
219
# Temperature sensors are done
221
220
if state == Parse .GET_SENSORS :
222
221
options_json = f'[ { options_json [:- 2 ]} ]'
223
-
224
222
state = Parse .NORMAL
225
223
226
224
# Strip the leading '* ' from block comments
@@ -230,7 +228,7 @@ def use_comment(c, opt, sec, bufref):
230
228
if state == Parse .GET_SENSORS :
231
229
sens = re .match (r'^(-?\d+)\s*:\s*(.+)$' , cline )
232
230
if sens :
233
- s2 = sens [2 ].replace ("'" ,"''" )
231
+ s2 = sens [2 ].replace ("'" , "''" )
234
232
options_json += f"{ sens [1 ]} :'{ sens [1 ]} - { s2 } ', "
235
233
236
234
elif state == Parse .BLOCK_COMMENT :
@@ -255,12 +253,11 @@ def use_comment(c, opt, sec, bufref):
255
253
comment_buff = []
256
254
state = Parse .BLOCK_COMMENT
257
255
eol_options = False
258
-
259
256
elif cpos2 != - 1 and (cpos2 < cpos1 or cpos1 == - 1 ):
260
257
cpos = cpos2
261
258
262
259
# Comment after a define may be continued on the following lines
263
- if defmatch != None and cpos > 10 :
260
+ if defmatch is not None and cpos > 10 :
264
261
state = Parse .EOL_COMMENT
265
262
prev_comment = '\n ' .join (comment_buff )
266
263
comment_buff = []
@@ -327,10 +324,10 @@ def atomize(s):
327
324
conditions .append ([ f'!defined({ line [7 :].strip ()} )' ])
328
325
329
326
# Handle a complete #define line
330
- elif defmatch != None :
327
+ elif defmatch is not None :
331
328
332
329
# Get the match groups into vars
333
- enabled , define_name , val = defmatch [1 ] == None , defmatch [3 ], defmatch [4 ]
330
+ enabled , define_name , val = defmatch [1 ] is None , defmatch [3 ], defmatch [4 ]
334
331
335
332
# Increment the serial ID
336
333
sid += 1
@@ -375,7 +372,7 @@ def atomize(s):
375
372
376
373
# If the comment_buff is not empty, add the comment to the info
377
374
if comment_buff :
378
- full_comment = '\n ' .join (comment_buff )
375
+ full_comment = '\n ' .join (comment_buff ). strip ()
379
376
380
377
# An EOL comment will be added later
381
378
# The handling could go here instead of above
@@ -392,6 +389,14 @@ def atomize(s):
392
389
if units == 's' or units == 'sec' : units = 'seconds'
393
390
define_info ['units' ] = units
394
391
392
+ if 'comment' not in define_info or define_info ['comment' ] == '' :
393
+ if prev_comment :
394
+ define_info ['comment' ] = prev_comment
395
+ prev_comment = ''
396
+
397
+ if 'comment' in define_info and define_info ['comment' ] == '' :
398
+ del define_info ['comment' ]
399
+
395
400
# Set the options for the current #define
396
401
if define_name == "MOTHERBOARD" and boards != '' :
397
402
define_info ['options' ] = boards
0 commit comments