Skip to content

Commit a7bd35b

Browse files
πŸ§‘β€πŸ’» Python scripts cleanup, improve (#27533)
1 parent 8d864d7 commit a7bd35b

19 files changed

+37
-56
lines changed

β€ŽMarlin/src/HAL/LPC1768/upload_extra_script.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def before_upload(source, target, env):
3737
#
3838
# platformio.ini will accept this for a Windows upload port designation: 'upload_port = L:'
3939
# Windows - doesn't care about the disk's name, only cares about the drive letter
40-
import subprocess,string
40+
import subprocess, string
4141
from ctypes import windll
4242
from pathlib import PureWindowsPath
4343

β€ŽMarlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/scripts/file2cpp.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
# location: <https://www.gnu.org/licenses/>.
1717

1818
from __future__ import print_function
19-
import argparse
20-
import textwrap
21-
import os
22-
import zlib
19+
import argparse, textwrap, os, zlib
2320

2421
def deflate(data):
2522
return zlib.compress(data)

β€ŽMarlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/scripts/font2cpp.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
from __future__ import print_function
1919
from PIL import Image
20-
import argparse
21-
import textwrap
20+
import argparse, textwrap
2221

2322
def pack_rle(data):
2423
"""Use run-length encoding to pack the bytes"""

β€ŽMarlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/scripts/img2cpp.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
from __future__ import print_function
1919
from PIL import Image
20-
import argparse
21-
import textwrap
22-
import os
23-
import sys
24-
import zlib
20+
import argparse, textwrap, os, sys, zlib
2521

2622
class WriteSource:
2723
def __init__(self, mode):

β€ŽMarlin/src/lcd/extui/ftdi_eve_touch_ui/ftdi_eve_lib/scripts/svg2cpp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# location: <https://www.gnu.org/licenses/>.
1717

1818
from __future__ import print_function
19-
import argparse,re,sys
19+
import argparse, re, sys
2020

2121
from html.parser import HTMLParser
2222

β€Žbuildroot/bin/opt_disable

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
import sys, os,config
3+
import sys, os, config
44

55
def main():
66
args = sys.argv[1:]

β€Žbuildroot/bin/opt_enable

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
import sys, os,config
3+
import sys, os, config
44

55
def main():
66
args = sys.argv[1:]

β€Žbuildroot/share/PlatformIO/scripts/generic_create_variant.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# will be picked up by PlatformIO just like any other variant.
77
#
88
import pioutil, re
9+
910
marlin_variant_pattern = re.compile("marlin_.*")
1011
if pioutil.is_pio_build():
1112
import shutil, marlin
@@ -55,4 +56,4 @@
5556
variants_dir = here / 'buildroot' / 'share' / 'PlatformIO' / 'variants'
5657
source_dir = variants_dir / variant
5758
assert source_dir.is_dir()
58-
board.update("build.variants_dir", str(variants_dir));
59+
board.update("build.variants_dir", str(variants_dir))

β€Žbuildroot/share/PlatformIO/scripts/schema.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# been extended to evaluate conditions and can determine what options are actually enabled, not just which
1212
# options are uncommented. That will be migrated to this script for standalone migration.
1313
#
14-
import re,json
14+
import re, json
1515
from pathlib import Path
1616

1717
def extend_dict(d:dict, k:tuple):
@@ -120,8 +120,6 @@ class Parse:
120120
defgrep = re.compile(r'^(//)?\s*(#define)\s+([A-Za-z0-9_]+)\s*(.*?)\s*(//.+)?$')
121121
# Pattern to match a float value
122122
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')
125123
# Start with unknown state
126124
state = Parse.NORMAL
127125
# Serial ID
@@ -138,7 +136,7 @@ class Parse:
138136
eol_options = False # The options came from end of line, so only apply once
139137
join_line = False # A flag that the line should be joined with the previous one
140138
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
142140
# Loop through the lines in the file
143141
for the_line in fileobj.readlines():
144142
line_number += 1
@@ -175,7 +173,8 @@ class Parse:
175173
comment_buff = []
176174
if cline != '':
177175
# 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'
179178
last_added_ref[cfield] = cline
180179

181180
#
@@ -220,7 +219,6 @@ def use_comment(c, opt, sec, bufref):
220219
# Temperature sensors are done
221220
if state == Parse.GET_SENSORS:
222221
options_json = f'[ {options_json[:-2]} ]'
223-
224222
state = Parse.NORMAL
225223

226224
# Strip the leading '* ' from block comments
@@ -230,7 +228,7 @@ def use_comment(c, opt, sec, bufref):
230228
if state == Parse.GET_SENSORS:
231229
sens = re.match(r'^(-?\d+)\s*:\s*(.+)$', cline)
232230
if sens:
233-
s2 = sens[2].replace("'","''")
231+
s2 = sens[2].replace("'", "''")
234232
options_json += f"{sens[1]}:'{sens[1]} - {s2}', "
235233

236234
elif state == Parse.BLOCK_COMMENT:
@@ -255,12 +253,11 @@ def use_comment(c, opt, sec, bufref):
255253
comment_buff = []
256254
state = Parse.BLOCK_COMMENT
257255
eol_options = False
258-
259256
elif cpos2 != -1 and (cpos2 < cpos1 or cpos1 == -1):
260257
cpos = cpos2
261258

262259
# 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:
264261
state = Parse.EOL_COMMENT
265262
prev_comment = '\n'.join(comment_buff)
266263
comment_buff = []
@@ -327,10 +324,10 @@ def atomize(s):
327324
conditions.append([ f'!defined({line[7:].strip()})' ])
328325

329326
# Handle a complete #define line
330-
elif defmatch != None:
327+
elif defmatch is not None:
331328

332329
# 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]
334331

335332
# Increment the serial ID
336333
sid += 1
@@ -375,7 +372,7 @@ def atomize(s):
375372

376373
# If the comment_buff is not empty, add the comment to the info
377374
if comment_buff:
378-
full_comment = '\n'.join(comment_buff)
375+
full_comment = '\n'.join(comment_buff).strip()
379376

380377
# An EOL comment will be added later
381378
# The handling could go here instead of above
@@ -392,6 +389,14 @@ def atomize(s):
392389
if units == 's' or units == 'sec': units = 'seconds'
393390
define_info['units'] = units
394391

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+
395400
# Set the options for the current #define
396401
if define_name == "MOTHERBOARD" and boards != '':
397402
define_info['options'] = boards

β€Žbuildroot/share/PlatformIO/scripts/signature.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
#
33
# signature.py
44
#
5-
import schema
6-
7-
import subprocess,re,json,hashlib
5+
import schema, subprocess, re, json, hashlib
86
from datetime import datetime
97
from pathlib import Path
108
from functools import reduce

β€Žbuildroot/share/dwin/bin/DWIN_ICO.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
# does not define a name for 39. This is specially handled to
5555
# prevent reordering stock icons.
5656

57-
import os
58-
import struct
57+
import os, struct
5958
from PIL import Image
6059

6160
def getJpegResolution(jpegFile):

β€Žbuildroot/share/dwin/bin/makeIco.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
#----------------------------------------------------------------
2020

21-
import os.path
22-
import argparse
23-
import DWIN_ICO
21+
import os.path, argparse, DWIN_ICO
2422

2523
version = '2.0.7'
2624

β€Žbuildroot/share/dwin/bin/splitIco.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919
#----------------------------------------------------------------
2020

21-
import os.path
22-
import argparse
23-
import DWIN_ICO
21+
import os.path, argparse, DWIN_ICO
2422

2523
version = '2.0.7'
2624

β€Žbuildroot/share/fonts/buildhzk.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
# Author: Taylor Talkington
55
# License: GPL
66

7-
import bdflib.reader
8-
import math
7+
import bdflib.reader, math
98

109
def glyph_bits(size_x, size_y, font, glyph_ord):
1110
asc = font[b'FONT_ASCENT']

β€Žbuildroot/share/scripts/MarlinBinaryProtocol.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
# MarlinBinaryProtocol.py
33
# Supporting Firmware upload via USB/Serial, saving to the attached media.
44
#
5-
import serial
6-
import math
7-
import time
5+
import serial, math, time, threading, sys, datetime, random
86
from collections import deque
9-
import threading
10-
import sys
11-
import datetime
12-
import random
7+
138
try:
149
import heatshrink2 as heatshrink
1510
heatshrink_exists = True

β€Žbuildroot/share/scripts/createSpeedLookupTable.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22

3-
from __future__ import print_function
4-
from __future__ import division
3+
from __future__ import print_function, division
54

65
""" Generate the stepper delay lookup table for Marlin firmware. """
76

β€Žbuildroot/share/scripts/createTemperatureLookupMarlin.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
--num-temps=... the number of temperature points to calculate (default: 36)
1919
"""
2020

21-
from __future__ import print_function
22-
from __future__ import division
21+
from __future__ import print_function, division
2322

2423
from math import *
2524
import sys, getopt

β€Žbuildroot/share/scripts/rle16_compress_cpp_image_data.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#
66
# Usage: rle16_compress_cpp_image_data.py INPUT_FILE.cpp OUTPUT_FILE.cpp
77
#
8-
import sys, struct
9-
import re
8+
import sys, struct, re
109

1110
def addCompressedData(input_file, output_file):
1211
ofile = open(output_file, 'wt')

β€Žbuildroot/share/scripts/rle_compress_bitmap.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#
77
# Usage: rle_compress_bitmap.py INPUT_FILE OUTPUT_FILE
88
#
9-
import sys, struct
10-
import re
9+
import sys, struct, re
1110

1211
def addCompressedData(input_file, output_file):
1312
input_lines = input_file.readlines()

0 commit comments

Comments
Β (0)