forked from blavejr/OrganiseDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClean.py
314 lines (268 loc) · 12.4 KB
/
Clean.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
__author__ = "Remigius Kalimba"
'''Add a timer so it does this automatically everyday at a set time'''
from os import path, mkdir, listdir, rename, environ
import sys
import json
import os
import undo
from crontab import CronTab
import getpass
from subprocess import call
import pickle
if sys.version_info >= (3,):
from tkinter import *
from tkinter import messagebox as tkMessageBox
else:
from tkinter import *
import tkMessageBox
pwd = os.path.dirname(os.path.abspath(__file__))
Extensions = json.load(open(pwd+'/Extension.json'))
folders = [x for x in Extensions]
class App(Frame):
def clean(self):
main(folders)
tkMessageBox.showinfo("Complete", "Desktop clean finished.")
def quit_all(self):
quit()
sys.exit(0)
def check(self, item):
if item in folders:
folders.remove(item)
else:
folders.append(item)
def undo_all(self):
undo.execute()
def schedule_start(self):
with open('./settings.txt', 'wb') as setting_file:
pickle.dump(folders, setting_file)
if sys.platform == 'darwin' or sys.platform == 'linux':
my_cron = CronTab(user=getpass.getuser())
job = my_cron.new(command=str(sys.executable+' '+pwd+"/cronCleanUp.py"),
comment="OrganiseDesktop")
job.day.every(1)
my_cron.write()
else:
if not os.path.isfile(pwd+"\\cronCleanUp.pyw"):
call("copy "+pwd+"\\cronCleanUp.py "+pwd+"\\cronCleanUp.pyw", shell=True)
call("SCHTASKS /Create /SC DAILY /TN OrganiseDesktop /TR "+pwd+"\\cronCleanUp.pyw /F",
shell=True)
def schedule_end(self):
os.remove("./settings.txt")
if sys.platform == 'darwin' or sys.platform == 'linux':
my_cron = CronTab(user=getpass.getuser())
my_cron.remove_all(comment="OrganiseDesktop")
my_cron.write()
else:
call("SCHTASKS /Delete /TN OrganiseDesktop /F",
shell=True)
def create(self):
self.winfo_toplevel().title("Desktop Cleaner")
self.shortcuts = Checkbutton(self)
self.shortcuts["text"] = "Shortcuts"
self.shortcuts.select()
self.shortcuts["command"] = lambda: self.check('Shortcuts')
self.shortcuts.pack({"side": "top"})
self.malware = Checkbutton(self)
self.malware["text"] = "Malware"
self.malware.select()
self.malware["command"] = lambda: self.check('Malware')
self.malware.pack({"side": "top"})
self.zip = Checkbutton(self)
self.zip["text"] = "Folders"
self.zip.select()
self.zip["command"] = lambda: self.check('Folders')
self.zip.pack({"side": "top"})
self.music = Checkbutton(self)
self.music["text"] = "Music"
self.music.select()
self.music["command"] = lambda: self.check('Music')
self.music.pack({"side": "top"})
self.images = Checkbutton(self)
self.images["text"] = "Images"
self.images.select()
self.images["command"] = lambda: self.check('Images')
self.images.pack({"side": "top"})
self.exe = Checkbutton(self)
self.exe["text"] = "Executables"
self.exe.select()
self.exe["command"] = lambda: self.check('Executables')
self.exe.pack({"side": "top"})
self.movies = Checkbutton(self)
self.movies["text"] = "Movies"
self.movies.select()
self.movies["command"] = lambda: self.check('Movies')
self.movies.pack({"side": "top"})
self.text = Checkbutton(self)
self.text["text"] = "Text"
self.text.select()
self.text["command"] = lambda: self.check('Text')
self.text.pack({"side": "top"})
self.d3 = Checkbutton(self)
self.d3["text"] = "CAD"
self.d3.select()
self.d3["command"] = lambda: self.check('CAD')
self.d3.pack({"side": "top"})
self.code = Checkbutton(self)
self.code["text"] = "Programming"
self.code.select()
self.code["command"] = lambda: self.check('Programming')
self.code.pack({"side": "top"})
self.clean_button = Button(self)
self.clean_button["text"] = "Clean"
self.clean_button["command"] = self.clean
self.clean_button.pack({"side": "left"})
self.quit_button = Button(self)
self.quit_button["text"] = "Exit"
self.quit_button["command"] = self.quit_all
self.quit_button.pack({"side": "left"})
self.undo_button = Button(self)
self.undo_button['text'] = 'Undo'
self.undo_button['command'] = self.undo_all
self.undo_button.pack({"side": "left"})
self.schedule_button = Button(self)
self.schedule_button['text'] = 'Schedule'
self.schedule_button['command'] = self.schedule_start
self.schedule_button.pack({"side": "left"})
self.remove_schedule_button = Button(self)
self.remove_schedule_button['text'] = 'Remove \nSchedule'
self.remove_schedule_button['command'] = self.schedule_end
self.remove_schedule_button.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.create()
class OrganiseDesktop():
def __init__(self):
'''
This is an initialization function, I do not wish to explain this.
This is a smart way to get the username
We could also have used os.environ, this brings a list and a lot of information we can manipulate.
'''
#
# References: https://en.wikipedia.org/wiki/Environment_variable#Default_values
# https://en.wikipedia.org/wiki/Windows_NT#Releases
#
if sys.platform == 'win32':
self.desktopdir = path.join(environ['USERPROFILE'], 'Desktop')
# Determine Windows version; check if this is XP; accordingly, read target folders
if not sys.getwindowsversion() == 10:
if sys.getwindowsversion().major < 6:
self.Alldesktopdir = path.join(environ['ALLUSERSPROFILE'], 'Desktop')
else:
self.Alldesktopdir = path.join(environ['PUBLIC'], 'Desktop')
'''list of folders to be created'''
elif sys.platform == 'linux' or 'darwin':
self.desktopdir = path.join(environ['HOME'], 'Desktop')
else:
print("{} version not implemented".format(sys.platform))
raise NotImplementedError
def makdir(self, folders_to_make):
'''
This function makes the needed folders if they are not already found.
'''
try:
'''For all the folders in the folder_name list, if that folder is not(False) on the main_desktop
then create that folder.
'''
if sys.platform == 'win32':
for nam in folders_to_make:
if path.isdir(self.desktopdir + '\\' + nam) is False:
mkdir(self.desktopdir + "\\" + nam)
elif sys.platform == 'linux' or 'darwin':
for nam in folders_to_make:
if path.isdir(self.desktopdir + '/' + nam) is False:
mkdir(self.desktopdir + "/" + nam)
except Exception as e:
print(e)
def mapper(self):
'''
This function checks the two folders (current user desktop and all user desktop),
if on windows, only checks one folder if on linux or macOS,
it takes all the items there and puts them into two respective lists which are
returned and used by the mover function
'''
if sys.platform == 'linux' or sys.platform == 'darwin' or sys.getwindowsversion()[0] == 10:
return [listdir(self.desktopdir)]
maps = [listdir(self.desktopdir), listdir(self.Alldesktopdir)]
return maps
def mover(self, maps, folder_names, separator):
print('moving with : '+str(folder_names))
'''
This function gets two lists with all the things on the desktops
and copies them into their respective folders, using a forloop and if statements
'''
# image extensions source: https://fileinfo.com/filetypes/raster_image,
# https://fileinfo.com/filetypes/vector_image, and
# https://fileinfo.com/filetypes/camera_raw
# music extensions source: https://fileinfo.com/filetypes/audio
# movie extensions source: http://bit.ly/2wvYjyr
# text extensions source: http://bit.ly/2wwcfZs
map1 = maps[0]
try:
'''Anything from the All_users_desktop goes to shortcuts, mainly because that's all that's ever there (i think)'''
if separator != '/' and not sys.getwindowsversion()[0] == 10:
map2 = maps[1]
for item in map2:
'''This is a cmd command to move items from one folder to the other'''
rename(self.Alldesktopdir + separator + item, self.desktopdir + separator + item)
for file_or_folder in map1:
if file_or_folder not in folder_names and not file_or_folder.startswith(".") and not file_or_folder.startswith(".."):
found = False
for sorting_folder in folder_names:
if os.path.isdir(self.desktopdir + separator + file_or_folder) and file_or_folder not in Extensions and "Folders" in folder_names:
rename(src=self.desktopdir + separator + file_or_folder,
dst=self.desktopdir + separator + app.zip['text'] + separator + file_or_folder)
found = True
break
for extension in Extensions[sorting_folder]:
if str(file_or_folder.lower()).endswith(extension) and \
str(file_or_folder) != "Clean.lnk" and \
str(file_or_folder) != "Clean.exe.lnk":
rename(src=self.desktopdir + separator + file_or_folder,
dst=self.desktopdir + separator + sorting_folder + separator + file_or_folder)
if separator == '/':
os.system('cd ..')
found = True
break
if not found:
print("Did not sort " + file_or_folder)
except () as e:
print(e)
def writter(self, maps):
'''
This function writes the two lists of all the items left on the desktop
just incase something isn't right and we need a log.
'''
lists1 = maps[0]
writeOB = open('Read_Me.txt', 'w')
writeOB.write("This is a list of all the items on your desktop before it was cleaned.\n"
"Email this list to [email protected] if anything is not working as planned, it will help with debugging\n"
"Together we can make a better app\n\n")
for i in lists1:
writeOB.write(i)
writeOB.write("\n")
if sys.platform == 'win32' and not sys.getwindowsversion()[0] == 10:
lists2 = maps[1]
for i in lists2:
writeOB.write(i)
writeOB.write("\n")
writeOB.close()
def main(folder_names=Extensions):
''' The oh so magnificent main function keeping shit in order '''
projectOB = OrganiseDesktop()
projectOB.makdir(folder_names)
maps = projectOB.mapper()
if sys.platform == 'win32':
projectOB.mover(maps, folder_names, separator='\\')
elif sys.platform == 'linux' or 'darwin':
projectOB.mover(maps, folder_names, separator='/')
projectOB.writter(maps)
if __name__ == "__main__":
root = Tk()
root.resizable = False
root.minsize(width=350, height=330)
root.maxsize(width=350, height=330)
app = App(root)
root.protocol('WM_DELETE_WINDOW', app.quit_all)
app.mainloop()
root.destroy()