-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommander.py
More file actions
129 lines (100 loc) · 3.33 KB
/
Copy pathCommander.py
File metadata and controls
129 lines (100 loc) · 3.33 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
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
#!/usr/bin/env python3
import time, os
from datetime import datetime
def askHowMany():
try:
n = int(input("How many commands do you want to repeat? "))
return n
except ValueError:
print("Invalid number")
return askHowMany()
def askTTR(commands):
try:
ttr = int(input("Enter Delay in seconds (0 is valid): "))
except ValueError:
print("Invalid number")
return askTTR(commands)
askHowManyTimes(commands, ttr)
def askHowManyTimes(commands, ttr):
try:
times = int(input("How many times do you want to repeat the command? (0 -> infinite) "))
except ValueError:
print("Invalid number")
return askHowManyTimes(commands, ttr)
if times < 0:
print("Times cannot be negative")
return askHowManyTimes(commands, ttr)
askWD(commands, ttr, times)
def askWD(commands, ttr, times):
wd = input("Do you want to set a specific working directory? (y/n): ").lower()
if wd not in ['y', 'n']:
print("Please answer with 'y' or 'n'")
return askWD(commands, ttr, times)
if wd == 'n':
askTime(commands, ttr, times)
elif wd == 'y':
path = input("Introduce the path: ")
if os.path.isdir(path):
os.chdir(path)
print(f"Working directory set to: {path}")
askTime(commands, ttr, times)
else:
print("Invalid path")
return askWD(commands, ttr, times)
def askTime(commands, ttr, times):
answer = input("Do you want to set a specific time to start? (y/n): ").lower()
if answer not in ['y', 'n']:
print("Please answer with 'y' or 'n'")
return askTime(commands, ttr, times)
if answer == 'n':
EXE(commands, ttr, times)
return
elif answer == 'y':
while True:
time = input("Introduce the time (HH:MM:SS): ")
try:
time.strptime(time, "%H:%M:%S")
break
except ValueError:
print("Incorrect format. Use HH:MM:SS")
waitTime(commands, ttr, time, times)
def waitTime(commands, ttr, time, times):
print(f"Waiting until {time}...")
while True:
now = datetime.now().strftime("%H:%M:%S")
if now == time:
EXE(commands, ttr, times)
break
time.sleep(1)
def EXE(commands, ttr, times):
print("\nRunning... (Ctrl+C to stop)\n")
if times == 0:
try:
while True:
for command in commands:
print(f"> {command}")
os.system(command)
time.sleep(ttr)
except KeyboardInterrupt:
print("\nStopped")
elif times > 0:
try:
while times > 0:
for command in commands:
print(f"> {command}")
os.system(command)
time.sleep(ttr)
times -= 1
except KeyboardInterrupt:
print("\nStopped")
else:
raise ValueError("Unexpected error")
break
def main():
commands = []
for i in range(askHowMany()):
command = input(f"Command Nº{i+1}: ")
commands.append(command)
askTTR(commands)
if __name__ == "__main__":
main()