From a54c9f1c127df38600997c498f1d9e25b934bafb Mon Sep 17 00:00:00 2001 From: Brian Read Date: Tue, 8 Jul 2025 09:49:14 +0100 Subject: [PATCH] All panels and lex files updating now --- lex_scan.py | 112 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 70 insertions(+), 42 deletions(-) diff --git a/lex_scan.py b/lex_scan.py index fa780e3..19c8955 100644 --- a/lex_scan.py +++ b/lex_scan.py @@ -68,55 +68,83 @@ def extract_title_prefix(controller_path): ) sys.exit(1) return prefix + +def find_matching_files_variable_part(input_string, directory): + # Extract the first alphanumeric part from the input string + match = re.match(r"([A-Za-z0-9]+)", input_string) + if not match: + return [] + variable_part = match.group(1) + + # Try matching the full variable_part, then progressively remove last characters + for length in range(len(variable_part), 1, -1): + sub_part = variable_part[:length] + matching_files = [] + for fname in os.listdir(directory): + name, ext = os.path.splitext(fname) + if name.startswith(sub_part): # match with extra characters allowed + matching_files.append(os.path.join(directory, fname)) + if matching_files: + return matching_files + return [] + def scan_application_files(system, panel, prefix, scan_general=False): - extracted_strings = {} - - full_base_path = get_full_base_path(system) + extracted_strings = {} - # Controller file - controller_path = os.path.join(full_base_path, "lib/SrvMngr/Controller", f"{panel}.pm") - logger.info(f"Scanning controller file: {controller_path}") - scan_file_for_lexical_strings(controller_path, prefix, extracted_strings, scan_general) - - #Controller file custom code - controller_custom_path = os.path.join(full_base_path, "lib/SrvMngr/Controller", f"{panel}-Custom.pm") - logger.info(f"Scanning Custom controller file: {controller_custom_path}") - scan_file_for_lexical_strings(controller_custom_path, prefix, extracted_strings, scan_general) - + full_base_path = get_full_base_path(system) - # Template files - themes = ["default", "AdminLTE"] - for theme in themes: - template_base_path = os.path.join(full_base_path, "themes", theme, "templates") - panel_template_path = os.path.join(template_base_path, f"{panel.lower()}.html.ep") - logger.info(f"Scanning panel template file: {panel_template_path}") - scan_file_for_lexical_strings(panel_template_path, prefix, extracted_strings, scan_general) + # Controller file + controller_path = os.path.join(full_base_path, "lib/SrvMngr/Controller", f"{panel}.pm") + logger.info(f"Scanning controller file: {controller_path}") + scan_file_for_lexical_strings(controller_path, prefix, extracted_strings, scan_general) - # Scan partials - partials_dir = os.path.join(template_base_path, "partials") - if os.path.exists(partials_dir): - for filename in os.listdir(partials_dir): - # Only scan partial files that match the pattern __.html.ep - if filename.startswith(f"_{prefix.lower()}_") and filename.endswith(".html.ep"): - partial_path = os.path.join(partials_dir, filename) - logger.info(f"Scanning partial template file: {partial_path}") - scan_file_for_lexical_strings(partial_path, prefix, extracted_strings, scan_general) + #Controller file custom code + controller_custom_path = os.path.join(full_base_path, "lib/SrvMngr/Controller", f"{panel}-Custom.pm") + logger.info(f"Scanning Custom controller file: {controller_custom_path}") + scan_file_for_lexical_strings(controller_custom_path, prefix, extracted_strings, scan_general) - # Deduplicate lists of dicts in extracted_strings - for key, value in extracted_strings.items(): - if isinstance(value, list) and value and isinstance(value[0], dict): - # Deduplicate list of dicts using JSON serialization - seen = set() - deduped = [] - for d in value: - ser = json.dumps(d, sort_keys=True) - if ser not in seen: - seen.add(ser) - deduped.append(d) - extracted_strings[key] = deduped + # Template files + themes = ["default", "AdminLTE"] + for theme in themes: + template_base_path = os.path.join(full_base_path, "themes", theme, "templates") + if panel in ['Backup','Yum','Bugreport']: + #find the extra layout type files that these use (they do not have partials) + template_files = find_matching_files_variable_part(panel.lower(),template_base_path) +# print(f"Matching template files: {panel.lower()!r} -> Matches: {[os.path.basename(m) for m in template_files]}") + for file_path in template_files: + panel_template_path = os.path.join(template_base_path, f"{file_path}") + logger.warning(f"Scanning panel template file: {panel_template_path}") + scan_file_for_lexical_strings(panel_template_path, prefix, extracted_strings, scan_general) + else: + panel_template_path = os.path.join(template_base_path, f"{panel.lower()}.html.ep") + logger.info(f"Scanning panel template file: {panel_template_path}") + scan_file_for_lexical_strings(panel_template_path, prefix, extracted_strings, scan_general) - return extracted_strings + # Scan partials + partials_dir = os.path.join(template_base_path, "partials") + if os.path.exists(partials_dir): + for filename in os.listdir(partials_dir): + # Only scan partial files that match the pattern __.html.ep + if filename.startswith(f"_{prefix.lower()}_") and filename.endswith(".html.ep"): + partial_path = os.path.join(partials_dir, filename) + logger.info(f"Scanning partial template file: {partial_path}") + scan_file_for_lexical_strings(partial_path, prefix, extracted_strings, scan_general) + + # Deduplicate lists of dicts in extracted_strings + for key, value in extracted_strings.items(): + if isinstance(value, list) and value and isinstance(value[0], dict): + # Deduplicate list of dicts using JSON serialization + seen = set() + deduped = [] + for d in value: + ser = json.dumps(d, sort_keys=True) + if ser not in seen: + seen.add(ser) + deduped.append(d) + extracted_strings[key] = deduped + + return extracted_strings def scan_file_for_lexical_strings(filepath, prefix, extracted_strings_dict, scan_general): if not os.path.exists(filepath):