Add in check domains, and highlight any files created .new in red

This commit is contained in:
2024-12-01 15:29:08 +00:00
parent 918f49eaf2
commit b0393cfe8a
12 changed files with 131 additions and 68 deletions

View File

@@ -434,6 +434,45 @@ def check_file_version(filename,force_Files=False):
logger.warning(f"An error occurred: {traceback.format_exc()}")
return filename
def highlight_occurrences(text, keystring='new', highlight_color='red'):
"""
Highlights all occurrences of `keystring` in `text` with the specified `highlight_color`.
Parameters:
text (str): The original text where occurrences will be highlighted.
keystring (str): The substring to be highlighted.
highlight_color (str): The color name for highlighting. Defaults to 'yellow'.
Supported colors: black, red, green, yellow, blue, magenta, cyan, white.
Returns:
str: The modified text with highlighted occurrences.
"""
# ANSI escape codes for colors
colors = {
'black': '\033[30m',
'red': '\033[31m',
'green': '\033[32m',
'yellow': '\033[33m',
'blue': '\033[34m',
'magenta': '\033[35m',
'cyan': '\033[36m',
'white': '\033[37m',
'reset': '\033[0m'
}
if highlight_color not in colors:
raise ValueError(f"Unsupported color '{highlight_color}'. Supported colors: {', '.join(colors.keys())}")
# Escape keystring to handle any special regex characters
escaped_keystring = re.escape(keystring)
# Define the replacement pattern with the chosen color
colored_keystring = f"{colors[highlight_color]}{keystring}{colors['reset']}"
# Use regex to replace all occurrences, case-sensitive
highlighted_text = re.sub(escaped_keystring, colored_keystring, text)
return highlighted_text
def convert_lex_to_dict(pairs_string):
# Splitting pairs string by comma and newline, excluding empty lines
@@ -630,7 +669,7 @@ if __name__ == "__main__":
)
with open(controller_file, "w") as file:
file.write(controller_perl)
logger.info(f"{controller_file} controller generated ok")
logger.info(f"{highlight_occurrences(controller_file)} controller generated ok")
except Exception as e:
logger.info(f"A Chameleon controller *render* error occurred: {e} {traceback.format_exc()}")
except Exception as e:
@@ -654,7 +693,7 @@ if __name__ == "__main__":
# 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)
logger.info(f"{custom_controller_file} custom controller generated ok")
logger.info(f"{highlight_occurrences(custom_controller_file,'new')} custom controller generated ok")
except Exception as e:
logger.info(f"A Chameleon custom controller *render* error occurred: {e} {traceback.format_exc()}")
except Exception as e:
@@ -670,7 +709,7 @@ if __name__ == "__main__":
)
with open(layout_file, "w") as file:
file.write(layout_mojo)
logger.info(f"{layout_file} mojo template layout file generated ok")
logger.info(f"{highlight_occurrences(layout_file)} mojo template layout file generated ok")
except Exception as e:
logger.info(f"A Chameleon *render* on layout file error occurred: {e}")
except Exception as e:
@@ -697,7 +736,7 @@ if __name__ == "__main__":
)
with open(partial_files[i], "w") as file:
file.write(partial_mojo_template)
logger.info(f"{partial_files[i]} mojo template generated ok - phase 1")
logger.info(f"{highlight_occurrences(partial_files[i])} mojo template generated ok - phase 1")
except Exception as e:
logger.info(
f"A Chameleon render error on partial file {html['route']} occurred: {e}"
@@ -845,7 +884,7 @@ if __name__ == "__main__":
lex_all = ""
for lex_str in string_lib:
lex_all += f"'{lex_str['left']}' => '{lex_str['right']}',\n"
logger.info(f"Writing {lex_file}")
logger.info(f"Writing {highlight_occurrences(lex_file)}")
with open(lex_file, "w") as file:
file.write(f"#\n# Generated by SM2Gen version: {strVersion}\n#\n")
file.write(lex_all)
@@ -866,7 +905,7 @@ if __name__ == "__main__":
# and write it back
with open(filename, "w") as file:
file.write(file_content)
logger.info(f"Write out modified:{filename}")
logger.info(f"Write out modified:{highlight_occurrences(filename)}")
# Now generate all the translated lex files from a list of the languages and codes
# if specifically requested