Add in entries for other mojo input tags

This commit is contained in:
2024-09-15 12:06:34 +01:00
parent 3b6e1930a0
commit 4d7cc1f076
18 changed files with 821 additions and 754 deletions

699
sm2gen.py
View File

@@ -306,371 +306,384 @@ def convert_lex_to_dict(pairs_string):
if __name__ == "__main__":
try:
chameleon_version = pkg_resources.get_distribution("Chameleon").version
except pkg_resources.DistributionNotFound:
chameleon_version = "Version information not available"
python_version = sys.version
version_pattern = r'(\d{1,3}\.\d{1,3}\.\d{1,3})'
version_match = re.search(version_pattern, python_version)
python_version = version_match.group(0) if version_match else 'Unknown'
current_datetime = datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M")
strVersion = (
"SM2Gen version:"
+ SME2Gen_version
+ " Chameleon version:"
+ chameleon_version
+ " On Python:"
+ python_version
+ " at "
+ formatted_datetime
)
try:
chameleon_version = pkg_resources.get_distribution("Chameleon").version
except pkg_resources.DistributionNotFound:
chameleon_version = "Version information not available"
python_version = sys.version
version_pattern = r"(\d{1,3}\.\d{1,3}\.\d{1,3})"
version_match = re.search(version_pattern, python_version)
python_version = version_match.group(0) if version_match else "Unknown"
current_datetime = datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M")
strVersion = (
"SM2Gen version:"
+ SME2Gen_version
+ " Chameleon version:"
+ chameleon_version
+ " On Python:"
+ python_version
+ " at "
+ formatted_datetime
)
json5_dict: dict = {}
json5_html_list: list = []
json5_dict: dict = {}
json5_html_list: list = []
print(f"SM2 code from JSON5 - {strVersion}")
print(f"SM2 code from JSON5 - {strVersion}")
home_dir = "/home/brianr/clients/SM2/SM2Gen/"
json_filename = f"{home_dir}/json5/nfsshare.json5" #CreateStarterWebsite.json5"
home_dir = "/home/brianr/clients/SM2/SM2Gen/"
json_filename = f"{home_dir}/json5/nfsshare.json5" # CreateStarterWebsite.json5"
# read open ai key from ini file
# Check if the file exists
if os.path.exists(ini_file_path):
# Create a configparser object and read the file
config = configparser.ConfigParser()
config.read(ini_file_path)
# read open ai key from ini file
# Check if the file exists
if os.path.exists(ini_file_path):
# Create a configparser object and read the file
config = configparser.ConfigParser()
config.read(ini_file_path)
# Read the value of "OPENAI_API_KEY"
if "OPENAI_API_KEY" in config["smegit"]:
OPENAI_API_KEY = config["smegit"]["OPENAI_API_KEY"]
# print("API Key:", OPENAI_API_KEY)
client = OpenAI(api_key=OPENAI_API_KEY)
else:
print("OPENAI_API_KEY not found in the configuration file.")
else:
print("Configuration file not found at:", file_path)
# Read the value of "OPENAI_API_KEY"
if "OPENAI_API_KEY" in config["smegit"]:
OPENAI_API_KEY = config["smegit"]["OPENAI_API_KEY"]
# print("API Key:", OPENAI_API_KEY)
client = OpenAI(api_key=OPENAI_API_KEY)
else:
print("OPENAI_API_KEY not found in the configuration file.")
else:
print("Configuration file not found at:", file_path)
# Command line parameters - not in use
parser = argparse.ArgumentParser(description="SM2Gen")
parser.add_argument(
"-f",
"--filename",
help="Specify a filename for the JSON5 file",
default=json_filename,
)
parser.add_argument(
"-nco",
"--noController",
help="Stop it creating a controller file",
default="yes",
)
parser.add_argument(
"-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="yes",
)
parser.add_argument(
"-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)
# Command line parameters - not in use
parser = argparse.ArgumentParser(description="SM2Gen")
parser.add_argument(
"-f",
"--filename",
help="Specify a filename for the JSON5 file",
default=json_filename,
)
parser.add_argument(
"-nco",
"--noController",
help="Stop it creating a controller file",
default="yes",
)
parser.add_argument(
"-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="yes",
)
parser.add_argument(
"-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 syntax of JSON5
lint_json5(json_filename)
# 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)
# Get dict of it all
json5_dict = json5_to_dict(json_filename)
# check syntax of JSON5
lint_json5(json_filename)
# Get dict of just the html bit
json5_html_list = json5_dict["html"]
# Get dict of it all
json5_dict = json5_to_dict(json_filename)
# Identify message
print(f"\nGenerating mojo panels for {hl('PackageName')}")
print("-----------------------------------")
# Get dict of just the html bit
json5_html_list = json5_dict["html"]
# Routes for each panel
routes = get_all_routes()
lc_routes = lc_get_all_routes()
# Identify message
print(f"\nGenerating mojo panels for {hl('PackageName')}")
print("-----------------------------------")
# File names
# 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")+"/"
# Routes for each panel
routes = get_all_routes()
lc_routes = lc_get_all_routes()
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 = target_directory_path + hl("PackageName").lower() + ".html.ep"
partial_files = list()
for panel in routes:
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
# File names
# 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") + "/"
# print(strVersion,tablecontrols,routes)
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 = target_directory_path + hl("PackageName").lower() + ".html.ep"
partial_files = list()
for panel in routes:
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
# Generate controller file
try:
controller_template = PageTemplateFile("Templates/controller.pm.tem",CHAMELEON_DEBUG="true")
dbentries = get_db_fields() # Params which correspond to Db fields
try:
controller_perl = controller_template.render(
version=strVersion,
tablecontrols=tablecontrols,
dbentries=dbentries,
**json5_dict,
panels=routes,
lcPackageName=json5_dict["PackageName"].lower(),
)
with open(controller_file, "w") as file:
file.write(controller_perl)
print(f"{controller_file} controller generated ok")
except Exception as e:
print(f"A Chameleon controller render error occurred: {e}")
except Exception as e:
print(f"A Chameleon controller template error occurred: {e}")
# print(strVersion,tablecontrols,routes)
# Generate Custom controller file
try:
custom_controller_template = PageTemplateFile("Templates/custom.pm.tem")
try:
custom_controller_perl = custom_controller_template.render(
version=strVersion, panels=routes, tablecontrols=tablecontrols
)
# We must be careful to not overwrite the custom file if the developer has already written to it - TBD
with open(custom_controller_file, "w") as file:
file.write(custom_controller_perl)
print(f"{custom_controller_file} custom controller generated ok")
except Exception as e:
print(f"A Chameleon custom controller render error occurred: {e}")
except Exception as e:
print(f"A Chameleon custom controller template error occurred: {e}")
# Generate controller file
try:
controller_template = PageTemplateFile(
"Templates/controller.pm.tem", CHAMELEON_DEBUG="true"
)
dbentries = get_db_fields() # Params which correspond to Db fields
try:
controller_perl = controller_template.render(
version=strVersion,
tablecontrols=tablecontrols,
dbentries=dbentries,
**json5_dict,
panels=routes,
lcPackageName=json5_dict["PackageName"].lower(),
)
with open(controller_file, "w") as file:
file.write(controller_perl)
print(f"{controller_file} controller generated ok")
except Exception as e:
print(f"A Chameleon controller render error occurred: {e}")
except Exception as e:
print(f"A Chameleon controller template error occurred: {e}")
# generate Layout file
layout_template = PageTemplateFile("Templates/layout.html.ep.tem")
try:
try:
layout_mojo = layout_template.render(
version=strVersion, **json5_dict, conditions=routes
)
with open(layout_file, "w") as file:
file.write(layout_mojo)
print(f"{layout_file} mojo template layout file generated ok")
except Exception as e:
print(f"A Chameleon render on layout file error occurred: {e}")
except Exception as e:
print(f"A Chameleon template layout file error occurred: {e}")
# Generate Custom controller file
try:
custom_controller_template = PageTemplateFile("Templates/custom.pm.tem")
try:
custom_controller_perl = custom_controller_template.render(
version=strVersion, panels=routes, tablecontrols=tablecontrols
)
# We must be careful to not overwrite the custom file if the developer has already written to it - TBD
with open(custom_controller_file, "w") as file:
file.write(custom_controller_perl)
print(f"{custom_controller_file} custom controller generated ok")
except Exception as e:
print(f"A Chameleon custom controller render error occurred: {e}")
except Exception as e:
print(f"A Chameleon custom controller template error occurred: {e}")
# Generate a partial file for each of the entries in the html list
# Pull in the template code for each of the input types
# html_controls = json5_to_dict('Templates/html_controls.html.ep.tem')
html_controls = parse_xml_to_dict("Templates/html_controls.html.ep.xml")
i = 0
for html in json5_html_list:
# Generate a mojo template file, and then add in the controls
# main file first
try:
partial_template = PageTemplateFile("Templates/partial.html.ep.tem")
partial_mojo_context = {**json5_dict, **html}
try:
partial_mojo_template = partial_template.render(
version=strVersion, **partial_mojo_context
)
with open(partial_files[i], "w") as file:
file.write(partial_mojo_template)
print(f"{partial_files[i]} mojo template generated ok - phase 1")
except Exception as e:
print(
f"A Chameleon render error on partial file {html['route']} occurred: {e}"
)
except Exception as e:
print(f"A Chameleon html {html['route']} error occurred: {e}")
# generate Layout file
layout_template = PageTemplateFile("Templates/layout.html.ep.tem")
try:
try:
layout_mojo = layout_template.render(
version=strVersion, **json5_dict, conditions=routes
)
with open(layout_file, "w") as file:
file.write(layout_mojo)
print(f"{layout_file} mojo template layout file generated ok")
except Exception as e:
print(f"A Chameleon render on layout file error occurred: {e}")
except Exception as e:
print(f"A Chameleon template layout file error occurred: {e}")
# Now generate the controls from the rest of the entries in the dict.
all_controls_html = ""
prefix_is = hl("prefix")
for html_control in html:
inner_html = html[html_control]
if isinstance(inner_html, dict):
try:
control_template = PageTemplate(html_controls[inner_html["Type"]])
try:
control_html = control_template.render(
version=strVersion, **inner_html, prefix=prefix_is
)
all_controls_html = all_controls_html + control_html
except Exception as e:
print(
f"A Chameleon render on partial file control {inner_html['Name']} error occurred: {e}"
)
except Exception as e:
print(
f"A Chameleon render on partial file control {inner_html['Name']} error occurred: {e}"
)
else:
# just a simple entry - name less numerics is type
html_Type = "".join(char for char in html_control if not char.isdigit())
try:
simple_control_template = PageTemplate(html_controls[html_Type])
try:
simple_control_html = simple_control_template.render(
version=strVersion, value=inner_html, prefix=prefix_is
)
all_controls_html = all_controls_html + simple_control_html
except Exception as e:
print(
f"A Chameleon render on partial file control {html_control} error occurred: {e}"
)
except Exception as e:
print(
f"A Chameleon template partial file control {html_control} error occurred: {e}"
)
# Generate a partial file for each of the entries in the html list
# Pull in the template code for each of the input types
# html_controls = json5_to_dict('Templates/html_controls.html.ep.tem')
html_controls = parse_xml_to_dict("Templates/html_controls.html.ep.xml")
i = 0
for html in json5_html_list:
# Generate a mojo template file, and then add in the controls
# main file first
try:
partial_template = PageTemplateFile("Templates/partial.html.ep.tem")
partial_mojo_context = {**json5_dict, **html}
try:
partial_mojo_template = partial_template.render(
version=strVersion, **partial_mojo_context
)
with open(partial_files[i], "w") as file:
file.write(partial_mojo_template)
print(f"{partial_files[i]} mojo template generated ok - phase 1")
except Exception as e:
print(
f"A Chameleon render error on partial file {html['route']} occurred: {e}"
)
except Exception as e:
print(f"A Chameleon html {html['route']} error occurred: {e}")
# Now insert it into the partial file in the correct place.
# Read in the text file and split at "%# Inputs etc in here."
with open(partial_files[i], "r") as file:
lines = file.readlines()
index = next(
(i for i, line in enumerate(lines) if "%# Inputs etc in here." in line),
len(lines),
)
# Now generate the controls from the rest of the entries in the dict.
all_controls_html = ""
prefix_is = hl("prefix")
for html_control in html:
inner_html = html[html_control]
if isinstance(inner_html, dict):
try:
control_template = PageTemplate(html_controls[inner_html["Type"]])
try:
control_html = control_template.render(
version=strVersion, **inner_html, prefix=prefix_is
)
all_controls_html = all_controls_html + control_html
except Exception as e:
print(
f"A Chameleon render on partial file control {inner_html['Name']} error occurred: {e}"
)
except Exception as e:
print(
f"A Chameleon render on partial file control {inner_html['Name']} error occurred: {e}"
)
else:
# just a simple entry - name less numerics is type
html_Type = "".join(char for char in html_control if not char.isdigit())
try:
simple_control_template = PageTemplate(html_controls[html_Type])
try:
simple_control_html = simple_control_template.render(
version=strVersion, value=inner_html, prefix=prefix_is
)
all_controls_html = all_controls_html + simple_control_html
except Exception as e:
print(
f"A Chameleon render on partial file control {html_control} error occurred: {e}"
)
except Exception as e:
print(
f"A Chameleon template partial file control {html_control} error occurred: {e}"
)
# Insert the string at the specified index
lines.insert(index + 1, all_controls_html + "\n")
# Now insert it into the partial file in the correct place.
# Read in the text file and split at "%# Inputs etc in here."
with open(partial_files[i], "r") as file:
lines = file.readlines()
index = next(
(i for i, line in enumerate(lines) if "%# Inputs etc in here." in line),
len(lines),
)
# Write the modified content back to the file
with open(partial_files[i], "w") as file:
file.writelines(lines)
print(f"Content modified and saved to {partial_files[i]}")
i += 1
# Insert the string at the specified index
lines.insert(index + 1, all_controls_html + "\n")
# Now generate the <name>.en file
# Look through the generated files for the /l[\s|(]['|"](.*)['|"]\)/ strings.
# Write the modified content back to the file
with open(partial_files[i], "w") as file:
file.writelines(lines)
print(f"Content modified and saved to {partial_files[i]}")
i += 1
# create a combined list of all the files
all_files = [controller_file, layout_file] + partial_files
all_strings = []
for filename in all_files:
with open(filename, "r") as file:
file_content = file.read()
# Define the regular expression pattern to match the strings you want to extract
pattern = r"l[\s|(][\'|\"](.*)[\'|\"]\)"
# Use re.findall to extract all occurrences of the pattern from the file content
extracted_strings = re.findall(pattern, file_content)
all_strings = all_strings + extracted_strings
# Take out any duplicates
all_strings = deduplicate_array(all_strings)
# '<prefix>_english-message' => 'English Message',
string_lib = [] # Array of dicts
for lex_message in all_strings:
# If has a prefix - leave it for left hand side but delete it for the right
# If has no prefix - add one for left hand side but and leave it for the right
# Map all spaces to "_" on left hand side
# amd truncate it to max five words
original_str = lex_message
# Checkif it starts with the prefix (any case|)
if lex_message.lower().startswith(hl("prefix").lower()):
left_str = lex_message
right_str = lex_message[len(hl("prefix")) + 1 :]
# And take out any "_", map to " "
else:
left_str = hl("prefix") + "_" + lex_message
right_str = lex_message
right_str = right_str.replace("_", " ")
# print(f"Right:{right_str}")
right_str = format_text(right_str)
left_str = left_str.replace(" ", "_")
words = left_str.split("_")[:6]
left_str = "_".join(words)
next_lex_str = {"orig": original_str, "left": left_str, "right": right_str}
string_lib.append(next_lex_str)
# And write it to lex file
# Now process them one by one into the lexical file
lex_all = ""
for lex_str in string_lib:
lex_all += f"'{lex_str['left']}' => '{lex_str['right']}',\n"
print(f"Writing {lex_file}")
with open(lex_file, "w") as file:
file.write(lex_all)
# and then play the strings back into the partials and the layout file
print("..and feed the lex string names back into other files")
for filename in all_files:
with open(filename, "r") as file:
file_content = file.read()
# Scan through
for item in string_lib:
original_str = item["orig"]
left_str = item["left"]
right_str = item["right"]
# Replace all occurrences of original string with left string in 'contents'
file_content = file_content.replace(
"l('" + original_str + "')", "l('" + left_str + "')"
)
# and write it back
with open(filename, "w") as file:
file.write(file_content)
print(f"Write out modified:{filename}")
# Now generate the <name>.en file
# Look through the generated files for the /l[\s|(]['|"](.*)['|"]\)/ strings.
# 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"]
)
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
# create a combined list of all the files
all_files = [controller_file, layout_file] + partial_files
all_strings = []
for filename in all_files:
with open(filename, "r") as file:
file_content = file.read()
# Define the regular expression pattern to match the strings you want to extract
pattern = r"l[\s|(][\'|\"](.*)[\'|\"]\)"
# Use re.findall to extract all occurrences of the pattern from the file content
extracted_strings = re.findall(pattern, file_content)
all_strings = all_strings + extracted_strings
# Take out any duplicates
all_strings = deduplicate_array(all_strings)
# '<prefix>_english-message' => 'English Message',
string_lib = [] # Array of dicts
for lex_message in all_strings:
# If has a prefix - leave it for left hand side but delete it for the right
# If has no prefix - add one for left hand side but and leave it for the right
# Map all spaces to "_" on left hand side
# amd truncate it to max five words
original_str = lex_message
# Checkif it starts with the prefix (any case|)
if lex_message.lower().startswith(hl("prefix").lower()):
left_str = lex_message
right_str = lex_message[len(hl("prefix")) + 1 :]
# And take out any "_", map to " "
else:
left_str = hl("prefix") + "_" + lex_message
right_str = lex_message
right_str = right_str.replace("_", " ")
# print(f"Right:{right_str}")
right_str = format_text(right_str)
left_str = left_str.replace(" ", "_")
words = left_str.split("_")[:6]
left_str = "_".join(words)
next_lex_str = {"orig": original_str, "left": left_str, "right": right_str}
string_lib.append(next_lex_str)
# And write it to lex file
# Now process them one by one into the lexical file
lex_all = ""
for lex_str in string_lib:
lex_all += f"'{lex_str['left']}' => '{lex_str['right']}',\n"
print(f"Writing {lex_file}")
with open(lex_file, "w") as file:
file.write(lex_all)
# and then play the strings back into the partials and the layout file
print("..and feed the lex string names back into other files")
for filename in all_files:
with open(filename, "r") as file:
file_content = file.read()
# Scan through
for item in string_lib:
original_str = item["orig"]
left_str = item["left"]
right_str = item["right"]
# Replace all occurrences of original string with left string in 'contents'
file_content = file_content.replace(
"l('" + original_str + "')", "l('" + left_str + "')"
)
# and write it back
with open(filename, "w") as file:
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
# 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"]
)
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