-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
193 lines (134 loc) · 3.86 KB
/
Copy pathutils.py
File metadata and controls
193 lines (134 loc) · 3.86 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
====================================================
Universal File Converter
Utility Functions
====================================================
"""
from __future__ import annotations
import os
import re
import shutil
import tempfile
import zipfile
from pathlib import Path
from typing import Iterable
from config import OUTPUT_DIR
def sanitize_filename(filename: str) -> str:
"""
Remove invalid characters from a filename.
"""
filename = Path(filename).name
filename = re.sub(r'[<>:"/\\|?*]', "_", filename)
filename = re.sub(r"\s+", "_", filename)
return filename
def ensure_directory(directory: str | Path) -> Path:
"""
Create a directory if it does not exist.
"""
directory = Path(directory)
directory.mkdir(parents=True, exist_ok=True)
return directory
def get_unique_filename(output_path: str | Path) -> Path:
"""
Prevent accidental overwrite by generating a unique filename.
"""
output_path = Path(output_path)
if not output_path.exists():
return output_path
stem = output_path.stem
suffix = output_path.suffix
parent = output_path.parent
counter = 1
while True:
candidate = parent / f"{stem}_{counter}{suffix}"
if not candidate.exists():
return candidate
counter += 1
def create_temp_directory() -> Path:
"""
Create a temporary working directory.
"""
return Path(tempfile.mkdtemp())
def cleanup_temp_directory(directory: str | Path) -> None:
"""
Delete a temporary directory safely.
"""
directory = Path(directory)
if directory.exists():
shutil.rmtree(directory, ignore_errors=True)
def create_zip(files: Iterable[str | Path], zip_name: str) -> Path:
"""
Create a ZIP archive from multiple files.
"""
ensure_directory(OUTPUT_DIR)
zip_path = OUTPUT_DIR / zip_name
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as archive:
for file in files:
file = Path(file)
if file.exists():
archive.write(file, arcname=file.name)
return zip_path
def file_size_mb(file_path: str | Path) -> float:
"""
Return file size in MB.
"""
size = Path(file_path).stat().st_size
return round(size / (1024 * 1024), 2)
def delete_file(file_path: str | Path) -> None:
"""
Delete a file if it exists.
"""
file_path = Path(file_path)
if file_path.exists():
file_path.unlink()
def clear_output_directory() -> None:
"""
Remove all files from the output directory.
"""
ensure_directory(OUTPUT_DIR)
for item in OUTPUT_DIR.iterdir():
if item.is_file():
item.unlink()
elif item.is_dir():
shutil.rmtree(item)
def list_output_files() -> list[Path]:
"""
Return all converted files.
"""
ensure_directory(OUTPUT_DIR)
return sorted([file for file in OUTPUT_DIR.iterdir() if file.is_file()])
def open_output_directory() -> None:
"""
Open output directory in the system file explorer.
"""
path = str(OUTPUT_DIR.resolve())
if os.name == "nt":
os.startfile(path)
elif os.name == "posix":
os.system(f'xdg-open "{path}"')
def reset_application() -> None:
"""
Reset application generated files.
"""
clear_output_directory()
def cleanup_old_files(
directory: str | Path,
max_age_hours: int = 24,
) -> None:
"""
Delete files older than max_age_hours.
"""
import time
directory = Path(directory)
if not directory.exists():
return
current_time = time.time()
for file in directory.iterdir():
if not file.is_file():
continue
age = (current_time - file.stat().st_mtime) / 3600
if age >= max_age_hours:
try:
file.unlink()
except Exception:
pass