1
1
import json
2
2
import tkinter as tk
3
3
from tkinter import filedialog
4
-
4
+ import threading
5
5
import PIL .Image
6
6
import PIL .ImageTk
7
7
import pystray
8
8
from notifypy import Notify
9
+ import time
10
+ import os
11
+ import shutil
12
+ import mimetypes
9
13
10
14
image = PIL .Image .open ("ico.png" )
11
15
12
- enable_notifications = True
16
+ NOTIFY_ENABLE = True
17
+
18
+ DIR_DESTINATION = ""
13
19
14
- destination = ""
20
+ AUTO_SORT = False
15
21
22
+ NOTIFY = Notify ()
16
23
17
- def create_settings ():
24
+
25
+ def SendNotify (title : str , msg :str ) -> None :
26
+ NOTIFY .application_name = "Sorty!"
27
+ NOTIFY .title = title
28
+ NOTIFY .message = msg
29
+ NOTIFY .icon = "ico.png"
30
+ NOTIFY .send ()
31
+
32
+ def create_settings (icon : pystray .Icon ) -> None :
18
33
root = tk .Tk ()
19
34
root .withdraw ()
20
35
ico = PIL .Image .open ('ico.png' )
@@ -23,8 +38,12 @@ def create_settings():
23
38
24
39
download_folder = filedialog .askdirectory (title = "Please select the downloads folder." )
25
40
if not download_folder :
26
- print ("The folder is not selected. Exit the program." )
27
- return
41
+ SendNotify ("😭" , "I cant work correctly without folder!" )
42
+ icon .stop ()
43
+
44
+
45
+
46
+
28
47
notifications_enabled = True
29
48
settings = {
30
49
"notificationsenabled" : notifications_enabled ,
@@ -33,13 +52,7 @@ def create_settings():
33
52
with open ("settings.sorty" , "w" ) as settings_file :
34
53
json .dump (settings , settings_file )
35
54
36
-
37
- import os
38
- import shutil
39
- import mimetypes
40
-
41
-
42
- def sort_files (directory ):
55
+ def sort_files (directory : str ) -> int :
43
56
folders = {
44
57
"Images" : os .path .join (directory , "Images" ),
45
58
"Audio" : os .path .join (directory , "Audio" ),
@@ -86,66 +99,86 @@ def sort_files(directory):
86
99
target_folder = folders ["Other" ]
87
100
88
101
shutil .move (file_path , os .path .join (target_folder , filename ))
89
- print (f"Перемещен: { filename } в { target_folder } " )
90
102
moved_files_count += 1
91
103
except Exception as e :
92
- print ( f"Не удалось переместить файл { filename } : { e } " )
104
+ pass
93
105
94
106
return moved_files_count
95
107
108
+ def sort (icon : pystray .Icon ) -> None :
109
+ if NOTIFY_ENABLE :
110
+ SendNotify ("Sorted" , f"We have sorted { sort_files (DIR_DESTINATION )} files!" )
111
+ else :
112
+ sort_files (DIR_DESTINATION )
96
113
97
- def sort (icon , item ):
98
- if enable_notifications :
99
- notification = Notify ()
100
- notification .application_name = "Sorty!"
101
- notification .title = "Sorted!"
102
- notification .message = f"We have sorted { sort_files (destination )} files!"
103
- notification .icon = "ico.png"
114
+ def exit_action (icon : pystray .Icon ) -> None :
115
+ icon .stop ()
116
+ exit (0 )
104
117
105
- notification .send ()
118
+ def change_dir (icon : pystray .Icon ) -> None :
119
+ global NOTIFY_ENABLE , DIR_DESTINATION
120
+ create_settings (icon )
121
+ with open ("settings.sorty" , "r" ) as settings_file :
122
+ settings = json .load (settings_file )
123
+
124
+ NOTIFY_ENABLE = settings .get ("notificationsenabled" , NOTIFY_ENABLE )
125
+ DIR_DESTINATION = settings .get ("download_folder" , DIR_DESTINATION )
126
+
127
+ def autoswitcher ():
128
+ global AUTO_SORT
129
+
130
+ if AUTO_SORT :
131
+ AUTO_SORT = False
132
+ SendNotify ("Switched" , "Auto is off now!" )
106
133
107
134
else :
108
- sort_files (destination )
135
+ AUTO_SORT = True
136
+ SendNotify ("Switched" , "Auto is on now!" )
109
137
110
- print ("sorted!" )
111
138
112
139
113
- def exit_action (icon : pystray .Icon ):
114
- icon .stop ()
115
- exit (0 )
140
+ def Auto () -> None :
141
+ while True :
142
+ if AUTO_SORT :
143
+ prev_mtime = os .path .getmtime (DIR_DESTINATION )
144
+ while True :
145
+ mtime = os .path .getmtime (DIR_DESTINATION )
146
+ if AUTO_SORT :
147
+ if mtime != prev_mtime :
148
+ sort_files (DIR_DESTINATION )
149
+ prev_mtime = mtime
150
+ else :
151
+ break
152
+ time .sleep (1 )
116
153
154
+ def Main () -> None :
117
155
118
- def change_settings ():
119
- global enable_notifications , destination
120
- create_settings ()
121
- with open ("settings.sorty" , "r" ) as settings_file :
122
- settings = json .load (settings_file )
156
+ icon = pystray .Icon ("Sorty" , image , menu = pystray .Menu (
157
+ pystray .MenuItem ("Sort!" , sort ),
158
+ pystray .MenuItem ("Сhange Directory" ,lambda : change_dir (icon )),
159
+ pystray .MenuItem ("Auto ON/OF" , autoswitcher ),
160
+ pystray .MenuItem ("Exit" , lambda : exit_action (icon ))
161
+ ))
162
+ global NOTIFY_ENABLE , DIR_DESTINATION
123
163
124
- enable_notifications = settings .get ("notificationsenabled" , enable_notifications )
125
- destination = settings .get ("download_folder" , destination )
164
+ if os .path .exists ("settings.sorty" ):
165
+ with open ("settings.sorty" , "r" ) as settings_file :
166
+ settings = json .load (settings_file )
167
+ NOTIFY_ENABLE = settings .get ("notificationsenabled" , NOTIFY_ENABLE )
168
+ DIR_DESTINATION = settings .get ("download_folder" , DIR_DESTINATION )
126
169
127
- print ( destination )
128
- print ( enable_notifications )
170
+ else :
171
+ create_settings ( icon )
129
172
130
173
131
- icon = pystray .Icon ("Sorty" , image , menu = pystray .Menu (
132
- pystray .MenuItem ("Sort!" , sort ),
133
- pystray .MenuItem ("Сhange Directory" , change_settings ),
134
- pystray .MenuItem ("Exit" , lambda : exit_action (icon ))
135
- ))
136
174
137
- if os .path .exists ("settings.sorty" ):
138
- with open ("settings.sorty" , "r" ) as settings_file :
139
- settings = json .load (settings_file )
140
- enable_notifications = settings .get ("notificationsenabled" , enable_notifications )
141
- destination = settings .get ("download_folder" , destination )
175
+ listener_thread = threading .Thread (target = Auto )
176
+ listener_thread .daemon = True
177
+ listener_thread .start ()
178
+
142
179
143
- print (destination )
144
- print (enable_notifications )
145
- else :
146
- create_settings ()
147
180
148
- print (destination )
149
- print (enable_notifications )
181
+ icon .run ()
150
182
151
- icon .run ()
183
+ if __name__ == "__main__" :
184
+ Main ()
0 commit comments