-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.py
70 lines (56 loc) · 1.64 KB
/
commands.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
import subprocess
from dataclasses import dataclass
from filesystem import is_windows
from sys import platform
@dataclass
class RunCommand:
command:str
def __call__(this):
subprocess.run(this.command,shell=True)
def __str__(this):
return this.command
def __repr__(this):
return str(this)
class NoCommand(RunCommand):
def __init__(this):
super().__init__("")
def __call__(this):
pass
def __str__(this):
return "NOTHING"
class ShutdownCommand(RunCommand):
def __init__(this):
if platform.startswith("linux"):
cmd = "systemctl poweroff"
elif platform.startswith("win"):
cmd = "shutdown /s /t 0"
elif platform.startswith("darwin"):
cmd = 'osascript -e tell app "System Events" to shut down'
else:
cmd = ""
super().__init__(cmd)
def __str__(this):
return "SHUTDOWN"
class SupendCommand(RunCommand):
def __init__(this):
if platform.startswith("linux"):
cmd = "systemctl suspend"
elif platform.startswith("win"):
cmd = "rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
elif platform.startswith("darwin"):
cmd = "pmset sleepnow"
else:
cmd = ""
super().__init__(cmd)
def __str__(this):
return "SUSPEND"
def make_command(cmd:str) -> RunCommand:
match cmd:
case "SHUTDOWN":
return ShutdownCommand()
case "SUSPEND":
return SupendCommand()
case "NOTHING" | None:
return NoCommand()
case _:
return RunCommand(cmd)