-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolder_manager.py
More file actions
113 lines (84 loc) · 2.98 KB
/
Copy pathfolder_manager.py
File metadata and controls
113 lines (84 loc) · 2.98 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Folder Manager Module
Manages event folders and file operations.
"""
import os
import shutil
from typing import List
def list_event_folders(year_dir: str) -> List[str]:
"""
List all event folders in a directory.
Args:
year_dir: The directory to search for event folders
Returns:
List of event folder names (not full paths)
"""
if not os.path.exists(year_dir):
return []
if not os.path.isdir(year_dir):
return []
folders = []
for item in os.listdir(year_dir):
item_path = os.path.join(year_dir, item)
# Skip hidden folders and files
if item.startswith('.'):
continue
if os.path.isdir(item_path):
folders.append(item)
return folders
def create_event_folder(year_dir: str, name: str) -> str:
"""
Create a new event folder.
Args:
year_dir: The directory where the event folder should be created
name: The name of the event folder
Returns:
The full path to the created folder
Raises:
ValueError: If the folder name is invalid
FileExistsError: If the folder already exists
"""
if not name or name.strip() == '':
raise ValueError("Folder name cannot be empty")
# Clean the folder name
name = name.strip()
# Check for invalid characters
invalid_chars = ['/', '\\', ':', '*', '?', '"', '<', '>', '|']
for char in invalid_chars:
if char in name:
raise ValueError(f"Folder name contains invalid character: {char}")
folder_path = os.path.join(year_dir, name)
if os.path.exists(folder_path):
raise FileExistsError(f"Folder already exists: {name}")
os.makedirs(folder_path, exist_ok=True)
return folder_path
def move_photo(src: str, dest_folder: str) -> str:
"""
Move a photo to a destination folder.
Args:
src: Source file path
dest_folder: Destination folder path
Returns:
The new path of the moved file
Raises:
FileNotFoundError: If source file doesn't exist
ValueError: If destination is not a directory
"""
if not os.path.exists(src):
raise FileNotFoundError(f"Source file not found: {src}")
if not os.path.exists(dest_folder):
raise ValueError(f"Destination folder does not exist: {dest_folder}")
if not os.path.isdir(dest_folder):
raise ValueError(f"Destination is not a directory: {dest_folder}")
filename = os.path.basename(src)
dest_path = os.path.join(dest_folder, filename)
# Handle filename conflicts
if os.path.exists(dest_path):
base, ext = os.path.splitext(filename)
counter = 1
while os.path.exists(dest_path):
new_filename = f"{base}_{counter}{ext}"
dest_path = os.path.join(dest_folder, new_filename)
counter += 1
shutil.move(src, dest_path)
return dest_path