Maily debugging info to find reason nfsshare works but diskusage fails

This commit is contained in:
Brian Read 2024-09-13 16:36:57 +01:00
parent 436bc6d2f2
commit 20d5828803
7 changed files with 371 additions and 369 deletions

View File

@ -1,7 +1,7 @@
%# %#
%# Generated by SM2Gen version:0.8 Chameleon version:3.8.1 On Python:3.12.3 ( at 2024-09-12 19:46 %# Generated by SM2Gen version:0.8 Chameleon version:3.8.1 On Python:3.12.3 at 2024-09-13 15:33
%# %#
<div id="DiskUsage-PARAMS" class="partial DiskUsage-PARAMS"> <div id="Diskusage-PARAMS" class="partial Diskusage-PARAMS">
<script> <script>
window.onload = function() { window.onload = function() {
SelectInput(); SelectInput();
@ -11,7 +11,7 @@
%= l('du Hello PARAMS'); %= l('du Hello PARAMS');
</h2> </h2>
% my $btn = l('APPLY'); % my $btn = l('APPLY');
%= form_for "DiskUsaged" => (method => 'POST') => begin %= form_for "Diskusaged" => (method => 'POST') => begin
% param 'trt' => ${prefix}_data->{trt} unless param 'trt'; % param 'trt' => ${prefix}_data->{trt} unless param 'trt';
%= hidden_field 'trt' => ${prefix}_data->{trt} %= hidden_field 'trt' => ${prefix}_data->{trt}
@ -27,15 +27,15 @@
%= l('Wed Sep 11 19:01:00 BST 2024') %= l('Wed Sep 11 19:01:00 BST 2024')
</p> </p>
<pre> <pre>
Filesystem Size Used Avail Use% Mounted on Filesystem Size Used Avail Use% Mounted on
/dev/mapper/main-root 29G 4.8G 24G 17% / /dev/mapper/main-root 29G 4.8G 24G 17% /
devtmpfs 2.0G 0 2.0G 0% /dev devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 12K 2.0G 1% /dev/shm tmpfs 2.0G 12K 2.0G 1% /dev/shm
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
tmpfs 2.0G 648K 2.0G 1% /run tmpfs 2.0G 648K 2.0G 1% /run
/dev/sda1 497M 111M 386M 23% /boot' /dev/sda1 497M 111M 386M 23% /boot'
</pre> </pre>
<br /><table class="sme-border TableSort sme-table"> <br /><table class="sme-border TableSort sme-table">

View File

@ -1,9 +1,9 @@
% layout 'default', title => "Sme server 2 - Disk Usage", share_dir => './'; % layout 'default', title => "Sme server 2 - Disk Usage", share_dir => './';
%# %#
%# Generated by SM2Gen version:0.8 Chameleon version:3.8.1 On Python:3.12.3 ( at 2024-09-12 19:46 %# Generated by SM2Gen version:0.8 Chameleon version:3.8.1 On Python:3.12.3 at 2024-09-13 15:33
%# %#
% content_for 'module' => begin % content_for 'module' => begin
<div id="module" class="module DiskUsage-panel"> <div id="module" class="module Diskusage-panel">
% if ($config->{debug} == 1) { % if ($config->{debug} == 1) {
<p> <p>

View File

@ -119,9 +119,9 @@
</Table> </Table>
<Preformatted><![CDATA[ <Preformatted><![CDATA[
<pre> <pre>
${value}' ${value}'
</pre> </pre>
]]> ]]>
</Preformatted> </Preformatted>

View File

@ -15,7 +15,7 @@
'Paragraph1': 'To create a simple web page for your company, fill \n\t in the fields below and click onCreate.', 'Paragraph1': 'To create a simple web page for your company, fill \n\t in the fields below and click onCreate.',
'Paragraph2': 'You can leave any field blank if you do not need it.', 'Paragraph2': 'You can leave any field blank if you do not need it.',
'Paragraph3': 'The text that you enter below will be line wrapped \n\t for a nicer appearance in your web page. Leave a blank line \n\t whenever you want to start a new paragraph. If you need \n\t to force a line break without starting a new paragraph (for \n\t example after each line of a mailing address), then type \n\t the four-character sequence', 'Paragraph3': 'The text that you enter below will be line wrapped \n\t for a nicer appearance in your web page. Leave a blank line \n\t whenever you want to start a new paragraph. If you need \n\t to force a line break without starting a new paragraph (for \n\t example after each line of a mailing address), then type \n\t the four-character sequence',
'Paragraph4': 'Do not use this optionif you have already customized your web site, since it will \n\t overwrite the \'index.htm\' file in your web site directory.', 'Paragraph4': 'Do not use this option if you have already customized your web site, since it will \n\t overwrite the \'index.htm\' file in your web site directory.',
'Input1': { 'Input1': {
'Type': 'Text', 'Type': 'Text',
'Value': '', 'Value': '',

View File

@ -1,48 +1,47 @@
{ {
'PackageName': 'DiskUsage', PackageName: 'Diskusage',
'prefix': 'du', prefix: 'du',
'MenuHeading': 'Miscellaneous', MenuHeading: 'Miscellaneous',
'MenuDescription': 'Disk Usage', MenuDescription: 'Disk Usage',
'MenuNavigation': '2000 400', MenuNavigation: '2000 400',
'firstPanel': 'PARAMS', firstPanel: 'PARAMS',
'signalEvent': 'smeserver-diskusage-update', signalEvent: 'smeserver-diskusage-update',
'html': [ { html: [{
'Name': 'params', Name: 'params',
'route': 'PARAMS', route: 'PARAMS',
'Header': 'Disk usage', Header: 'Disk usage',
'SubHeader': 'Manage DiskUsage settings:', SubHeader: 'Manage DiskUsage settings:',
'Paragraph1': '&nbsp', Paragraph1: '&nbsp',
'Paragraph2': 'Wed Sep 11 19:01:00 BST 2024', Paragraph2: 'Wed Sep 11 19:01:00 BST 2024',
'Preformatted1': 'Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/main-root 29G 4.8G 24G 17% /\ndevtmpfs 2.0G 0 2.0G 0% /dev\ntmpfs 2.0G 12K 2.0G 1% /dev/shm\ntmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup\ntmpfs 2.0G 648K 2.0G 1% /run\n/dev/sda1 497M 111M 386M 23% /boot', Preformatted1: 'Filesystem Size Used Avail Use% Mounted on\n/dev/mapper/main-root 29G 4.8G 24G 17% /\ndevtmpfs 2.0G 0 2.0G 0% /dev\ntmpfs 2.0G 12K 2.0G 1% /dev/shm\ntmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup\ntmpfs 2.0G 648K 2.0G 1% /run\n/dev/sda1 497M 111M 386M 23% /boot',
'Table1': { Table1: {
'Type': 'Table', Type: 'Table',
'TableControl': 'Table1', TableControl: 'Table1',
'TopHeadings': [ TopHeadings: [
'I-bays', 'I-bays',
'Usage', 'Usage',
'Path' 'Path'
], ],
'Columns': [ Columns: [
'Table1-I-bays', 'Table1-I-bays',
'Table1-Usage', 'Table1-Usage',
'Table1-Path' 'Table1-Path'
] ]
}, },
'Paragraph3': '&nbsp', Paragraph3: '&nbsp',
'Table2': { Table2: {
'Type': 'Table', Type: 'Table',
'TableControl': 'Table2', TableControl: 'Table2',
'TopHeadings': [ TopHeadings: [
'Users', 'Users',
'Usage', 'Usage',
'Path' 'Path'
], ],
'Columns': [ Columns: [
'Table2-Users', 'Table2-Users',
'Table2-Usage', 'Table2-Usage',
'Table2-Path' 'Table2-Path'
] ]
} }
} }]
]
} }

View File

@ -149,7 +149,7 @@ def insert_spaces_before_caps(text):
def save_to_json5(data, output_filename, package_name, header, sub_header): def save_to_json5(data, output_filename, package_name, header, sub_header):
"""Save extracted data to a JSON5 file with a specific structure.""" """Save extracted data to a JSON5 file with a specific structure."""
# Generate prefix from uppercase letters in PackageName made into lowercase # Generate prefix from uppercase letters in PackageName made into lowercase
prefix = ''.join(re.findall(r'[A-Z]', package_name)).lowercase() prefix = ''.join(re.findall(r'[A-Z]', package_name)).lower()
# Prepare structured html list # Prepare structured html list
structured_html = [] structured_html = []

659
sm2gen.py
View File

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