forked from amrwc/macos-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep.py
executable file
·55 lines (38 loc) · 1.05 KB
/
sleep.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
#!/usr/bin/env python3
"""
# Sleep
A utility for putting the machine to sleep from command-line.
## Setup
Create a symlink to the script. Remember that there's a built-in `sleep`
utility in the system that takes precedence. That's why it's necessary to
choose a different name.
```console
ln -s "$(pwd)/sleep.py" /usr/local/bin/night
```
## Usage
```console
night
```
@author: amrwc
"""
from typing import List
from abstract_automation import Automation
from utils import (
execute_cmd,
log,
print_coloured,
raise_error,
)
class Sleep(Automation):
def execute(self) -> str:
log('Putting the machine to sleep')
return execute_cmd(['pmset', 'sleepnow']).strip()
def parse_argv(self, argv: List[str]) -> List[str]:
return []
def usage(self) -> None:
print_coloured('Usage:\n', 'white', 'bold')
print_coloured('$ ./sleep.py\n', 'white')
if __name__ == '__main__':
result = Sleep().execute()
if result != 'Sleeping now...':
raise_error(f"Something went wrong; stdout: {result}")