From 35da223f0a0dd32f04dc35664cedd5d441cc9268 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 7 Oct 2025 02:26:49 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Py=20script?= =?UTF-8?q?s=20refinements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- buildroot/bin/config.py | 6 +++--- buildroot/share/PlatformIO/scripts/config.py | 6 +++--- buildroot/share/PlatformIO/scripts/mc-apply.py | 2 +- .../PlatformIO/scripts/preflight-checks.py | 2 +- .../share/PlatformIO/scripts/signature.py | 2 +- buildroot/share/scripts/languageExport.py | 6 +++--- buildroot/share/scripts/languageImport.py | 18 +++++++++--------- buildroot/share/scripts/linesformat.py | 6 ++++-- 8 files changed, 25 insertions(+), 23 deletions(-) diff --git a/buildroot/bin/config.py b/buildroot/bin/config.py index 8d6cc5891a..a3c16e58f3 100755 --- a/buildroot/bin/config.py +++ b/buildroot/bin/config.py @@ -32,7 +32,7 @@ def set(file_path, define_name, value): # Write the modified content back to the file only if changes were made if modified: - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) return True @@ -59,7 +59,7 @@ def add(file_path, define_name, value=""): # If no blank line is found, append to the end content.append(f"#define {define_name}{value}\n") - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) def enable(file_path, define_name, enable=True): @@ -96,7 +96,7 @@ def enable(file_path, define_name, enable=True): # Write the modified content back to the file only if changes were made if modified: - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) return found diff --git a/buildroot/share/PlatformIO/scripts/config.py b/buildroot/share/PlatformIO/scripts/config.py index 8d6cc5891a..a3c16e58f3 100755 --- a/buildroot/share/PlatformIO/scripts/config.py +++ b/buildroot/share/PlatformIO/scripts/config.py @@ -32,7 +32,7 @@ def set(file_path, define_name, value): # Write the modified content back to the file only if changes were made if modified: - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) return True @@ -59,7 +59,7 @@ def add(file_path, define_name, value=""): # If no blank line is found, append to the end content.append(f"#define {define_name}{value}\n") - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) def enable(file_path, define_name, enable=True): @@ -96,7 +96,7 @@ def enable(file_path, define_name, enable=True): # Write the modified content back to the file only if changes were made if modified: - with open(file_path, 'w', encoding='utf-8') as f: + with open(file_path, 'w', encoding='utf-8', newline='') as f: f.writelines(content) return found diff --git a/buildroot/share/PlatformIO/scripts/mc-apply.py b/buildroot/share/PlatformIO/scripts/mc-apply.py index 4f341b0065..90b0064222 100755 --- a/buildroot/share/PlatformIO/scripts/mc-apply.py +++ b/buildroot/share/PlatformIO/scripts/mc-apply.py @@ -59,7 +59,7 @@ def back_up_config(name): nr = 1 if nr == '' else nr + 1 continue - with open(bak_path, 'w', encoding='utf-8') as b: + with open(bak_path, 'w', encoding='utf-8', newline='') as b: b.writelines(f.readlines()) break diff --git a/buildroot/share/PlatformIO/scripts/preflight-checks.py b/buildroot/share/PlatformIO/scripts/preflight-checks.py index 33beb49c16..210dc8fd0b 100644 --- a/buildroot/share/PlatformIO/scripts/preflight-checks.py +++ b/buildroot/share/PlatformIO/scripts/preflight-checks.py @@ -79,7 +79,7 @@ if pioutil.is_pio_build(): modified_text = text.replace("BOTH(", "ALL(").replace("EITHER(", "ANY(") if text != modified_text: conf_modified = True - with open(conf_path, 'w', encoding="utf8") as file: + with open(conf_path, 'w', encoding="utf8", newline='') as file: file.write(modified_text) if conf_modified: diff --git a/buildroot/share/PlatformIO/scripts/signature.py b/buildroot/share/PlatformIO/scripts/signature.py index fd155ef760..c91b5fcf2d 100755 --- a/buildroot/share/PlatformIO/scripts/signature.py +++ b/buildroot/share/PlatformIO/scripts/signature.py @@ -284,7 +284,7 @@ def compute_build_signature(env): for line in sec_lines[1:]: sec_list += '\n' + ext_fmt.format('', line) config_ini = build_path / 'config.ini' - with config_ini.open('w', encoding='utf-8') as outfile: + with config_ini.open('w', encoding='utf-8', newline='') as outfile: filegrp = { 'Configuration.h':'config:basic', 'Configuration_adv.h':'config:advanced' } vers = build_defines["CONFIGURATION_H_VERSION"] dt_string = datetime.now().strftime("%Y-%m-%d at %H:%M:%S") diff --git a/buildroot/share/scripts/languageExport.py b/buildroot/share/scripts/languageExport.py index ca1998c18e..30a983c076 100755 --- a/buildroot/share/scripts/languageExport.py +++ b/buildroot/share/scripts/languageExport.py @@ -17,7 +17,7 @@ LANGHOME = "Marlin/src/lcd/language" # Write multiple sheets if true, otherwise write one giant sheet MULTISHEET = '--single' not in argv[1:] -OUTDIR = 'out-csv' +OUTDIR = Path('out-csv') # Check for the path to the language files if not Path(LANGHOME).is_dir(): @@ -125,10 +125,10 @@ if MULTISHEET: # # Export a separate sheet for each language # - Path.mkdir(Path(OUTDIR), exist_ok=True) + OUTDIR.mkdir(exist_ok=True) for lang in langcodes: - with open("%s/language_%s.csv" % (OUTDIR, lang), 'w', encoding='utf-8') as f: + with open(OUTDIR / f"language_{lang}.csv", 'w', encoding='utf-8') as f: lname = lang + ' ' + namebyid(lang) header = ['name', lname, lname + ' (wide)', lname + ' (tall)'] f.write('"' + '","'.join(header) + '"\n') diff --git a/buildroot/share/scripts/languageImport.py b/buildroot/share/scripts/languageImport.py index c0bbd5951c..11d4c64cf1 100755 --- a/buildroot/share/scripts/languageImport.py +++ b/buildroot/share/scripts/languageImport.py @@ -18,9 +18,10 @@ TODO: Use the defines and comments above the namespace from existing language fi import sys, re, requests, csv, datetime #from languageUtil import namebyid +from pathlib import Path LANGHOME = "Marlin/src/lcd/language" -OUTDIR = 'out-language' +OUTDIR = Path('out-language') # Get the file path from the command line FILEPATH = sys.argv[1] if len(sys.argv) > 1 else None @@ -51,7 +52,7 @@ if download: exit(0) lines = csvdata.splitlines() -print(lines) +#print(lines) reader = csv.reader(lines, delimiter=',') gothead = False columns = [''] @@ -82,8 +83,7 @@ for row in reader: strings_per_lang[col['lang']][col['style']][name] = str_key # Create a folder for the imported language outfiles -from pathlib import Path -Path.mkdir(Path(OUTDIR), exist_ok=True) +OUTDIR.mkdir(exist_ok=True) FILEHEADER = ''' /** @@ -142,8 +142,8 @@ for i in range(1, numcols): if not lang in gotlang: gotlang[lang] = {} if f: f.close() - fn = "%s/language_%s.h" % (OUTDIR, lang) - f = open(fn, 'w', encoding='utf-8') + fn = OUTDIR / f"language_{lang}.h" + f = open(fn, 'w', encoding='utf-8', newline='') if not f: print("Failed to open %s." % fn) exit(1) @@ -199,9 +199,9 @@ for i in range(1, numcols): comm = '' if lang != 'en' and 'en' in strings_per_lang: en = strings_per_lang['en'] - if name in en[style]: str_key = en[style][name] - elif name in en['Narrow']: str_key = en['Narrow'][name] - if str_key: + if name in en[style]: str_key = en[style][name].strip() + elif name in en['Narrow']: str_key = en['Narrow'][name].strip() + if str_key and str_key != "English": cfmt = '%%%ss// %%s' % (50 - len(val) if len(val) < 50 else 1) comm = cfmt % (' ', str_key) diff --git a/buildroot/share/scripts/linesformat.py b/buildroot/share/scripts/linesformat.py index 4cbe8190f5..8978de0ec9 100755 --- a/buildroot/share/scripts/linesformat.py +++ b/buildroot/share/scripts/linesformat.py @@ -51,7 +51,8 @@ def format_text(argv): return # Open and read the file src_file - with open(src_file, 'r', encoding='utf-8') as rf: file_text = rf.read() + with open(src_file, 'r', encoding='utf-8') as rf: + file_text = rf.read() if len(file_text) == 0: print('No text to process') @@ -60,7 +61,8 @@ def format_text(argv): # Read from file or STDIN until it terminates filtered = re.sub(r'\s+$', '', file_text) + '\n' if dst_file: - with open(dst_file, 'w', encoding='utf-8') as wf: wf.write(filtered) + with open(dst_file, 'w', encoding='utf-8', newline='') as wf: + wf.write(filtered) else: print(filtered)