Move to python venv so that upto date chameleon avaiable
This commit is contained in:
111
sm2gen.py
111
sm2gen.py
@@ -10,6 +10,8 @@ from datetime import datetime
|
||||
from openai import OpenAI
|
||||
import configparser
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
#
|
||||
# To Do
|
||||
@@ -362,25 +364,31 @@ if __name__ == "__main__":
|
||||
"-nco",
|
||||
"--noController",
|
||||
help="Stop it creating a controller file",
|
||||
default="no",
|
||||
default="yes",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-nh", "--noHtml", help="Stop it creating html files(s)", default="no"
|
||||
"-nh", "--noHtml", help="Stop it creating html files(s)", default="yes"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-nl",
|
||||
"--noLang",
|
||||
help="Stop it creating language localise files(s)",
|
||||
default="no",
|
||||
default="yes",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-ncu", "--noCust", help="Stop it creating Custom controller file", default="no"
|
||||
"-ncu", "--noCust", help="Stop it creating Custom controller file", default="yes"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
json_filename = args.filename
|
||||
print(
|
||||
f"JSON5 from {json_filename} with noController={args.noController}, noHtml={args.noHtml} and noLang={args.noLang}"
|
||||
) # Not yet activated
|
||||
|
||||
#check if json5 file exists
|
||||
json_file_path = Path(json_filename)
|
||||
if not json_file_path.exists():
|
||||
print(f"json5 file: {json_filename} not found")
|
||||
quit(1)
|
||||
|
||||
# check syntax of JSON5
|
||||
lint_json5(json_filename)
|
||||
@@ -400,17 +408,23 @@ if __name__ == "__main__":
|
||||
lc_routes = lc_get_all_routes()
|
||||
|
||||
# File names
|
||||
controller_file = "Targets/" + hl("PackageName") + ".pm"
|
||||
custom_controller_file = "Targets/" + hl("PackageName") + "-Custom.pm"
|
||||
# Define the path for the generated files
|
||||
directory_path = Path("Targets/" + hl("PackageName"))
|
||||
# Create the directory if it doesn't exist
|
||||
directory_path.mkdir(parents=True, exist_ok=True)
|
||||
target_directory_path = "Targets/" + hl("PackageName")+"/"
|
||||
|
||||
controller_file = target_directory_path + hl("PackageName") + ".pm"
|
||||
custom_controller_file = target_directory_path + hl("PackageName") + "-Custom.pm"
|
||||
# Call it .new if one is already there (and may have been editted by the developer)
|
||||
if os.path.exists(custom_controller_file):
|
||||
custom_controller_file = custom_controller_file + ".new"
|
||||
layout_file = "Targets/" + hl("PackageName").lower() + ".html.ep"
|
||||
layout_file = target_directory_path + hl("PackageName").lower() + ".html.ep"
|
||||
partial_files = list()
|
||||
for panel in routes:
|
||||
partial_files.append("Targets/_" + hl("prefix") + "_" + panel + ".html.ep")
|
||||
print(partial_files)
|
||||
lex_file = "Targets/" + hl("PackageName").lower() + "_en.lex"
|
||||
partial_files.append(target_directory_path + hl("prefix") + "_" + panel + ".html.ep")
|
||||
print(f"Partial files to be created:{partial_files}")
|
||||
lex_file = target_directory_path + hl("PackageName").lower() + "_en.lex"
|
||||
tablecontrols = (
|
||||
get_table_control_data()
|
||||
) # arrays of hashes used to drive rows in tables
|
||||
@@ -619,43 +633,44 @@ if __name__ == "__main__":
|
||||
file.write(file_content)
|
||||
print(f"Write out modified:{filename}")
|
||||
|
||||
# Now generate all the translated lex files from a list of the languages and codes
|
||||
#home_dir = os.path.dirname(json_filename)
|
||||
languages_path = "Templates/languages.json"
|
||||
with open(languages_path, "r") as file:
|
||||
languages_str = file.read()
|
||||
lang_dict = json.loads(languages_str)
|
||||
with open(lex_file, "r") as file:
|
||||
lex_str = file.read()
|
||||
eng_lex_dict = convert_lex_to_dict(lex_str)
|
||||
for lang_item in lang_dict:
|
||||
print(f"Translating from english lex file to {lang_item['language']}")
|
||||
code = lang_item["code"]
|
||||
translated_lex_file = f"Targets/{hl('PackageName').lower()}_{code}.lex"
|
||||
# Only do it if the lex file is missing
|
||||
if not os.path.exists(translated_lex_file):
|
||||
translated_dict = []
|
||||
for lex_item in eng_lex_dict:
|
||||
# Get it from ChatGPT
|
||||
translated_text = get_translation(
|
||||
lex_item["text"], lang_item["language"]
|
||||
)
|
||||
translated_dict.append({"id": lex_item["id"], "text": translated_text})
|
||||
print(f"Writing out lex file for {lang_item['code']}")
|
||||
with open(translated_lex_file, "w") as file:
|
||||
for item in translated_dict:
|
||||
# escape any nasties
|
||||
translated_text = (
|
||||
item["text"]
|
||||
.replace("\\", r"\\")
|
||||
.replace('"', r"\"")
|
||||
.replace("'", r"\'")
|
||||
# Now generate all the translated lex files from a list of the languages and codes
|
||||
# if specifically requested
|
||||
if not args.noLang:
|
||||
languages_path = "Templates/languages.json"
|
||||
with open(languages_path, "r") as file:
|
||||
languages_str = file.read()
|
||||
lang_dict = json.loads(languages_str)
|
||||
with open(lex_file, "r") as file:
|
||||
lex_str = file.read()
|
||||
eng_lex_dict = convert_lex_to_dict(lex_str)
|
||||
for lang_item in lang_dict:
|
||||
print(f"Translating from english lex file to {lang_item['language']}")
|
||||
code = lang_item["code"]
|
||||
translated_lex_file = f"{target_directory_path}{hl('PackageName').lower()}_{code}.lex"
|
||||
# Only do it if the lex file is missing
|
||||
if not os.path.exists(translated_lex_file):
|
||||
translated_dict = []
|
||||
for lex_item in eng_lex_dict:
|
||||
# Get it from ChatGPT
|
||||
translated_text = get_translation(
|
||||
lex_item["text"], lang_item["language"]
|
||||
)
|
||||
line = "'" + item["id"] + "' => " + '"' + translated_text + '",\n'
|
||||
file.write(line)
|
||||
# print(f"{item['id']} => {item['text']}\n")
|
||||
else:
|
||||
print(
|
||||
f"Skipping the creation of {translated_lex_file} as it exists already"
|
||||
)
|
||||
translated_dict.append({"id": lex_item["id"], "text": translated_text})
|
||||
print(f"Writing out lex file for {lang_item['code']}")
|
||||
with open(translated_lex_file, "w") as file:
|
||||
for item in translated_dict:
|
||||
# escape any nasties
|
||||
translated_text = (
|
||||
item["text"]
|
||||
.replace("\\", r"\\")
|
||||
.replace('"', r"\"")
|
||||
.replace("'", r"\'")
|
||||
)
|
||||
line = "'" + item["id"] + "' => " + '"' + translated_text + '",\n'
|
||||
file.write(line)
|
||||
# print(f"{item['id']} => {item['text']}\n")
|
||||
else:
|
||||
print(
|
||||
f"Skipping the creation of {translated_lex_file} as it exists already"
|
||||
)
|
||||
quit() # end of the program
|
||||
|
Reference in New Issue
Block a user