-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatcher.py
More file actions
89 lines (62 loc) · 1.91 KB
/
watcher.py
File metadata and controls
89 lines (62 loc) · 1.91 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
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
from os import get_terminal_size, path
from subprocess import PIPE, run
from sys import argv
from time import perf_counter, sleep
def getFile() -> str:
if argv[1::]:
return argv[1]
else:
print("usage: python run_file.py __filename__")
quit()
def getmtime(filename: str) -> float:
return path.getmtime(filename)
def intro() -> None:
total = get_terminal_size().columns
line = ("—" * total) * 2
print(f"{line}\nChange Detected In File [RUNNING]\n{line}\n")
def timer(func):
def wrapper(*args, **kwargs) -> None:
start = perf_counter()
func(*args, **kwargs)
print(f"\nThis Process Takes {(perf_counter() - start):.2f} Seconds")
return wrapper
@timer
def runFile(filename: str) -> None:
process = run(f"python3 {filename}", stderr=PIPE, shell=True)
if process.returncode:
error = process.stderr.decode()
print(error)
def watcher(filename: str) -> None:
timeForWait = 3
lastModified = getmtime(filename)
print("Watching .....")
while True:
lastmt = getmtime(filename)
if lastModified != lastmt:
sleep(timeForWait)
if lastmt != getmtime(filename):
continue
intro()
try:
runFile(filename) # Running File
except KeyboardInterrupt:
...
print("\nWatching .....")
try:
lastModified = getmtime(filename)
except FileNotFoundError:
continue
else:
try:
sleep(0.5)
except KeyboardInterrupt:
print()
quit()
if __name__ == "__main__":
filename = getFile()
if not path.exists(filename):
print("File Not Found!")
elif filename.split(".")[-1] == "py":
watcher(filename)
else:
print("Only Python File Supported!")