-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSHandler.py
467 lines (390 loc) · 16.5 KB
/
OSHandler.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import datetime
import os
import psutil
import wmi
import music
from global_function import string_similarity, data_exist, Adjust_Volume_Off, Adjust_Volume_On
from pynput.keyboard import Key, Controller, KeyCode
import pickle
import threading
import re
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import cpuinfo
import json
import PIL.ImageGrab
import winshell
import shutil
import win32security
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import win32con
import win32gui,win32process
computer_username = os.getlogin()
def batteryInfo():
battery = psutil.sensors_battery()
pr = str(battery.percent)
if battery.power_plugged:
return "Your System is currently on Charging Mode and it's " + pr + "% done."
return "Your System is currently on " + pr + "% battery life."
cpu_usage = 0.0
def Monitor_CPU():
while True:
global cpu_usage
cpu_usage = psutil.cpu_percent(interval=1)
threading.Thread(target=Monitor_CPU, daemon=True).start()
def Delete_Trash_Files():
if os.path.exists('Resources/web scraping/billboard/result.json'):
os.remove('Resources/web scraping/billboard/result.json')
if os.path.exists('Resources/web scraping/covid/result.json'):
os.remove('Resources/web scraping/covid/result.json')
folder = 'Resources/web scraping/imgDownloads'
for file in os.listdir(folder):
file_path = os.path.join(folder, file)
try:
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except:
pass
def systemInfo(type):
if type == 0:
c = wmi.WMI()
my_system_1 = c.Win32_LogicalDisk()[0]
my_system_2 = c.Win32_ComputerSystem()[0]
info = ['Total Disk Space: ' + str(round(int(my_system_1.Size) / (1024 ** 3), 2)) + ' GB',
'Free Disk Space: ' + str(round(int(my_system_1.Freespace) / (1024 ** 3), 2)) + ' GB',
'Manufacturer: ' + my_system_2.Manufacturer,
'Model: ' + my_system_2.Model,
'Owner: ' + my_system_2.PrimaryOwnerName,
'Number of Processors: ' + str(my_system_2.NumberOfProcessors),
'System Type: ' + my_system_2.SystemType]
elif type == 1:
info = ['Total: ' + str(round(psutil.virtual_memory().total / (1024**3), 1)) + ' GB']
else:
cpu = json.loads(cpuinfo.get_cpu_info_json())
info = ['Brand: ' + cpu['brand_raw'],
'Based speed: ' + str(round(psutil.cpu_freq().current / 1000, 2)) + ' GHz',
'Processors: ' + str(cpu['count']),
'Cores: ' + str(cpu['family'])]
return info
def systemUsage(type, app_use=False):
if type == 0:
return str(round(cpu_usage))
mem = psutil.virtual_memory()
percent = round(mem.used / mem.total * 100)
if type == 1:
if app_use:
process = psutil.Process(os.getpid())
mem_used = process.memory_info().rss
percent = round(mem_used / mem.total * 100)
return [round(mem_used / (1024**2), 1), percent]
return [str(round(mem.used / (1024**3), 1)), percent]
return ['CPU: ' + str(round(cpu_usage)) + '%',
'RAM: ' + str(round(mem.used / (1024**3), 1)) + ' GB (' + str(percent) + '%)']
list_of_program = set()
def Get_Programs(path):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.exe') or file.endswith('.lnk'):
if file.endswith('.exe'):
list_of_program.add((os.path.join(root, file), ''))
else:
shortcut = winshell.shortcut(os.path.join(root, file))
list_of_program.add((shortcut.path, shortcut.arguments))
def Update_Program_List():
if new_program_list or delete_program_list:
remove_program = []
for program in delete_program_list:
for exist_program in list_of_program:
if program in exist_program[0]:
remove_program.append(exist_program)
break
for exist_program in remove_program:
list_of_program.remove(exist_program)
for program in new_program_list:
if os.path.isfile(program):
if program.endswith('.exe') or program.endswith('.lnk'):
if program.endswith('.exe'):
list_of_program.add((program, ''))
else:
shortcut = winshell.shortcut(program)
list_of_program.add((shortcut.path, shortcut.arguments))
else:
Get_Programs(program)
new_program_list.clear()
delete_program_list.clear()
with open('Resources/file path/program.pck', 'wb') as file:
pickle.dump(list_of_program, file)
def Path_Update(type):
def get_bit(x, pos):
return (x >> pos) & 1
if get_bit(type, 0) == 1:
list_of_program.clear()
thread1 = threading.Thread(target=Get_Programs, args=('C:\\Program Files',), daemon=True)
thread2 = threading.Thread(target=Get_Programs,
args=(f'C:\\Users\\{computer_username}\\Desktop',),
daemon=True)
thread1.start()
thread2.start()
Get_Programs('C:\\Program Files (x86)')
thread1.join()
thread2.join()
with open('Resources/file path/program.pck', 'wb') as file:
pickle.dump(list_of_program, file)
if get_bit(type, 1) == 1:
music.Update()
def Get_Program_List():
global list_of_program
if os.path.exists('Resources/file path/program.pck') is False:
thread1 = threading.Thread(target=Get_Programs, args=('C:\\Program Files',), daemon=True)
thread2 = threading.Thread(target=Get_Programs,
args=(f'C:\\Users\\{computer_username}\\Desktop',),
daemon=True)
thread1.start()
thread2.start()
Get_Programs('C:\\Program Files (x86)')
thread1.join()
thread2.join()
with open('Resources/file path/program.pck', 'wb') as file:
pickle.dump(list_of_program, file)
else:
modified = False
with open('Resources/file path/program.pck', 'rb') as file:
tmp_list_of_program = pickle.load(file)
for program in tmp_list_of_program:
if os.path.isfile(program[0]):
list_of_program.add(program)
else:
#some programs are deleted
modified = True
if modified:
with open('Resources/file path/program.pck', 'wb') as file:
pickle.dump(list_of_program, file)
new_program_list = set()
delete_program_list = set()
class MyHandler(FileSystemEventHandler):
def on_deleted(self, event):
if event.src_path in new_program_list:
new_program_list.remove(event.src_path)
delete_program_list.add(event.src_path)
def on_created(self, event):
if event.src_path in delete_program_list:
delete_program_list.remove(event.src_path)
new_program_list.add(event.src_path)
def on_moved(self, event):
if event.src_path in new_program_list:
new_program_list.remove(event.src_path)
if event.dest_path in delete_program_list:
delete_program_list.remove(event.dest_path)
delete_program_list.add(event.src_path)
new_program_list.add(event.dest_path)
event_handler = MyHandler()
observer = Observer()
observer.daemon = True
observer.schedule(event_handler, path='C:\\Program Files', recursive=False)
observer.schedule(event_handler, path=f'C:\\Users\\{computer_username}\\Desktop', recursive=False)
observer.schedule(event_handler, path='C:\\Program Files (x86)', recursive=False)
observer.start()
#return program path and arguments if needed
def Program_Obj(program):
program = program.replace(' ', '')
if program == '':
return None
Update_Program_List()
with open('Resources/command/rename program.pck', 'rb') as file:
rename_program = pickle.load(file)
if program in rename_program:
program = rename_program[program]
name_match = []
for program_obj in list_of_program:
if os.path.isfile(program_obj[0]):
if program in program_obj[0].lower().replace(' ', ''):
program_name = os.path.splitext(os.path.basename(program_obj[0]))[0]
name_match.append([program_obj, string_similarity(program_name.lower(), program)])
if name_match:
match_percentage = 0.0
return_obj = []
for x in name_match:
if x[1] > match_percentage:
return_obj = x[0]
match_percentage = x[1]
return return_obj
return None
def Unikey_Switch():
keyboard = Controller()
keyboard.press(Key.ctrl)
keyboard.press(Key.shift)
keyboard.release(Key.ctrl)
keyboard.release(Key.shift)
def Press(command, type_num):
keyboard = Controller()
for i in range(type_num):
for x in command:
keyboard.press(x)
for x in command:
keyboard.release(x)
def Type(word, type_num, enter):
keyboard = Controller()
for i in range(type_num):
if i != 0:
keyboard.press(Key.space)
keyboard.release(Key.space)
for x in word:
keyboard.press(KeyCode(char=x))
keyboard.release((KeyCode(char=x)))
if enter:
keyboard.press(Key.enter)
keyboard.release(Key.enter)
volume = cast(AudioUtilities.GetSpeakers().Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None), POINTER(IAudioEndpointVolume))
'''def Standard_Volume_Scale():
if os.path.exists('Resources/userData/standard volume level'):
return pickle.load(open('Resources/userData/standard volume level', 'rb'))
standard_volume_decibel = -16.000192642211914
current_level = round(volume.GetMasterVolumeLevelScalar(), 2)
standard_volume_scale = -1
min_diff = 1e9
for i in range(101):
volume.SetMasterVolumeLevelScalar(i / 100, None)
level = volume.GetMasterVolumeLevel()
if abs(level - standard_volume_decibel) < min_diff:
min_diff = abs(level - standard_volume_decibel)
standard_volume_scale = i
volume.SetMasterVolumeLevelScalar(current_level, None)
pickle.dump(standard_volume_scale, open('Resources/userData/standard volume level', 'wb'))
return standard_volume_scale'''
def Volume(command):
current_level = round(volume.GetMasterVolumeLevelScalar(), 2)
if data_exist(['volume to'], command, 2):
nums = re.findall(r'[0-9]+', command)
if nums:
num = int(nums[0])
else:
if not data_exist(['correct', 'adjust', 'auto', 'standard', 'default'], command):
with open('Resources/userData/standard volume level.txt', 'r') as file:
num = file.read()
else:
num = 30
num /= 100
num = max(num, 0)
num = min(num, 1)
if data_exist(['correct', 'adjust', 'auto', 'standard', 'default'], command):
return 'set standard volume to ' + str(int(num * 100))
volume.SetMasterVolumeLevelScalar(num, None)
return 'set volume to ' + str(int(num * 100))
if data_exist(['correct', 'adjust', 'auto'], command, 5):
if data_exist(['stop', 'off', 'disable', 'terminate'], command):
Adjust_Volume_Off()
return 'adjusting volume off'
else:
Adjust_Volume_On()
return 'adjusting volume on'
if data_exist(['unmute', 'on', 'unmuted'], command):
volume.SetMute(False, None)
return 'volume unmuted'
if data_exist(['mute', 'off', 'shut up', 'muted'], command, 2):
volume.SetMute(True, None)
return 'volume muted'
if data_exist(['standard', 'default'], command):
with open('Resources/userData/standard volume level.txt', 'r') as file:
num = file.read()
return 'the current standard volume is ' + str(num)
'''if data_exist(['twice'], command):
num = 2
else:
nums = re.findall(r'[0-9]+', command)
if nums:
num = int(nums[0])
else:
num = 1
if data_exist(['up', 'high'], command):
target_level = min(current_level + num / 100, 1)
volume.SetMasterVolumeLevelScalar(target_level, None)
return 'set volume to ' + str(int(target_level * 100))
if data_exist(['down', 'low'], command):
target_level = max(current_level - num / 100, 0)
volume.SetMasterVolumeLevelScalar(target_level, None)
return 'set volume to ' + str(int(target_level * 100))'''
return 'your current volume is: ' + str(int(current_level * 100))
def Screen_Capture():
img = PIL.ImageGrab.grab()
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
path = os.path.join(f'C:\\Users\\{computer_username}\\Pictures',
f'screenshot {current_time}.jpg')
img.save(path)
os.startfile(path)
return path
def Delete_Temp_Files():
folder = f'C:/Users/{computer_username}/AppData/Local/Temp'
for file in os.listdir(folder):
file_path = os.path.join(folder, file)
try:
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except:
pass
def Empty_Recycle_Bin():
temp_file_thread = threading.Thread(target=Delete_Temp_Files, daemon=True)
temp_file_thread.start()
try:
winshell.recycle_bin().empty(confirm=False, show_progress=False,sound=False)
except:
pass
temp_file_thread.join()
#return folder path
def Open_Folder(folder):
with open('Resources/file path/folder path.pck', 'rb') as file:
paths = pickle.load(file)
for path in paths:
path['name'].sort(key=lambda x : len(x), reverse=True)
paths.sort(key=lambda x : len(x['name'][0]), reverse=True)
for path in paths:
if data_exist(path['name'], folder, 6):
folder_path = path['path']
folder_path = folder_path.replace('__os_UserName__', computer_username)
folder_path = folder_path.replace('__security_identity__', str(win32security.LookupAccountName(None, computer_username)[0])[6:])
if os.path.isdir(folder_path):
for name in path['name']:
if data_exist([name], folder, 6):
return [name, folder_path]
return None
def windowEnumerationHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_window_pid(title):
hwnd = win32gui.FindWindow(None, title)
threadid, pid = win32process.GetWindowThreadProcessId(hwnd)
return pid
def Program_Already_Opened(program_path):
top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
for i in top_windows:
if win32gui.IsWindowVisible(i[0]):
path = psutil.Process(get_window_pid(i[1])).exe()
if program_path == path:
win32gui.ShowWindow(i[0], win32con.SW_MAXIMIZE)
win32gui.SetForegroundWindow(i[0])
return True
return False
def Close_Program(program_name):
with open('Resources/command/close system files rename.pck', 'rb') as file:
close_system_files_rename = pickle.load(file)
is_system_file = False
program_exe = ''
for file in close_system_files_rename:
if data_exist(file['name'], program_name, 0):
program_exe = file['rename']
is_system_file = True
break
if not is_system_file:
program_obj = Program_Obj(program_name)
if program_obj is None:
return
program_exe = os.path.basename(program_obj[0])
for process in psutil.process_iter():
if process.name() == program_exe:
process.kill()