-
Notifications
You must be signed in to change notification settings - Fork 0
/
autofile_organizer.py
155 lines (126 loc) · 5.28 KB
/
autofile_organizer.py
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
""" Built-in modules """
import os
import shutil
import sys
import time
from pathlib import Path
# External modules #
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Global variables #
SRC_DIR = 'Dock'
TEXT_DIR = 'TextFiles'
IMG_DIR = 'Images'
VID_DIR = 'Videos'
MUSIC_DIR = 'Music'
CODE_DIR = 'Code'
COMP_DIR = 'CompressedData'
DATA_DIR = 'RawData'
WIN_DIR = 'MicrosoftFiles'
OTHER_DIR = 'OtherData'
TEXT_EXT = ('.txt', '.pdf', '.rtf', '.tif', '.tiff', '.vob')
IMG_EXT = ('.bmp', '.gif', '.jpg', '.jpeg', '.png', '.thm')
VID_EXT = ('.acc', '.adt', '.adts', '.avi', '.flv', '.mov', '.mpeg', '.wmv')
MUSIC_EXT = ('.aif', '.aifc', '.aiff', '.m4a', '.mp3', '.mp4', '.wav', '.wma')
CODE_EXT = ('.bat', '.csv', '.dll', '.htm', '.html', '.xhtml', '.jhtml', '.ini', '.jar', '.c',
'.cc', '.cpp', '.cxx', '.js', '.jse', '.cs', '.py', '.css', '.jsp', '.jspx', '.wss',
'.do', '.action', '.pl', '.php', '.php3', '.php4', '.phtml', '.rb', '.rhtml', '.xml')
COMP_EXT = ('.ar', '.bz2', '.gzip', '.rar', '.zip')
DATA_EXT = ('.bin', '.dif', '.dump', '.exe', '.iso', '.mid', '.midi', '.swf', '.tmp')
WIN_EXT = ('.doc', '.docm', '.docx', '.dot', '.dotx', '.eml', '.mdb', '.msi', '.pot', '.potm',
'.potx', '.ppam', '.pps', '.ppsm', '.ppsx', '.ppt', '.pptm', '.pptx', '.pst', '.pub',
'.sldm', '.sldx', '.sys', '.wks', '.wmd')
class BackupHandler(FileSystemEventHandler):
""" Class is trigger when the monitored SRC_DIR detects modification """
def on_modified(self, event):
"""
Copies contents of source dir to dest dir on event detection per modification.
:param event: On detection of modification of contents of SRC_DIR.
:return: Nothing
"""
# Get the current working directory #
cwd = Path.cwd()
# Iterate through available files in source dir #
for file in os.scandir(SRC_DIR):
# Set the source file path #
src_file = cwd / SRC_DIR / file.name
# If the file is a text file #
if file.name.endswith(TEXT_EXT):
# Set the destination file path #
dst_file = cwd / TEXT_DIR / file.name
# If the file is a image #
elif file.name.endswith(IMG_EXT):
# Set the destination file path #
dst_file = cwd / IMG_DIR / file.name
# If the file is a video #
elif file.name.endswith(VID_EXT):
# Set the destination file path #
dst_file = cwd / VID_DIR / file.name
# If the file is a music track #
elif file.name.endswith(MUSIC_EXT):
# Set the destination file path #
dst_file = cwd / MUSIC_DIR / file.name
# If the file is source code #
elif file.name.endswith(CODE_EXT):
# Set the destination file path #
dst_file = cwd / CODE_DIR / file.name
# If the file is a compressed archive #
elif file.name.endswith(COMP_EXT):
# Set the destination file path #
dst_file = cwd / COMP_DIR / file.name
# If the file in raw data format #
elif file.name.endswith(DATA_EXT):
# Set the destination file path #
dst_file = cwd / DATA_DIR / file.name
# If the file is a Windows based file #
elif file.name.endswith(WIN_EXT):
# Set the destination file path #
dst_file = cwd / WIN_DIR / file.name
# If the file is of unknown data type #
else:
# Set the destination file path #
dst_file = cwd / OTHER_DIR / file.name
try:
# Copy the source dir file to the destination dir #
shutil.copy(src_file, dst_file)
# Delete moved file from source dir #
src_file.unlink()
# Ignore passive OS access errors #
except OSError:
pass
def main():
"""
Facilitates file system handler, copies data to desired folders based on file extension.
:return: Nothing
"""
# Ensure the program directories exist #
for dirname in (SRC_DIR, TEXT_DIR, IMG_DIR, VID_DIR, MUSIC_DIR,
CODE_DIR, COMP_DIR, DATA_DIR, WIN_DIR, OTHER_DIR):
# Format current dir as path #
dir_path = Path.cwd() / dirname
# If the directory does not exist #
if not dir_path.exists():
# Create the directory #
dir_path.mkdir(parents=True)
print('[!] Running File Backup Automater, hit Ctrl + C to exit')
# Initialize BackupHandler object #
file_monitor = BackupHandler()
# Initialize the observer object #
observer = Observer()
# Schedule the file monitoring object to run #
observer.schedule(file_monitor, SRC_DIR, recursive=True)
# Start the file monitoring object #
observer.start()
# Run file system monitor until Ctrl+C #
try:
while True:
time.sleep(15)
# If Ctrl+C is detected #
except KeyboardInterrupt:
# Stop the file monitoring object #
observer.stop()
# Join the file monitoring child process #
observer.join()
sys.exit(0)
if __name__ == '__main__':
main()