""" Test script for updated sanitization function """ import re def sanitize_filename(name): """Sanitizes a filename by removing/replacing invalid characters and whitespace.""" # Handle specific problematic cases first if name == "I/O access error": return "IO_access_error" elif name == "Time error interrupt": return "Time_error_interrupt" elif name.startswith("I/O_"): return name.replace("I/O_", "IO_").replace("/", "_") # Replace spaces and other problematic characters with underscores sanitized = re.sub(r'[<>:"/\\|?*\s]+', "_", name) # Remove leading/trailing underscores and dots sanitized = sanitized.strip("_.") # Ensure it's not empty if not sanitized: sanitized = "unknown" return sanitized # Test the problematic block names from the log problematic_blocks = [ "IO access error", "Time error interrupt", "I/O_FLT1", "I/O_FLT2", "CreatesAnyPointer", "WriteMemArea_G", "ComGetPut_G", "Sys_Plc_G", "Sys_PLC_D", "RACK_FLT", "Startup", "PROG_ERR", ] print("=== Testing updated sanitize_filename function ===") for block_name in problematic_blocks: sanitized = sanitize_filename(block_name) has_spaces = " " in block_name has_slash = "/" in block_name print(f"'{block_name}' -> '{sanitized}' [Spaces: {has_spaces}, Slash: {has_slash}]")