Skip to content

Commit 6d94d21

Browse files
committed
Create and Add EventHandler and cleandest python files along with .gitignore file
1 parent 0a2c7d7 commit 6d94d21

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed

app/EventHandler.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import shutil
2+
from datetime import date
3+
from pathlib import Path
4+
5+
from watchdog.events import FileSystemEventHandler
6+
7+
from extensions import extension_paths
8+
9+
10+
def add_date_to_path(path: Path):
11+
"""
12+
Helper function that adds current year/month to destination path. If the path
13+
doesn't already exist, it is created.
14+
15+
:param Path path: destination root to append subdirectories based on date
16+
"""
17+
dated_path = path / f'{date.today().year}' / f'{date.today().strftime("%b").upper()}'
18+
dated_path.mkdir(parents=True, exist_ok=True)
19+
return dated_path
20+
21+
22+
def rename_file(source: Path, destination_path: Path):
23+
"""
24+
Helper function that renames file to reflect new path. If a file of the same
25+
name already exists in the destination folder, the file name is numbered and
26+
incremented until the filename is unique (prevents overwriting files).
27+
28+
:param Path source: source of file to be moved
29+
:param Path destination_path: path to destination directory
30+
"""
31+
if Path(destination_path / source.name).exists():
32+
increment = 0
33+
34+
while True:
35+
increment += 1
36+
new_name = destination_path / f'{source.stem}_{increment}{source.suffix}'
37+
38+
if not new_name.exists():
39+
return new_name
40+
else:
41+
return destination_path / source.name
42+
43+
44+
class EventHandler(FileSystemEventHandler):
45+
def __init__(self, watch_path: Path, destination_root: Path):
46+
self.watch_path = watch_path.resolve()
47+
self.destination_root = destination_root.resolve()
48+
49+
def on_modified(self, event):
50+
for child in self.watch_path.iterdir():
51+
# skips directories and non-specified extensions
52+
if child.is_file() and child.suffix.lower() in extension_paths:
53+
destination_path = self.destination_root / extension_paths[child.suffix.lower()]
54+
destination_path = add_date_to_path(path=destination_path)
55+
destination_path = rename_file(source=child, destination_path=destination_path)
56+
shutil.move(src=child, dst=destination_path)

app/__init__.py

Whitespace-only changes.

app/cleandesk.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from pathlib import Path
2+
from time import sleep
3+
4+
from watchdog.observers import Observer
5+
6+
from EventHandler import EventHandler
7+
8+
if __name__ == '__main__':
9+
watch_path_1 = Path.home() / 'Downloads'
10+
watch_path_2 = Path.home() / 'Pictures'
11+
watch_path_3 = Path.home() / 'Desktop'
12+
destination_root_1 = Path.home() / 'Downloads/holder of things'
13+
destination_root_2 = Path.home() / 'Pictures/holder of things'
14+
destination_root_3 = Path.home() / 'Desktop/holder of things'
15+
16+
event_handler_1 = EventHandler(watch_path=watch_path_1, destination_root=destination_root_1)
17+
event_handler_2 = EventHandler(watch_path=watch_path_2, destination_root=destination_root_2)
18+
event_handler_3 = EventHandler(watch_path=watch_path_3, destination_root=destination_root_3)
19+
20+
observer_1 = Observer()
21+
observer_1.schedule(event_handler_1, f'{watch_path_1}', recursive=True)
22+
observer_1.start()
23+
24+
observer_2 = Observer()
25+
observer_2.schedule(event_handler_2, f'{watch_path_2}', recursive=True)
26+
observer_2.start()
27+
28+
observer_3 = Observer()
29+
observer_3.schedule(event_handler_3, f'{watch_path_3}', recursive=True)
30+
observer_3.start()
31+
32+
try:
33+
while True:
34+
sleep(60)
35+
except KeyboardInterrupt:
36+
observer_1.stop()
37+
observer_2.stop()
38+
observer_3.stop()
39+
observer_1.join()
40+
observer_2.join()
41+
observer_3.join()

