-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfocus_lock.py
More file actions
49 lines (43 loc) · 1.88 KB
/
Copy pathfocus_lock.py
File metadata and controls
49 lines (43 loc) · 1.88 KB
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
import psutil
import time
try:
check_app = input("Enter the name(s) of the app(s) to block \n(comma separated for multiple apps. e.g.: notepad.exe, msedge.exe): ").lower().split(",")
check_app = [app.strip() for app in check_app]
if not check_app:
raise ValueError("No input entered. Please enter the App name to terminate.")
interval = 2
time_format = int(input("Please select your time format:\n1. Type 1 for minutes.\n2. Type 2 for hours.\n"))
duration = int(input("Enter the time (in your selected format) you want to lock the app for: "))
if not duration:
raise ValueError("No duration entered to lock the app for!")
if time_format == 1:
duration = duration * 60
elif time_format == 2:
duration = duration * 60 * 60
endtime = time.time() + duration
print("Focus-lock is now running...")
while endtime > time.time():
for app in check_app:
found = False
processes = psutil.process_iter(['name'])
try:
for process in processes:
if process.info['name'].lower() == app:
found = True
process.terminate()
process.wait(timeout=3)
continue
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess, AttributeError) as e:
print(f"An exception ({e}) has occurred while terminating {app}")
if found:
print(f"{app} was found to be running and was terminated.")
time.sleep(interval)
print("The apps are no longer locked.")
except ValueError as e:
print(e)
except TypeError:
print("The key 'name' doesn't exist.")
except AttributeError:
print("A process doesn't have a name attribute.")
except psutil.Error:
print("An exception has occurred.")