2024-04-09 11:30:13 +02:00
|
|
|
import json5
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
from chameleon import PageTemplateFile,PageTemplate
|
|
|
|
import pkg_resources
|
|
|
|
import xml.etree.ElementTree as ET
|
2024-04-10 09:41:31 +02:00
|
|
|
import re
|
2024-04-25 17:23:07 +02:00
|
|
|
import os
|
2024-04-26 10:26:38 +02:00
|
|
|
import pkg_resources
|
|
|
|
from datetime import datetime
|
|
|
|
import xml.etree.ElementTree as ET
|
2024-04-09 11:30:13 +02:00
|
|
|
|
2024-05-06 11:05:43 +02:00
|
|
|
#
|
|
|
|
#To Do
|
|
|
|
# 1. Get routing working
|
|
|
|
# 2. Deal with ? and ! as sentence ends in text-format
|
|
|
|
# 3. Make lexical scan work with double or single quotes (if possible)
|
|
|
|
|
2024-05-05 10:31:10 +02:00
|
|
|
SME2Gen_version = '0.7'
|
2024-04-09 11:30:13 +02:00
|
|
|
json5_dict: dict = {}
|
|
|
|
json5_html_list: list = []
|
|
|
|
|
|
|
|
def parse_json(json_obj, prefix=''):
|
|
|
|
structured_list = []
|
|
|
|
if isinstance(json_obj, dict):
|
|
|
|
for k, v in json_obj.items():
|
|
|
|
new_key = f"{prefix}.{k}" if prefix else k
|
|
|
|
structured_list.extend(parse_json(v, new_key))
|
|
|
|
elif isinstance(json_obj, list):
|
|
|
|
for i, v in enumerate(json_obj):
|
|
|
|
new_key = f"{prefix}[{i}]"
|
|
|
|
structured_list.extend(parse_json(v, new_key))
|
|
|
|
else:
|
|
|
|
structured_list.append(f"{prefix}: {json_obj}")
|
|
|
|
return structured_list
|
|
|
|
|
|
|
|
def json5_to_list(filename):
|
|
|
|
with open(filename, 'r') as file:
|
|
|
|
data = json5.load(file)
|
|
|
|
return parse_json(data)
|
|
|
|
|
|
|
|
def json5_to_pandas(filename):
|
|
|
|
with open(filename, 'r') as file:
|
|
|
|
data = json5.load(file)
|
|
|
|
print (data)
|
|
|
|
return data.json_normalize(data)
|
|
|
|
|
|
|
|
def json5_to_dict(filename):
|
|
|
|
with open(filename, 'r') as file:
|
|
|
|
data = json5.load(file)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def rec_print(data, prefix=''):
|
|
|
|
# Check if this item is a dictionary.
|
|
|
|
if isinstance(data, dict):
|
|
|
|
for key, val in data.items():
|
|
|
|
rec_print(val, f"{prefix}.{key}")
|
|
|
|
# Check if this item is a list.
|
|
|
|
elif isinstance(data, list):
|
|
|
|
for idx, val in enumerate(data):
|
|
|
|
rec_print(val, f"{prefix}[{idx}]")
|
|
|
|
# If neither, it's a basic type.
|
|
|
|
else:
|
|
|
|
print(f"{prefix}: {data}")
|
|
|
|
|
|
|
|
def find_item(nested_dict, target_key):
|
|
|
|
for key, val in nested_dict.items():
|
|
|
|
if key == target_key:
|
|
|
|
return val
|
|
|
|
elif isinstance(val, dict):
|
|
|
|
result = find_item(val, target_key)
|
|
|
|
if result is not None:
|
2024-04-25 17:23:07 +02:00
|
|
|
return result
|
2024-04-09 11:30:13 +02:00
|
|
|
|
2024-04-25 17:23:07 +02:00
|
|
|
def find_dicts_with_key(data, target_key):
|
2024-04-09 11:30:13 +02:00
|
|
|
results = []
|
2024-04-25 17:23:07 +02:00
|
|
|
if isinstance(data, dict):
|
|
|
|
if target_key in data:
|
|
|
|
results.append(data)
|
|
|
|
for val in data.values():
|
|
|
|
if isinstance(val, (dict, list)):
|
|
|
|
results.extend(find_dicts_with_key(val, target_key))
|
|
|
|
elif isinstance(data, list):
|
|
|
|
for item in data:
|
|
|
|
if isinstance(item, (dict, list)):
|
|
|
|
results.extend(find_dicts_with_key(item, target_key))
|
|
|
|
return results
|
|
|
|
|
|
|
|
def find_values_with_key(data, target_key):
|
|
|
|
results = []
|
|
|
|
if isinstance(data, dict):
|
|
|
|
if target_key in data:
|
|
|
|
results.append(data[target_key])
|
|
|
|
for val in data.values():
|
|
|
|
if isinstance(val, (dict, list)):
|
|
|
|
results.extend(find_values_with_key(val, target_key))
|
|
|
|
elif isinstance(data, list):
|
|
|
|
for item in data:
|
|
|
|
if isinstance(item, (dict, list)):
|
|
|
|
results.extend(find_values_with_key(item, target_key))
|
2024-04-09 11:30:13 +02:00
|
|
|
return results
|
|
|
|
|
|
|
|
def lint_json5(filename):
|
|
|
|
try:
|
|
|
|
with open(filename, 'r') as file:
|
|
|
|
data = file.read()
|
|
|
|
json5.loads(data)
|
|
|
|
print(f"{filename} as JSON5 data is valid")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"{filename} as JSON5 data is invalid")
|
|
|
|
print("Error:", str(e))
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
def flatten_hash_of_lists(hash_of_lists):
|
|
|
|
flattened = {}
|
|
|
|
for key, value in hash_of_lists.items():
|
|
|
|
if isinstance(value, list):
|
|
|
|
for i, item in enumerate(value):
|
|
|
|
new_key = f"{key}_{i}" # Appending index to the key to maintain uniqueness
|
|
|
|
flattened[new_key] = item
|
|
|
|
else:
|
|
|
|
flattened[key] = value
|
|
|
|
return flattened
|
|
|
|
|
|
|
|
def hl(keyname):
|
|
|
|
# Return highest level value for the keyname
|
|
|
|
if keyname in json5_dict:
|
|
|
|
return json5_dict[keyname]
|
|
|
|
else:
|
|
|
|
print(f"{keyname} not found in JSON5 - top level")
|
|
|
|
return 'None'
|
|
|
|
|
|
|
|
def get_all_routes():
|
|
|
|
route_list = [html_block.get('route') for html_block in json5_dict.get('html', [])]
|
|
|
|
return route_list
|
|
|
|
|
|
|
|
def lc_get_all_routes():
|
|
|
|
# All routes in lower case
|
|
|
|
route_list = [html_block.get('route').lower() for html_block in json5_dict.get('html', [])]
|
|
|
|
return route_list
|
2024-04-25 17:23:07 +02:00
|
|
|
|
|
|
|
def has_file_been_modified(file_path):
|
|
|
|
# Get the file's creation time and last modification time in Unix timestamp
|
|
|
|
creation_time = os.path.getctime(file_path)
|
|
|
|
last_modification_time = os.path.getmtime(file_path)
|
2024-04-26 10:26:38 +02:00
|
|
|
print(f"{creation_time}*{last_modification_time}")
|
|
|
|
quit()
|
2024-04-25 17:23:07 +02:00
|
|
|
|
|
|
|
# Compare the creation time and last modification time
|
|
|
|
if creation_time < last_modification_time:
|
|
|
|
return True # File has been modified after creation
|
|
|
|
else:
|
|
|
|
return False # File has not been modified after creation
|
|
|
|
|
2024-04-09 11:30:13 +02:00
|
|
|
def parse_xml_to_dict(xml_file):
|
|
|
|
# Parse the XML file
|
|
|
|
tree = ET.parse(xml_file)
|
|
|
|
root = tree.getroot()
|
|
|
|
|
|
|
|
xml_dict = {} # Initialize an empty dictionary to store the data
|
|
|
|
|
|
|
|
# Iterate through the XML tree and extract data
|
|
|
|
for elem in root:
|
|
|
|
tag = elem.tag
|
|
|
|
if elem.text:
|
|
|
|
xml_dict[tag] = elem.text
|
|
|
|
else:
|
|
|
|
cdata_content = elem.find('.//').text # Extract CDATA text
|
|
|
|
xml_dict[tag] = cdata_content
|
|
|
|
|
|
|
|
return xml_dict
|
|
|
|
|
2024-04-10 09:41:31 +02:00
|
|
|
def deduplicate_array(arr):
|
|
|
|
# Convert the array to a set to remove duplicates
|
|
|
|
unique_set = set(arr)
|
|
|
|
|
|
|
|
# Convert the set back to a list to maintain the order
|
|
|
|
deduplicated_list = list(unique_set)
|
|
|
|
|
|
|
|
return deduplicated_list
|
|
|
|
|
2024-04-25 17:23:07 +02:00
|
|
|
def get_db_fields():
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_table_control_data():
|
|
|
|
return find_values_with_key(json5_html_list,'TableControl')
|
2024-04-10 09:41:31 +02:00
|
|
|
|
2024-05-05 10:31:10 +02:00
|
|
|
def format_text(text):
|
|
|
|
#
|
|
|
|
# Turn a piece of text into something a bit better formatted - spaces after full stop and comma and also capitalise sentences
|
|
|
|
#
|
|
|
|
# Create a list to hold the formatted sentences
|
|
|
|
formatted_sentences = []
|
|
|
|
# Split the text into sentences
|
|
|
|
sentences = text.split(".")
|
|
|
|
words = sentences[0].split(" ")
|
|
|
|
#print(len(sentences))
|
|
|
|
# Deal with one capitalised word
|
|
|
|
if sentences[0].isupper() and len(sentences) == 1 and len(words) == 1:
|
|
|
|
return sentences[0].capitalize()
|
|
|
|
else:
|
|
|
|
for sentence in sentences:
|
|
|
|
#print(sentence)
|
|
|
|
# and splt into sub phrases, based on comma
|
|
|
|
formatted_phrases = []
|
|
|
|
phrases = sentence.split(",")
|
|
|
|
|
|
|
|
for phrase in phrases:
|
|
|
|
#print(phrase)
|
|
|
|
phrase = phrase.lstrip()
|
|
|
|
formatted_words = []
|
|
|
|
words = phrase.split(' ')
|
|
|
|
|
|
|
|
for i,word in enumerate(words):
|
|
|
|
#print(i,word)
|
|
|
|
# Check if the word is fully uppercase or not the first
|
|
|
|
word = word.lstrip()
|
|
|
|
if word.isupper() or i != 0:
|
|
|
|
formatted_words.append(word)
|
|
|
|
else:
|
|
|
|
# Capitalize the word
|
|
|
|
formatted_words.append(word.capitalize())
|
|
|
|
|
|
|
|
# Join the formatted words back together for this phrase
|
|
|
|
formatted_phrase = ' '.join(formatted_words).lstrip()
|
|
|
|
formatted_phrases.append(formatted_phrase)
|
|
|
|
# and sentence
|
|
|
|
formatted_sentence = ", ".join(formatted_phrases).lstrip()
|
|
|
|
formatted_sentences.append(formatted_sentence)
|
|
|
|
# Join the formatted sentences back together
|
|
|
|
formatted_text = ". ".join(formatted_sentences).lstrip()
|
|
|
|
return formatted_text
|
|
|
|
|
2024-04-09 11:30:13 +02:00
|
|
|
if __name__ == "__main__":
|
2024-04-28 13:03:06 +02:00
|
|
|
try:
|
|
|
|
chameleon_version = pkg_resources.get_distribution("Chameleon").version
|
|
|
|
except pkg_resources.DistributionNotFound:
|
|
|
|
chameleon_version = "Version information not available"
|
|
|
|
python_version = sys.version
|
|
|
|
python_version = python_version[:8]
|
|
|
|
current_datetime = datetime.now()
|
|
|
|
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M")
|
|
|
|
|
|
|
|
strVersion = "SME2Gen version:"+SME2Gen_version+" Chameleon version:"+chameleon_version+" On Python:"+python_version+" at "+formatted_datetime
|
|
|
|
|
|
|
|
json5_dict: dict = {}
|
|
|
|
json5_html_list: list = []
|
|
|
|
|
|
|
|
print(f"SM2 code from JSON5 - {strVersion}")
|
|
|
|
|
2024-04-09 11:30:13 +02:00
|
|
|
filename = '/home/brianr/clients/SM2/SM2Gen/nfsshare.json5'
|
|
|
|
|
|
|
|
# Command line parameters
|
|
|
|
parser = argparse.ArgumentParser(description="SM2Gen")
|
|
|
|
parser.add_argument('-f', '--filename', help='Specify a filename for the JSON5 file', default=filename)
|
2024-04-28 13:03:06 +02:00
|
|
|
parser.add_argument('-nco', '--noController', help='Stop it creating a controller file', default="no")
|
2024-04-09 11:30:13 +02:00
|
|
|
parser.add_argument('-nh', '--noHtml', help='Stop it creating html files(s)', default="no")
|
2024-04-10 09:41:31 +02:00
|
|
|
parser.add_argument('-nl', '--noLang', help='Stop it creating language localise files(s)', default="no")
|
2024-04-28 13:03:06 +02:00
|
|
|
parser.add_argument('-ncu', '--noCust', help='Stop it creating Custom controller file', default="no")
|
2024-04-09 11:30:13 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
filename = args.filename
|
2024-04-10 09:41:31 +02:00
|
|
|
print(f"JSON5 from {filename} with noController={args.noController}, noHtml={args.noHtml} and noLang={args.noLang}") #Not yet activated
|
2024-04-09 11:30:13 +02:00
|
|
|
|
|
|
|
# check syntax of JSON5
|
|
|
|
lint_json5(filename);
|
|
|
|
|
|
|
|
# Get dict of it all
|
|
|
|
json5_dict = json5_to_dict(filename)
|
|
|
|
|
|
|
|
# Get dict of just the html bit
|
|
|
|
json5_html_list = json5_dict['html']
|
|
|
|
|
|
|
|
#Identify message
|
|
|
|
print(f"\nGenerating mojo panels for {hl('PackageName')}")
|
|
|
|
print( "-----------------------------------")
|
|
|
|
|
|
|
|
# Routes for each panel
|
|
|
|
routes = get_all_routes();
|
|
|
|
lc_routes =lc_get_all_routes();
|
|
|
|
|
2024-04-10 09:41:31 +02:00
|
|
|
#File names
|
|
|
|
controller_file = 'Targets/'+hl('PackageName')+'.pm'
|
2024-04-25 17:23:07 +02:00
|
|
|
custom_controller_file = 'Targets/'+hl('PackageName')+'-Custom.pm'
|
2024-04-28 13:03:06 +02:00
|
|
|
|
|
|
|
# see if it has been modified by developer - this did not work - the modified by was the same as the creation time/date
|
2024-04-26 10:26:38 +02:00
|
|
|
#if has_file_been_modified(custom_controller_file):
|
|
|
|
# custom_controller_file = custom_controller_file+'.new'
|
2024-04-28 13:03:06 +02:00
|
|
|
|
|
|
|
# Call it .new if one is already there (and may have been editted by the developer)
|
2024-04-26 10:26:38 +02:00
|
|
|
if os.path.exists(custom_controller_file):
|
2024-04-25 17:23:07 +02:00
|
|
|
custom_controller_file = custom_controller_file+'.new'
|
2024-04-10 09:41:31 +02:00
|
|
|
layout_file = 'Targets/'+hl('PackageName')+'.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'
|
2024-04-25 17:23:07 +02:00
|
|
|
tablecontrols = get_table_control_data() #arrays of hashes used to drive rows in tables
|
2024-04-10 09:41:31 +02:00
|
|
|
|
2024-04-09 11:30:13 +02:00
|
|
|
#Generate controller file
|
|
|
|
try:
|
|
|
|
controller_template = PageTemplateFile("Templates/controller.pm.tem")
|
2024-04-25 17:23:07 +02:00
|
|
|
dbentries = get_db_fields() #Params which correspond to Db fields
|
2024-04-09 11:30:13 +02:00
|
|
|
try:
|
2024-04-28 13:03:06 +02:00
|
|
|
controller_perl = controller_template.render(version=strVersion,
|
|
|
|
tablecontrols=tablecontrols,
|
|
|
|
dbentries=dbentries,
|
|
|
|
**json5_dict,
|
|
|
|
panels=routes,
|
|
|
|
lcPackageName=json5_dict['PackageName'].lower()
|
|
|
|
)
|
2024-04-10 09:41:31 +02:00
|
|
|
with open(controller_file, 'w') as file:
|
2024-04-09 11:30:13 +02:00
|
|
|
file.write(controller_perl)
|
2024-04-10 09:41:31 +02:00
|
|
|
print(f"{controller_file} controller generated ok")
|
2024-04-09 11:30:13 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon controller render error occurred: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon controller template error occurred: {e}")
|
2024-04-25 17:23:07 +02:00
|
|
|
|
|
|
|
#Generate Custom controller file
|
|
|
|
try:
|
|
|
|
custom_controller_template = PageTemplateFile("Templates/custom.pm.tem")
|
|
|
|
try:
|
2024-04-28 13:03:06 +02:00
|
|
|
custom_controller_perl = custom_controller_template.render(version=strVersion,
|
|
|
|
panels=routes,
|
|
|
|
tablecontrols=tablecontrols
|
|
|
|
)
|
2024-04-25 17:23:07 +02:00
|
|
|
# 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"An chameleon custom controller render error occurred: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon custom controller template error occurred: {e}")
|
2024-04-09 11:30:13 +02:00
|
|
|
|
|
|
|
#generate Layout file
|
|
|
|
layout_template = PageTemplateFile("Templates/layout.html.ep.tem")
|
|
|
|
try:
|
|
|
|
try:
|
2024-04-28 13:03:06 +02:00
|
|
|
layout_mojo = layout_template.render(version=strVersion,**json5_dict,conditions=routes)
|
2024-04-10 09:41:31 +02:00
|
|
|
with open(layout_file, 'w') as file:
|
2024-04-09 11:30:13 +02:00
|
|
|
file.write(layout_mojo)
|
2024-04-24 17:09:23 +02:00
|
|
|
print(f"{layout_file} mojo template layout file generated ok")
|
2024-04-09 11:30:13 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon render on layout file error occurred: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon template layout file 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')
|
2024-04-10 09:41:31 +02:00
|
|
|
i = 0
|
2024-04-09 11:30:13 +02:00
|
|
|
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:
|
2024-04-28 13:03:06 +02:00
|
|
|
partial_mojo_template = partial_template.render(version=strVersion,**partial_mojo_context)
|
2024-04-10 09:41:31 +02:00
|
|
|
with open( partial_files[i], 'w') as file:
|
2024-04-09 11:30:13 +02:00
|
|
|
file.write(partial_mojo_template)
|
2024-04-24 17:09:23 +02:00
|
|
|
print(f"{partial_files[i]} mojo template generated ok - phase 1")
|
2024-04-09 11:30:13 +02:00
|
|
|
except Exception as e:
|
2024-04-24 17:09:23 +02:00
|
|
|
print(f"An chameleon render error on partial file {html['route']} occurred: {e}")
|
2024-04-09 11:30:13 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon html {html['route']} 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:
|
2024-04-28 13:03:06 +02:00
|
|
|
control_html = control_template.render(version=strVersion,**inner_html,prefix=prefix_is)
|
2024-04-09 11:30:13 +02:00
|
|
|
all_controls_html = all_controls_html + control_html
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon render on partial file control {inner_html['Name']} error occurred: {e}")
|
|
|
|
except Exception as e:
|
|
|
|
print(f"An 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:
|
2024-04-28 13:03:06 +02:00
|
|
|
simple_control_html = simple_control_template.render(version=strVersion,value=inner_html,prefix=prefix_is)
|
2024-04-09 11:30:13 +02:00
|
|
|
all_controls_html = all_controls_html + simple_control_html
|
|
|
|
except Exception as e:
|
2024-04-24 17:09:23 +02:00
|
|
|
print(f"An chameleon render on partial file control {html_control} error occurred: {e}")
|
2024-04-09 11:30:13 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An chameleon template partial file control {html_control} 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."
|
2024-04-10 09:41:31 +02:00
|
|
|
with open(partial_files[i], 'r') as file:
|
2024-04-09 11:30:13 +02:00
|
|
|
lines = file.readlines()
|
|
|
|
index = next((i for i, line in enumerate(lines) if "%# Inputs etc in here." in line), len(lines))
|
|
|
|
|
|
|
|
# Insert the string at the specified index
|
|
|
|
lines.insert(index+1, all_controls_html + '\n')
|
|
|
|
|
|
|
|
# Write the modified content back to the file
|
2024-04-10 09:41:31 +02:00
|
|
|
with open(partial_files[i], 'w') as file:
|
2024-04-09 11:30:13 +02:00
|
|
|
file.writelines(lines)
|
2024-04-10 09:41:31 +02:00
|
|
|
print(f"Content modified and saved to {partial_files[i]}")
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# Now generate the <name>.en file
|
|
|
|
# Look through the generated files for the /l[\s|(]['|"](.*)['|"]\)/ strings.
|
|
|
|
|
|
|
|
# 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',
|
2024-05-03 19:08:44 +02:00
|
|
|
string_lib = [] #Array of dicts
|
2024-04-10 09:41:31 +02:00
|
|
|
for lex_message in all_strings:
|
|
|
|
# If has a prefix - leave it for left hand side but delete it for the right
|
2024-05-03 19:08:44 +02:00
|
|
|
# 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
|
2024-05-05 10:31:10 +02:00
|
|
|
# Checkif it starts with the prefix (any case|)
|
|
|
|
if lex_message.lower().startswith(hl('prefix').lower()):
|
2024-04-10 09:41:31 +02:00
|
|
|
left_str = lex_message
|
|
|
|
right_str = lex_message[len(hl('prefix'))+1:]
|
2024-05-05 10:31:10 +02:00
|
|
|
# And take out any "_", map to " "
|
2024-04-10 09:41:31 +02:00
|
|
|
else:
|
|
|
|
left_str = hl('prefix')+"_"+lex_message
|
|
|
|
right_str = lex_message
|
2024-05-05 10:31:10 +02:00
|
|
|
right_str = right_str.replace("_"," ")
|
2024-05-06 11:05:43 +02:00
|
|
|
print(f"Right:{right_str}")
|
2024-05-05 10:31:10 +02:00
|
|
|
right_str = format_text(right_str)
|
2024-05-03 19:08:44 +02:00
|
|
|
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)
|
2024-04-10 09:41:31 +02:00
|
|
|
#And write it to lex file
|
2024-05-03 19:08:44 +02:00
|
|
|
# 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}")
|
2024-04-10 09:41:31 +02:00
|
|
|
with open( lex_file, 'w') as file:
|
|
|
|
file.write(lex_all)
|
2024-05-03 19:08:44 +02:00
|
|
|
#and then play the strings back into the partials and the layout file
|
2024-05-05 10:31:10 +02:00
|
|
|
print("..and feed the lex string names back into other files")
|
2024-05-03 19:08:44 +02:00
|
|
|
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'
|
2024-05-06 11:05:43 +02:00
|
|
|
file_content = file_content.replace("l('"+original_str+"')", "l('"+left_str+"')")
|
2024-05-03 19:08:44 +02:00
|
|
|
# and write it back
|
|
|
|
with open(filename, 'w') as file:
|
|
|
|
file.write(file_content)
|
2024-05-05 10:31:10 +02:00
|
|
|
print(f"Write out modified:{filename}")
|
2024-05-03 19:08:44 +02:00
|
|
|
|
2024-04-10 09:41:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-09 11:30:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|