-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.py
More file actions
99 lines (85 loc) · 3.38 KB
/
Copy pathhelper.py
File metadata and controls
99 lines (85 loc) · 3.38 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
from os import path, listdir
from pathlib import Path
from fnmatch import fnmatch
from config import Config, Target, Filter
from ui import MainWindowTab
import shutil
def is_file_movable(file_path):
try:
# Try to open the file in append mode
with open(file_path, 'a'):
pass
return True
except IOError:
return False
class FileInstance:
def __init__(self, file, config:Config, target:Target):
self.file = file
self.config = config
self._target = target
self.filter = None
self.origin = None
self.target = None
def apply_filter(self, filter:Filter):
self.filter = filter
self.origin = path.join(self._target.path, self.file)
self.target = path.join(self._target.path, filter.folder, self.file)
class FileInstance:
def __init__(self, file, config:Config, target:Target):
self.file = file
self.config = config
self.target = target
self.filter = None
self.origin:Path = Path(target.path) / file
self.target_path:Path = None
def apply_filter(self, filter:Filter):
self.filter = filter
self.target_path = Path(self.target.path) / filter.folder / self.file
def clean_folders(config: Config, window: 'MainWindowTab'):
files: list[FileInstance] = []
window.progress_bar.setValue(0)
window.progress_bar.setMaximum(0)
window.progress_bar.setLabelText('Looking for files to move...')
filters_folders = [filter.folder for filter in config.filters]
for target in config.targets:
if path.isdir(target.path):
for file in listdir(target.path):
full_path = path.join(target.path, file)
if path.isdir(full_path) and file in filters_folders:
continue
file_instance = FileInstance(file, config, target)
files.append(file_instance)
window.progress_bar.setMaximum(len(files))
window.progress_bar.setValue(0)
window.progress_bar.setLabelText('Applying filters...')
for file_instance in files:
matched = False
for filter in config.filters:
for expression in filter.expressions:
if fnmatch(file_instance.file, expression):
file_instance.apply_filter(filter)
matched = True
break
if matched:
break
window.progress_bar.increment()
window.progress_bar.setMaximum(len(files))
window.progress_bar.setValue(0)
window.progress_bar.setLabelText('Moving files...')
moved_count = 0
for file_instance in files:
if file_instance.filter:
target_dir = file_instance.target_path.parent
if not target_dir.exists():
target_dir.mkdir(parents=True, exist_ok=True)
if is_file_movable(file_instance.origin):
shutil.move(file_instance.origin, file_instance.target_path)
else:
print(f"File {file_instance.origin} is currently in use and cannot be moved.")
# file_instance.origin.replace(file_instance.target_path)
moved_count += 1
window.progress_bar.increment()
window.progress_bar.setLabelText(f'Done! Moved {moved_count} files.')
window.progress_bar.setMaximum(100)
window.progress_bar.setValue(100)
window._toggle_all(True)