🧑‍💻 Py scripts refinements

This commit is contained in:
Scott Lahteine 2025-10-07 02:26:49 -05:00
parent 0937fe55a4
commit 35da223f0a
8 changed files with 25 additions and 23 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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")

View file

@ -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')

View file

@ -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)

View file

@ -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)