Lex file creation

This commit is contained in:
Brian Read 2024-04-10 08:41:31 +01:00
parent 2ff5681988
commit 2d3fabcc5b
6 changed files with 128 additions and 36 deletions

View File

@ -87,12 +87,12 @@ sub do_update {
if ($trt eq 'PARAMS'){
#Validate for panel PARAMS
# set $ret = $c->l(''Error message') if invalid'
# set $ret = $c->l('Error message') if invalid'
}
if ($trt eq 'TABLE'){
#Validate for panel TABLE
# set $ret = $c->l(''Error message') if invalid'
# set $ret = $c->l('Error message') if invalid'
}
if ($ret ne "ok"){

View File

@ -15,13 +15,13 @@
%# Inputs etc in here.
<h2>Manage NFS Ibay settings:</h2><p>These parameters will be effective only if the share is enabled. The share is in /home/e-smith/files/ibays/$(STASH:ibayname)/files</p>
<p><span class=label>
%=l('Information Bay name'), class => 'label'
%=l('nfs_Information Bay name'), class => 'label'
</span><span class=data>
%= stash("IbayName"), class => 'data'
</span></p>
<p><span class=label>
%=l('Share owner Group')
%=l('nfs_Share owner Group')
</span><span class=data>
% my @ShareOwnerGrp_options = ['Write = admin, Read = group', 'Write = group, Read = everyone', 'Write = group, Read = group'];
% param 'ShareOwnerGrp' => $nfs_data->{ShareOwnerGrp} unless param 'ShareOwnerGrp';
@ -29,7 +29,7 @@
<br></span> </p>
<p><span class=label>
%=l('Enable the NFS Share')
%=l('nfs_Enable the NFS Share')
</span><span class=data>
% my @EnableNFSshare_options = ['Disabled', 'Enabled'];
% param 'EnableNFSshare' => $nfs_data->{EnableNFSshare} unless param 'EnableNFSshare';
@ -37,7 +37,7 @@
<br></span> </p>
<p><span class=label>
%=l('EnableShare on local network')
%=l('nfs_EnableShare on local network')
</span><span class=data>
% my @ShareOnLocalNetwork_options = ['Disabled', 'Enabled'];
% param 'ShareOnLocalNetwork' => $nfs_data->{ShareOnLocalNetwork} unless param 'ShareOnLocalNetwork';
@ -45,14 +45,14 @@
<br></span> </p>
<p>For writing permissions,allowing the root user and using insecure ports, you must configure a list of one IP per line, being part of the local network(s).</p>
<span class=label>
%=l('NFS Client(s) allowed')
%=l('nfs_NFS Client(s) allowed')
</span><span class=data>
% param 'NFSClientsAllowed' => $nfs_data->{NFSClientsAllowed} unless param 'NFSClientsAllowed';
%= text_area 'NFSClientsAllowed', cols=>40, rows=>5
</span><br>
<p><span class=label>
%=l('File system permissions')
%=l('nfs_File system permissions')
</span><span class=data>
% my @FileSystemPermissions_options = ['Read only', 'Read and Write'];
% param 'FileSystemPermissions' => $nfs_data->{FileSystemPermissions} unless param 'FileSystemPermissions';
@ -60,7 +60,7 @@
<br></span> </p>
<p><span class=label>
%=l('Write (a)synchronously.')
%=l('nfs_Write (a)synchronously.')
</span><span class=data>
% my @WriteAsync_options = ['Synchronous', 'Asynchronous'];
% param 'WriteAsync' => $nfs_data->{WriteAsync} unless param 'WriteAsync';
@ -68,7 +68,7 @@
<br></span> </p>
<p><span class=label>
%=l('Delays the disk writings.')
%=l('nfs_Delays the disk writings.')
</span><span class=data>
% my @DelayWrite_options = ['Write delay', 'No write delay'];
% param 'DelayWrite' => $nfs_data->{DelayWrite} unless param 'DelayWrite';
@ -76,7 +76,7 @@
<br></span> </p>
<p><span class=label>
%=l('Squash the power of users.')
%=l('nfs_Squash the power of users.')
</span><span class=data>
% my @Squash_options = ['All users squash', 'No root squash', 'root squash'];
% param 'Squash' => $nfs_data->{Squash} unless param 'Squash';
@ -84,7 +84,7 @@
<br></span> </p>
<p><span class=label>
%=l('Browse the parent folders')
%=l('nfs_Browse the parent folders')
</span><span class=data>
% my @BrowseParents_options = ['Hide folder', 'Show folder'];
% param 'BrowseParents' => $nfs_data->{BrowseParents} unless param 'BrowseParents';
@ -92,7 +92,7 @@
<br></span> </p>
<p><span class=label>
%=l('Requests on secure ports.')
%=l('nfs_Requests on secure ports.')
</span><span class=data>
% my @SecurePorts_options = ['Secure', 'Insecure'];
% param 'SecurePorts' => $nfs_data->{SecurePorts} unless param 'SecurePorts';
@ -100,14 +100,14 @@
<br></span> </p>
<p>Set the uid and gid if you want all requests appear to be from one user or one group, otherwise leave blank.</p>
<p><span class=label>
%=l('Set the UID.')
%=l('nfs_Set the UID.')
</span><span class=data>
% param 'SetUID' => $nfs_data->{SetUID} unless param 'SetUID';
%= text_field 'SetUID', size => '50', class => 'input' , pattern=>'.*' , placeholder=>'SetUID'
<br></span></p>
<p><span class=label>
%=l('Set the GID.')
%=l('nfs_Set the GID.')
</span><span class=data>
% param 'SetGID' => $nfs_data->{SetGID} unless param 'SetGID';
%= text_field 'SetGID', size => '50', class => 'input' , pattern=>'.*' , placeholder=>'SetGID'

20
Targets/nfsshare_en.lex Normal file
View File

@ -0,0 +1,20 @@
'nfs_APPLY' => 'APPLY'
'nfs_Save' => 'Save'
'nfs_Squash the power of users.' => 'Squash the power of users.'
'nfs Hello PARAMS' => 'Hello PARAMS'
'nfs_Requests on secure ports.' => 'Requests on secure ports.'
'nfs_Error message' => 'Error message'
'nfs_Enable the NFS Share' => 'Enable the NFS Share'
'nfs_NFS data share' => 'NFS data share'
'nfs_Delays the disk writings.' => 'Delays the disk writings.'
'nfs_Browse the parent folders' => 'Browse the parent folders'
'nfs_Write (a)synchronously.' => 'Write (a)synchronously.'
'nfs_NFS Client(s) allowed' => 'NFS Client(s) allowed'
'nfs_EnableShare on local network' => 'EnableShare on local network'
'nfs_Information Bay name' => 'Information Bay name'
'nfs_ok message' => 'ok message'
'nfs_Set the GID.' => 'Set the GID.'
'nfs_Share owner Group' => 'Share owner Group'
'nfs Hello TABLE' => 'Hello TABLE'
'nfs_Set the UID.' => 'Set the UID.'
'nfs_File system permissions' => 'File system permissions'

View File

@ -87,7 +87,7 @@ sub do_update {
<tal:block tal:repeat="condition conditions">
if ($trt eq '${condition}'){
#Validate for panel ${condition}
# set $ret = $c->l(''Error message') if invalid'
# set $ret = $c->l('Error message') if invalid'
}
</tal:block>
if ($ret ne "ok"){

View File

@ -1,7 +1,7 @@
<root>
<Text><![CDATA[
<p><span class=label>
%=l('${Label}'), class => 'label'
%=l('${prefix}_${Label}'), class => 'label'
</span><span class=data>
%= ${Value}, class => 'data'
</span></p>
@ -9,7 +9,7 @@
<Selection><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
% my @${Name}_options = ${Value};
% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';
@ -19,7 +19,7 @@
<Textarea><![CDATA[
<span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';
%= text_area '${Name}', cols=>${cols | 40}, rows=>${rows | 10}
@ -28,7 +28,7 @@
<Date><![CDATA[
<span class=label>
%=$%=l('${Label}')
%=$%=l('${prefix}_${Label}')
</span><span class=data>
%=date_field '${Name}' =>$$${Name}, pattern=>${regexp | .*}
</span><br>
@ -36,7 +36,7 @@
<Textinput><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
% param '${Name}' => $$${prefix}_data->{${Name}} unless param '${Name}';
%= text_field '${Name}', size => '${size | 50}', class => 'input' , pattern=>'${regexp | ".*"}' , placeholder=>'${placeholder | Name}'
@ -54,7 +54,7 @@
<search><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
</span>span></p>
]]>
@ -62,7 +62,7 @@
<url><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
</span>span></p>
]]>
@ -70,7 +70,7 @@
<tel><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
</span>span></p>
]]>
@ -78,7 +78,7 @@
<email><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
</span>span></p>
]]>
@ -86,7 +86,7 @@
<password><![CDATA[
<p><span class=label>
%=l('${Label}')
%=l('${prefix}_${Label}')
</span><span class=data>
</span>span></p>
]]>

View File

@ -5,6 +5,7 @@ import argparse
from chameleon import PageTemplateFile,PageTemplate
import pkg_resources
import xml.etree.ElementTree as ET
import re
version = 0.5
@ -140,6 +141,16 @@ def parse_xml_to_dict(xml_file):
return xml_dict
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
if __name__ == "__main__":
filename = '/home/brianr/clients/SM2/SM2Gen/nfsshare.json5'
@ -148,9 +159,10 @@ if __name__ == "__main__":
parser.add_argument('-f', '--filename', help='Specify a filename for the JSON5 file', default=filename)
parser.add_argument('-nc', '--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")
args = parser.parse_args()
filename = args.filename
print(f"JSON5 from {filename} with noController={args.noController} and noHtml={args.noHtml}")
print(f"JSON5 from {filename} with noController={args.noController}, noHtml={args.noHtml} and noLang={args.noLang}") #Not yet activated
#print("Chameleon version:", chameleon.__version__)
# check syntax of JSON5
@ -172,6 +184,15 @@ if __name__ == "__main__":
routes = get_all_routes();
lc_routes =lc_get_all_routes();
#File names
controller_file = 'Targets/'+hl('PackageName')+'.pm'
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'
#Generate controller file
try:
controller_template = PageTemplateFile("Templates/controller.pm.tem")
@ -181,9 +202,9 @@ if __name__ == "__main__":
#print(controller_perl)
# Map '$ 'to '$' to overcome problem with escaping $ signs
#controller_perl = controller_perl.replace("$ ", "$")
with open('Targets/'+hl('PackageName')+'.pm', 'w') as file:
with open(controller_file, 'w') as file:
file.write(controller_perl)
print("Targets/"+hl('PackageName')+'.pm controller generated ok')
print(f"{controller_file} controller generated ok")
except Exception as e:
print(f"An chameleon controller render error occurred: {e}")
except Exception as e:
@ -196,9 +217,9 @@ if __name__ == "__main__":
layout_mojo = layout_template.render(**json5_dict,conditions=routes)
# Map '$ 'to '$' to overcome problem with escaping $ signs
#layout_mojo = layout_mojo.replace("$ ", "$")
with open('Targets/'+hl('PackageName')+'.html.ep', 'w') as file:
with open(layout_file, 'w') as file:
file.write(layout_mojo)
print("Targets/"+hl('PackageName')+'.html.ep mojo template generated ok')
print(f"{layout_file} mojo template generated ok")
except Exception as e:
print(f"An chameleon render on layout file error occurred: {e}")
#print()
@ -211,6 +232,7 @@ if __name__ == "__main__":
#html_controls = json5_to_dict('Templates/html_controls.html.ep.tem')
html_controls = parse_xml_to_dict('Templates/html_controls.html.ep.xml')
#print(html_controls)
i = 0
for html in json5_html_list:
# Generate a mojo template file, and then add in the controls
# main file first
@ -221,9 +243,9 @@ if __name__ == "__main__":
partial_mojo_template = partial_template.render(**partial_mojo_context)
# Map '$ 'to '$' to overcome problem with escaping $ signs
#partial_mojo_template = partial_mojo_template.replace("$ ", "$")
with open('Targets/_'+hl('prefix')+"_"+html['route']+'.html.ep', 'w') as file:
with open( partial_files[i], 'w') as file:
file.write(partial_mojo_template)
print('Targets/_'+hl('prefix')+"_"+html['route']+'.html.ep mojo template generated ok')
print(f"{partial_files[i]} mojo template generated ok")
except Exception as e:
print(f"An chameleon render on partial file {html['route']} occurred: {e}")
except Exception as e:
@ -275,7 +297,7 @@ if __name__ == "__main__":
# 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('Targets/_'+hl('prefix')+"_"+html['route']+'.html.ep', 'r') as file:
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))
@ -283,9 +305,59 @@ if __name__ == "__main__":
lines.insert(index+1, all_controls_html + '\n')
# Write the modified content back to the file
with open('Targets/_'+hl('prefix')+"_"+html['route']+'.html.ep', 'w') as file:
with open(partial_files[i], 'w') as file:
file.writelines(lines)
print('Content modified and saved to Targets/_'+hl('prefix')+"_"+html['route']+'.html.ep .')
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
print(all_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)
print(len(extracted_strings))
all_strings = all_strings + extracted_strings
print(len(all_strings))
#Take out any duplicates
all_strings = deduplicate_array(all_strings)
print(len(all_strings))
# Now process them one by one into the lexical file
lex_all = "";
# '<prefix>_english-message' => 'English Message',
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 lkeave it for the right
if lex_message.startswith(hl('prefix')):
left_str = lex_message
right_str = lex_message[len(hl('prefix'))+1:]
else:
left_str = hl('prefix')+"_"+lex_message
right_str = lex_message
lex_all += f"'{left_str}' => '{right_str}'\n"
#And write it to lex file
with open( lex_file, 'w') as file:
file.write(lex_all)