-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
57 lines (48 loc) · 2.06 KB
/
Copy pathutils.py
File metadata and controls
57 lines (48 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import re
import os
from pathlib import Path
def sanitize_filename(name):
"""Removes or replaces characters invalid for filenames."""
# Remove leading/trailing whitespace
name = name.strip()
# Replace spaces with underscores
name = name.replace(" ", "_")
# Remove characters that are problematic in filenames
# (adjust the set of characters based on strictness needed)
invalid_chars = r'[\\/*?:"<>|]'
name = re.sub(invalid_chars, '', name)
# Prevent empty names or names consisting only of invalid chars
if not name:
name = "untitled_schema"
# Ensure it doesn't end with a dot or space (problematic on some systems)
name = re.sub(r'[. ]+$', '', name)
# Handle reserved names if necessary (e.g., CON, PRN on Windows, though less relevant on Linux)
# For simplicity, we'll skip this for now on Linux target.
if not name: # Check again after potential removal of trailing dots/spaces
name = "untitled_schema_final"
return name
def validate_schema_paths(schema_data):
"""
Checks if all source paths and the destination path in a schema exist.
Args:
schema_data (dict): Dictionary containing 'sources' (list) and 'destination' (str).
Returns:
bool: True if all paths are valid, False otherwise.
list: A list of invalid paths found.
"""
invalid_paths = []
if not schema_data:
return False, ["Schema data is empty"]
sources = schema_data.get('sources', [])
destination = schema_data.get('destination')
if not isinstance(sources, list):
invalid_paths.append("Sources format is invalid")
sources = [] # Prevent further errors
if not destination or not isinstance(destination, str):
invalid_paths.append("Destination path is missing or invalid")
elif not Path(destination).exists():
invalid_paths.append(f"Destination: {destination}")
for src in sources:
if not isinstance(src, str) or not Path(src).exists():
invalid_paths.append(f"Source: {src}")
return not invalid_paths, invalid_paths