Tools: Apply Black formatter to updatets.py

This commit is contained in:
Chris Hennes
2023-04-16 20:51:16 -05:00
parent e363a27c97
commit 2dabffbf6c

View File

@@ -54,33 +54,129 @@ import re
import pathlib
directories = [
{"tsname":"App", "workingdir":"./src/App", "tsdir":"Resources/translations"},
{"tsname":"FreeCAD", "workingdir":"./src/Gui", "tsdir":"Language"},
{"tsname":"AddonManager", "workingdir":"./src/Mod/AddonManager/", "tsdir":"Resources/translations"},
{"tsname":"Arch", "workingdir":"./src/Mod/Arch/", "tsdir":"Resources/translations"},
{"tsname":"Draft", "workingdir":"./src/Mod/Draft/", "tsdir":"Resources/translations"},
{"tsname":"Drawing", "workingdir":"./src/Mod/Drawing/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Fem", "workingdir":"./src/Mod/Fem/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Inspection", "workingdir":"./src/Mod/Inspection/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Material", "workingdir":"./src/Mod/Material/", "tsdir":"Resources/translations"},
{"tsname":"Mesh", "workingdir":"./src/Mod/Mesh/", "tsdir":"Gui/Resources/translations"},
{"tsname":"MeshPart", "workingdir":"./src/Mod/MeshPart/", "tsdir":"Gui/Resources/translations"},
{"tsname":"OpenSCAD", "workingdir":"./src/Mod/OpenSCAD/", "tsdir":"Resources/translations"},
{"tsname":"PartDesign", "workingdir":"./src/Mod/PartDesign/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Part", "workingdir":"./src/Mod/Part/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Path", "workingdir":"./src/Mod/Path/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Points", "workingdir":"./src/Mod/Points/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Raytracing", "workingdir":"./src/Mod/Raytracing/", "tsdir":"Gui/Resources/translations"},
{"tsname":"ReverseEngineering", "workingdir":"./src/Mod/ReverseEngineering/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Robot", "workingdir":"./src/Mod/Robot/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Sketcher", "workingdir":"./src/Mod/Sketcher/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Spreadsheet", "workingdir":"./src/Mod/Spreadsheet/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Start", "workingdir":"./src/Mod/Start/", "tsdir":"Gui/Resources/translations"},
{"tsname":"TechDraw", "workingdir":"./src/Mod/TechDraw/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Test", "workingdir":"./src/Mod/Test/", "tsdir":"Gui/Resources/translations"},
{"tsname":"Tux", "workingdir":"./src/Mod/Tux/", "tsdir":"Resources/translations"},
{"tsname":"Web", "workingdir":"./src/Mod/Web/", "tsdir":"Gui/Resources/translations"}
]
{"tsname": "App", "workingdir": "./src/App", "tsdir": "Resources/translations"},
{"tsname": "FreeCAD", "workingdir": "./src/Gui", "tsdir": "Language"},
{
"tsname": "AddonManager",
"workingdir": "./src/Mod/AddonManager/",
"tsdir": "Resources/translations",
},
{
"tsname": "Arch",
"workingdir": "./src/Mod/Arch/",
"tsdir": "Resources/translations",
},
{
"tsname": "Draft",
"workingdir": "./src/Mod/Draft/",
"tsdir": "Resources/translations",
},
{
"tsname": "Drawing",
"workingdir": "./src/Mod/Drawing/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Fem",
"workingdir": "./src/Mod/Fem/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Inspection",
"workingdir": "./src/Mod/Inspection/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Material",
"workingdir": "./src/Mod/Material/",
"tsdir": "Resources/translations",
},
{
"tsname": "Mesh",
"workingdir": "./src/Mod/Mesh/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "MeshPart",
"workingdir": "./src/Mod/MeshPart/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "OpenSCAD",
"workingdir": "./src/Mod/OpenSCAD/",
"tsdir": "Resources/translations",
},
{
"tsname": "PartDesign",
"workingdir": "./src/Mod/PartDesign/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Part",
"workingdir": "./src/Mod/Part/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Path",
"workingdir": "./src/Mod/Path/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Points",
"workingdir": "./src/Mod/Points/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Raytracing",
"workingdir": "./src/Mod/Raytracing/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "ReverseEngineering",
"workingdir": "./src/Mod/ReverseEngineering/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Robot",
"workingdir": "./src/Mod/Robot/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Sketcher",
"workingdir": "./src/Mod/Sketcher/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Spreadsheet",
"workingdir": "./src/Mod/Spreadsheet/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Start",
"workingdir": "./src/Mod/Start/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "TechDraw",
"workingdir": "./src/Mod/TechDraw/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Test",
"workingdir": "./src/Mod/Test/",
"tsdir": "Gui/Resources/translations",
},
{
"tsname": "Tux",
"workingdir": "./src/Mod/Tux/",
"tsdir": "Resources/translations",
},
{
"tsname": "Web",
"workingdir": "./src/Mod/Web/",
"tsdir": "Gui/Resources/translations",
},
]
QMAKE = ""
LUPDATE = ""
@@ -88,29 +184,30 @@ PYLUPDATE = ""
LCONVERT = ""
QT_VERSION_MAJOR = ""
def find_tools(noobsolete=True):
print(Usage + "\nFirst, lets find all necessary tools on your system")
global QMAKE, LUPDATE, PYLUPDATE, LCONVERT, QT_VERSION_MAJOR
p = subprocess.run(["lupdate","-version"],check=True,stdout=subprocess.PIPE)
p = subprocess.run(["lupdate", "-version"], check=True, stdout=subprocess.PIPE)
lupdate_version = p.stdout.decode()
result = re.search(r'.* ([456])\.([\d]+)\.([\d]+)', lupdate_version)
result = re.search(r".* ([456])\.([\d]+)\.([\d]+)", lupdate_version)
if not result:
print (f"Failed to parse version from {lupdate_version}")
print(f"Failed to parse version from {lupdate_version}")
QT_VERSION_MAJOR = int(result.group(1))
QT_VERSION_MINOR = int(result.group(2))
QT_VERSION_PATCH = int(result.group(3))
QT_VERSION = f"{QT_VERSION_MAJOR}.{QT_VERSION_MINOR}.{QT_VERSION_PATCH}"
print (f"Found Qt {QT_VERSION}")
print(f"Found Qt {QT_VERSION}")
if QT_VERSION_MAJOR < 6:
if (os.system("lupdate -version") == 0):
if os.system("lupdate -version") == 0:
LUPDATE = "lupdate"
# TODO: we suppose lupdate is a symlink to lupdate-qt4 for now
if noobsolete:
LUPDATE += " -no-obsolete"
elif (os.system("lupdate-qt5 -version") == 0):
elif os.system("lupdate-qt5 -version") == 0:
LUPDATE = "lupdate-qt5"
if noobsolete:
LUPDATE += " -no-obsolete"
@@ -120,47 +217,52 @@ def find_tools(noobsolete=True):
LUPDATE = "lupdate"
if QT_VERSION_MAJOR < 6:
if (os.system("qmake -version") == 0):
if os.system("qmake -version") == 0:
QMAKE = "qmake"
elif (os.system("qmake-qt5 -version") == 0):
elif os.system("qmake-qt5 -version") == 0:
QMAKE = "qmake-qt5"
else:
raise Exception("Cannot find qmake")
if (os.system("pylupdate -version") == 0):
if os.system("pylupdate -version") == 0:
PYLUPDATE = "pylupdate"
elif (os.system("pylupdate6 --version") == 0):
elif os.system("pylupdate6 --version") == 0:
PYLUPDATE = "pylupdate6"
if noobsolete:
PYLUPDATE += " -no-obsolete"
elif (os.system("pylupdate5 -version") == 0):
elif os.system("pylupdate5 -version") == 0:
PYLUPDATE = "pylupdate5"
if noobsolete:
PYLUPDATE += " -noobsolete"
elif (os.system("pylupdate4 -version") == 0):
elif os.system("pylupdate4 -version") == 0:
PYLUPDATE = "pylupdate4"
if noobsolete:
PYLUPDATE += " -noobsolete"
elif (os.system("pyside2-lupdate -version") == 0):
elif os.system("pyside2-lupdate -version") == 0:
PYLUPDATE = "pyside2-lupdate"
raise Exception("Please do not use pyside2-lupdate at the moment, as it shows encoding problems. Please use pylupdate5 or 6 instead.")
raise Exception(
"Please do not use pyside2-lupdate at the moment, as it shows encoding problems. Please use pylupdate5 or 6 instead."
)
else:
raise Exception("Cannot find pylupdate")
else:
QMAKE = "(qmake not needed for Qt 6 and later)"
PYLUPDATE = "(pylupdate not needed for Qt 6 and later)"
if (os.system("lconvert -h") == 0):
if os.system("lconvert -h") == 0:
LCONVERT = "lconvert"
if noobsolete and QT_VERSION_MAJOR < 6:
LCONVERT += " -no-obsolete"
else:
raise Exception("Cannot find lconvert")
print("\nAll Qt tools have been found!\n",
"\t" + QMAKE + "\n",
"\t" + LUPDATE + "\n",
"\t" + PYLUPDATE + "\n",
"\t" + LCONVERT + "\n")
print(
"\nAll Qt tools have been found!\n",
"\t" + QMAKE + "\n",
"\t" + LUPDATE + "\n",
"\t" + PYLUPDATE + "\n",
"\t" + LCONVERT + "\n",
)
print("==============================================\n")
def update_translation(entry):
global QMAKE, LUPDATE, LCONVERT, QT_VERSION_MAJOR
@@ -169,27 +271,37 @@ def update_translation(entry):
os.chdir(entry["workingdir"])
existingjsons = [f for f in os.listdir(".") if f.endswith(".json")]
project_filename = entry["tsname"] + ".pro"
tsBasename = os.path.join(entry["tsdir"],entry["tsname"])
tsBasename = os.path.join(entry["tsdir"], entry["tsname"])
if QT_VERSION_MAJOR < 6:
print ("\n\n=============================================")
print (f"EXTRACTING STRINGS FOR {entry['tsname']}")
print ("=============================================",flush=True)
print("\n\n=============================================")
print(f"EXTRACTING STRINGS FOR {entry['tsname']}")
print("=============================================", flush=True)
execline = []
execline.append (f"touch dummy_cpp_file_for_lupdate.cpp") #lupdate 5.x requires at least one source file to process the UI files
execline.append (f"touch {tsBasename}py.ts")
execline.append (f"{PYLUPDATE} `find ./ -name \"*.py\"` -ts {tsBasename}py.ts {log_redirect}")
execline.append (f"{QMAKE} -project -o {project_filename} -r")
execline.append (f"{LUPDATE} {project_filename} -ts {tsBasename}.ts {log_redirect}")
execline.append (f"sed 's/<translation.*>.*<\/translation>/<translation type=\"unfinished\"><\/translation>/g' {tsBasename}.ts > {tsBasename}.ts.temp")
execline.append (f"mv {tsBasename}.ts.temp {tsBasename}.ts")
execline.append (f"{LCONVERT} -i {tsBasename}py.ts {tsBasename}.ts -o {tsBasename}.ts {log_redirect}")
execline.append (f"rm {tsBasename}py.ts")
execline.append (f"rm dummy_cpp_file_for_lupdate.cpp")
execline.append(
f"touch dummy_cpp_file_for_lupdate.cpp"
) # lupdate 5.x requires at least one source file to process the UI files
execline.append(f"touch {tsBasename}py.ts")
execline.append(
f'{PYLUPDATE} `find ./ -name "*.py"` -ts {tsBasename}py.ts {log_redirect}'
)
execline.append(f"{QMAKE} -project -o {project_filename} -r")
execline.append(
f"{LUPDATE} {project_filename} -ts {tsBasename}.ts {log_redirect}"
)
execline.append(
f"sed 's/<translation.*>.*<\/translation>/<translation type=\"unfinished\"><\/translation>/g' {tsBasename}.ts > {tsBasename}.ts.temp"
)
execline.append(f"mv {tsBasename}.ts.temp {tsBasename}.ts")
execline.append(
f"{LCONVERT} -i {tsBasename}py.ts {tsBasename}.ts -o {tsBasename}.ts {log_redirect}"
)
execline.append(f"rm {tsBasename}py.ts")
execline.append(f"rm dummy_cpp_file_for_lupdate.cpp")
print(f"Executing commands in {entry['workingdir']}:")
for line in execline:
print (line)
print(line)
os.system(line)
print()
@@ -204,35 +316,81 @@ def update_translation(entry):
# used for all supported programming languages, so it's just a single function call
# For Windows compatibility, do most of the work in Python:
extensions = ["java","jui","ui","c","c++","cc","cpp","cxx","ch","h","h++","hh","hpp","hxx","js","qs","qml","qrc","py"]
with open ("files_to_translate.txt","w") as file_list:
extensions = [
"java",
"jui",
"ui",
"c",
"c++",
"cc",
"cpp",
"cxx",
"ch",
"h",
"h++",
"hh",
"hpp",
"hxx",
"js",
"qs",
"qml",
"qrc",
"py",
]
with open("files_to_translate.txt", "w") as file_list:
for root, dirs, files in os.walk("./"):
for f in files:
if pathlib.Path(f).suffix[1:] in extensions:
file_list.write(os.path.join(root,f) + "\n")
file_list.write(os.path.join(root, f) + "\n")
try:
p = subprocess.run([LUPDATE, "@files_to_translate.txt","-I","./", "-recursive", "-no-sort", "-ts", f"{tsBasename}.ts"], capture_output=True, timeout=60, encoding="utf-8")
p = subprocess.run(
[
LUPDATE,
"@files_to_translate.txt",
"-I",
"./",
"-recursive",
"-no-sort",
"-ts",
f"{tsBasename}.ts",
],
capture_output=True,
timeout=60,
encoding="utf-8",
)
except Exception as e:
print(str(e))
os.chdir(cur)
return
with open (f"{cur}/tsupdate_stdout.log", "a", encoding="utf-8") as f:
with open(f"{cur}/tsupdate_stdout.log", "a", encoding="utf-8") as f:
f.write(p.stdout.decode())
print(p.stdout.decode())
with open (f"{cur}/tsupdate_stderr.log", "a", encoding="utf-8") as f:
with open(f"{cur}/tsupdate_stderr.log", "a", encoding="utf-8") as f:
f.write(p.stderr.decode())
# Strip out obsolete strings, and make sure there are no translations in the main *.ts file
subprocess.run([LCONVERT, "-drop-translations", "-i", f"{tsBasename}.ts", "-o", f"{tsBasename}.ts"])
subprocess.run(
[
LCONVERT,
"-drop-translations",
"-i",
f"{tsBasename}.ts",
"-o",
f"{tsBasename}.ts",
]
)
else:
print("ERROR: unrecognized version of lupdate -- found Qt {QT_VERSION_MAJOR}, we only support 4, 5 and 6")
print(
"ERROR: unrecognized version of lupdate -- found Qt {QT_VERSION_MAJOR}, we only support 4, 5 and 6"
)
exit(1)
os.chdir(cur)
def main(mod=None):
find_tools()
@@ -245,16 +403,19 @@ def main(mod=None):
if mod:
for i in directories:
if i["tsname"] == mod:
print("WARNING - Updating",mod,"ONLY")
print("WARNING - Updating", mod, "ONLY")
update_translation(i)
break
else:
for i in directories:
update_translation(i)
print("\nIf updatets.py was run successfully, the next step is to run ./src/Tools/updatecrowdin.py")
print(
"\nIf updatets.py was run successfully, the next step is to run ./src/Tools/updatecrowdin.py"
)
print("stderr output from lupdate can be found in tsupdate_stderr.log")
print("stdout output from lupdate can be found in tsupdate_stdout.log")
if __name__ == "__main__":
if len(sys.argv[1:]) > 0:
main(sys.argv[1])