-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinbox_cleaner.py
More file actions
98 lines (79 loc) · 3.35 KB
/
Copy pathinbox_cleaner.py
File metadata and controls
98 lines (79 loc) · 3.35 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
#!/usr/bin/env python3
import os
import shutil
from pathlib import Path
from datetime import datetime
from config import INBOX_DIR, VAULT_DIR
from file_processor.processor_factory import ProcessorFactory
from file_processor.utils import STATS, format_file_size
class InboxCleaner:
"""Clean up empty directories and hidden files in the inbox after processing"""
def __init__(self, inbox_dir: Path):
self.inbox_dir = Path(inbox_dir)
if not self.inbox_dir.exists():
raise ValueError(f"Inbox directory does not exist: {self.inbox_dir}")
def is_hidden_file(self, path: Path) -> bool:
"""Check if a file is hidden (starts with dot or is .DS_Store)"""
return path.name.startswith('.') or path.name == '.DS_Store'
def is_empty(self, directory: Path) -> bool:
"""Check if a directory is empty (no files, only empty subdirs)"""
for item in directory.iterdir():
if item.is_file():
return False
if item.is_dir() and not self.is_empty(item):
return False
return True
def remove_hidden_files(self) -> int:
"""Remove hidden files from the inbox"""
removed_count = 0
for path in self.inbox_dir.rglob('*'):
if path.is_file() and self.is_hidden_file(path):
try:
path.unlink()
removed_count += 1
print(f" Removed hidden file: {path.name}")
except OSError as e:
print(f" ⚠️ Error removing file {path.name}: {e}")
return removed_count
def remove_empty_directories(self) -> int:
"""Remove empty directories from the inbox"""
removed_count = 0
# Start from deepest directories
for root, dirs, files in os.walk(self.inbox_dir, topdown=False):
root_path = Path(root)
# Skip the inbox root
if root_path == self.inbox_dir:
continue
# Remove if empty
if self.is_empty(root_path):
try:
root_path.rmdir()
removed_count += 1
print(f" Removed empty directory: {root_path.name}")
except OSError as e:
print(f" ⚠️ Error removing directory {root_path.name}: {e}")
return removed_count
def cleanup(self, vault_dir: Path):
"""Clean up the inbox by removing hidden files and empty directories"""
# Remove hidden files
hidden_files_removed = self.remove_hidden_files()
if hidden_files_removed > 0:
print(f" ✓ Removed {hidden_files_removed} hidden files")
# Remove empty directories
empty_dirs_removed = self.remove_empty_directories()
if empty_dirs_removed > 0:
print(f" ✓ Removed {empty_dirs_removed} empty directories")
def main():
"""
Clean up the inbox by removing hidden files and empty directories
"""
print("Digital Vault - Inbox Cleaner")
print("=" * 30)
try:
cleaner = InboxCleaner(INBOX_DIR)
VAULT_DIR.mkdir(parents=True, exist_ok=True)
cleaner.cleanup(VAULT_DIR)
except Exception as e:
print(f"Error during cleanup: {e}")
if __name__ == "__main__":
main()