104 lines
3.1 KiB
Python
104 lines
3.1 KiB
Python
"""
|
|
Test script for path validation and sanitization functions
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
|
|
|
|
def sanitize_filename(name):
|
|
"""Sanitizes a filename by removing/replacing invalid characters and whitespace."""
|
|
# 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
|
|
|
|
|
|
def sanitize_path(path):
|
|
"""Sanitizes a path by ensuring it doesn't contain problematic whitespace."""
|
|
# Normalize the path and remove any trailing/leading whitespace
|
|
normalized = os.path.normpath(path.strip())
|
|
return normalized
|
|
|
|
|
|
def validate_export_path(path):
|
|
"""Validates that an export path is suitable for TIA Portal."""
|
|
if not path:
|
|
return False, "La ruta está vacía"
|
|
|
|
# Check for problematic characters or patterns
|
|
if any(char in path for char in '<>"|?*'):
|
|
return False, f"La ruta contiene caracteres no válidos: {path}"
|
|
|
|
# Check for excessive whitespace
|
|
if path != path.strip():
|
|
return False, f"La ruta contiene espacios al inicio o final: '{path}'"
|
|
|
|
# Check for multiple consecutive spaces
|
|
if " " in path:
|
|
return False, f"La ruta contiene espacios múltiples consecutivos: '{path}'"
|
|
|
|
# Check path length (Windows limitation)
|
|
if len(path) > 250:
|
|
return False, f"La ruta es demasiado larga ({len(path)} caracteres): {path}"
|
|
|
|
return True, "OK"
|
|
|
|
|
|
# Test cases
|
|
test_names = [
|
|
"IO access error",
|
|
"CreatesAnyPointer",
|
|
"WriteMemArea_G",
|
|
"Block with spaces",
|
|
"Block<>with:invalid|chars",
|
|
" Block with leading and trailing spaces ",
|
|
"Block with multiple spaces",
|
|
"",
|
|
]
|
|
|
|
test_paths = [
|
|
"C:\\normal\\path",
|
|
"C:\\path with spaces\\subdir",
|
|
" C:\\path with leading space",
|
|
"C:\\path with trailing space ",
|
|
"C:\\path with multiple spaces\\subdir",
|
|
"C:\\path<with>invalid:chars|in\\subdir",
|
|
"",
|
|
]
|
|
|
|
print("=== Testing sanitize_filename ===")
|
|
for name in test_names:
|
|
sanitized = sanitize_filename(name)
|
|
print(f"'{name}' -> '{sanitized}'")
|
|
|
|
print("\n=== Testing sanitize_path ===")
|
|
for path in test_paths:
|
|
sanitized = sanitize_path(path)
|
|
print(f"'{path}' -> '{sanitized}'")
|
|
|
|
print("\n=== Testing validate_export_path ===")
|
|
for path in test_paths:
|
|
sanitized = sanitize_path(path)
|
|
is_valid, msg = validate_export_path(sanitized)
|
|
print(f"'{sanitized}' -> Valid: {is_valid}, Message: {msg}")
|
|
|
|
print("\n=== Test specific problematic block names ===")
|
|
problematic_blocks = ["IO access error", "CreatesAnyPointer", "WriteMemArea_G"]
|
|
base_path = "D:\\Export\\Test"
|
|
|
|
for block_name in problematic_blocks:
|
|
sanitized_name = sanitize_filename(block_name)
|
|
full_path = sanitize_path(
|
|
os.path.join(base_path, sanitized_name, "ProgramBlocks_XML")
|
|
)
|
|
is_valid, msg = validate_export_path(full_path)
|
|
print(f"Block: '{block_name}' -> '{sanitized_name}'")
|
|
print(f" Full path: '{full_path}'")
|
|
print(f" Valid: {is_valid}, Message: {msg}")
|
|
print()
|