-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoweroff.py
executable file
·157 lines (119 loc) · 3.81 KB
/
poweroff.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
#!/bin/python
import os
welcomemsg = "\nSpecify the desired action:\n\n"
shoutdown = "s - shoutdown\n"
reboot = "r - reboot\n"
logout = "e - end user session\n"
lock = "l - lock the screen\n"
quit = "q - quit\n"
yourinp = "\nYour input: "
msg = welcomemsg + shoutdown + reboot + logout + lock + quit + yourinp
def title():
print(
" ____ __ __ "
)
print(
" | _ \ _____ _____ _ __ ___ / _|/ _| _ __ ___ ___ _ __ _ _ "
)
print(
" | |_) / _ \ \ /\ / / _ \ '__/ _ \| |_| |_ | '_ \`_ \/ _ \ '_ \| | | |"
)
print(
" | __/ (_) \ V V / __/ | | (_) | _| _| | | | | | | __/ | | | |_| |"
)
print(
" |_| \___/ \_/\_/ \___|_| \___/|_| |_| |_| |_| |_|\___|_| |_|_____|"
)
print(
" by saulpierotti"
)
# def reset():
# if os.path.exists("poweroff_menu.running"):
# if not_running != False: #removes the running file if this istance created it
# os.remove("poweroff_menu.running")
# def check_not_running():
# if os.path.exists("poweroff_menu.running"):
# return False
# else:
# open("poweroff_menu.running","w")
# return True
def cmd_s():
# reset()
os.system("systemctl poweroff")
return
def cmd_r():
# reset()
os.system("systemctl reboot")
return
def cmd_e():
# reset()
os.system("i3-msg exit")
return
def cmd_l():
# reset()
os.system("light-locker-command -l")
return
def p_menu(): # create an infinite loop that exits via return
while True:
global inp
inp = input(msg) # ask for user input printing allowed values
try: # try statement
assert ((inp == "s") or (inp == "r") or (inp == "l")
or (inp == "e") or (inp == "q")) # check input
if inp == "s": # test different imput values and take actions
if confirm() == True:
cmd_s()
return
elif inp == "r":
if confirm() == True:
cmd_r()
return
elif inp == "e":
if confirm() == True:
cmd_e()
return
elif inp == "l":
cmd_l()
return
elif inp == "q":
# print("Menu closed")
return # escape the loop via return
else:
print(
"\nReached p_menu() else statement, this should not happen"
) # control statement for debugging
return
except AssertionError: # handle non-allowed input
print("\nNot a valid command") # return an error message
# run the loop again
def confirm(): # customise dialog for user input
if inp == "s":
msg = "shutdown the system"
elif inp == "r":
msg = "reboot the system"
elif inp == "e":
msg = "end user session"
else:
print("\nReached confirm() else statement 1, this should not happen"
) # control statement for debugging
return False
conf_inp = input("\nDo you really want to " + msg +
"? (y/n)\nYour input: ")
assert conf_inp == "y" or conf_inp == "n"
if conf_inp == "y":
print("\nOperation confirmed")
return True
elif conf_inp == "n":
print("\nOperation aborted by the user")
return False
else:
print("\nReached confirm() else statement 2, this should not happen")
return False
# try:
# not_running = check_not_running()
# if not_running:
title()
p_menu()
# finally:
# reset()
# print("poweroff.py ended")