app/extensions.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
extension_paths = {
2+
# No name
3+
'noname': 'other/uncategorized',
4+
# audio
5+
'.aif': 'media/audio',
6+
'.cda': 'media/audio',
7+
'.mid': 'media/audio',
8+
'.midi': 'media/audio',
9+
'.mp3': 'media/audio',
10+
'.mpa': 'media/audio',
11+
'.ogg': 'media/audio',
12+
'.wav': 'media/audio',
13+
'.wma': 'media/audio',
14+
'.wpl': 'media/audio',
15+
'.m3u': 'media/audio',
16+
# text
17+
'.txt': 'text/text_files',
18+
'.doc': 'text/microsoft/word',
19+
'.docx': 'text/microsoft/word',
20+
'.odt ': 'text/text_files',
21+
'.pdf': 'text/pdf',
22+
'.rtf': 'text/text_files',
23+
'.tex': 'text/text_files',
24+
'.wks ': 'text/text_files',
25+
'.wps': 'text/text_files',
26+
'.wpd': 'text/text_files',
27+
# video
28+
'.3g2': 'media/video',
29+
'.3gp': 'media/video',
30+
'.avi': 'media/video',
31+
'.flv': 'media/video',
32+
'.h264': 'media/video',
33+
'.m4v': 'media/video',
34+
'.mkv': 'media/video',
35+
'.mov': 'media/video',
36+
'.mp4': 'media/video',
37+
'.mpg': 'media/video',
38+
'.mpeg': 'media/video',
39+
'.rm': 'media/video',
40+
'.swf': 'media/video',
41+
'.vob': 'media/video',
42+
'.wmv': 'media/video',
43+
# images
44+
'.ai': 'media/images',
45+
'.bmp': 'media/images',
46+
'.gif': 'media/images',
47+
'.jpg': 'media/images',
48+
'.jpeg': 'media/images',
49+
'.png': 'media/images',
50+
'.ps': 'media/images',
51+
'.psd': 'media/images',
52+
'.svg': 'media/images',
53+
'.tif': 'media/images',
54+
'.tiff': 'media/images',
55+
'.cr2': 'media/images',
56+
# internet
57+
'.asp': 'other/internet',
58+
'.aspx': 'other/internet',
59+
'.cer': 'other/internet',
60+
'.cfm': 'other/internet',
61+
'.cgi': 'other/internet',
62+
'.pl': 'other/internet',
63+
'.css': 'other/internet',
64+
'.htm': 'other/internet',
65+
'.js': 'other/internet',
66+
'.jsp': 'other/internet',
67+
'.part': 'other/internet',
68+
'.php': 'other/internet',
69+
'.rss': 'other/internet',
70+
'.xhtml': 'other/internet',
71+
'.html': 'other/internet',
72+
# compressed
73+
'.7z': 'other/compressed',
74+
'.arj': 'other/compressed',
75+
'.deb': 'other/compressed',
76+
'.pkg': 'other/compressed',
77+
'.rar': 'other/compressed',
78+
'.rpm': 'other/compressed',
79+
'.tar.gz': 'other/compressed',
80+
'.z': 'other/compressed',
81+
'.zip': 'other/compressed',
82+
# disc
83+
'.bin': 'other/disc',
84+
'.dmg': 'other/disc',
85+
'.iso': 'other/disc',
86+
'.toast': 'other/disc',
87+
'.vcd': 'other/disc',
88+
# data
89+
'.csv': 'programming/database',
90+
'.dat': 'programming/database',
91+
'.db': 'programming/database',
92+
'.dbf': 'programming/database',
93+
'.log': 'programming/database',
94+
'.mdb': 'programming/database',
95+
'.sav': 'programming/database',
96+
'.sql': 'programming/database',
97+
'.tar': 'programming/database',
98+
'.xml': 'programming/database',
99+
'.json': 'programming/database',
100+
# executables
101+
'.apk': 'other/executables',
102+
'.bat': 'other/executables',
103+
'.com': 'other/executables',
104+
'.exe': 'other/executables',
105+
'.gadget': 'other/executables',
106+
'.jar': 'other/executables',
107+
'.wsf': 'other/executables',
108+
# fonts
109+
'.fnt': 'other/fonts',
110+
'.fon': 'other/fonts',
111+
'.otf': 'other/fonts',
112+
'.ttf': 'other/fonts',
113+
# presentations
114+
'.key': 'text/presentations',
115+
'.odp': 'text/presentations',
116+
'.pps': 'text/presentations',
117+
'.ppt': 'text/presentations',
118+
'.pptx': 'text/presentations',
119+
# programming
120+
'.c': 'programming/c&c++',
121+
'.class': 'programming/java',
122+
'.java': 'programming/java',
123+
'.py': 'programming/python',
124+
'.sh': 'programming/shell',
125+
'.h': 'programming/c&c++',
126+
# spreadsheets
127+
'.ods': 'text/microsoft/excel',
128+
'.xlr': 'text/microsoft/excel',
129+
'.xls': 'text/microsoft/excel',
130+
'.xlsx': 'text/microsoft/excel',
131+
# system
132+
'.bak': 'text/other/system',
133+
'.cab': 'text/other/system',
134+
'.cfg': 'text/other/system',
135+
'.cpl': 'text/other/system',
136+
'.cur': 'text/other/system',
137+
'.dll': 'text/other/system',
138+
'.dmp': 'text/other/system',
139+
'.drv': 'text/other/system',
140+
'.icns': 'text/other/system',
141+
'.ico': 'text/other/system',
142+
'.ini': 'text/other/system',
143+
'.lnk': 'text/other/system',
144+
'.msi': 'text/other/system',
145+
'.sys': 'text/other/system',
146+
'.tmp': 'text/other/system'
147+
}

0 commit comments

Comments
 (0)