forked from amrwc/macos-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth.py
executable file
·68 lines (48 loc) · 1.4 KB
/
bluetooth.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
#!/usr/bin/env python3
"""
# Bluetooth
A utility for turning Bluetooth on/off from command-line.
## Setup
1. Install Blueutil:
```console
brew install blueutil
```
2. Create a symlink to the script.
```console
ln -s "$(pwd)/bluetooth.py" /usr/local/bin/bluetooth
```
## Usage
```console
bluetooth <on/off>
```
@author: amrwc
"""
import sys
from typing import List
from abstract_automation import Automation
from utils import (
execute_cmd,
log,
print_coloured,
raise_error,
)
class Bluetooth(Automation):
def __init__(self, argv: List[str] = []):
self.argv = self.parse_argv(argv)
def execute(self) -> str:
log(f"Turning Bluetooth {self.argv[0]}")
on = self.argv[0] == 'on'
return execute_cmd(['blueutil', '-p', ('1' if on else '0')]).strip()
def parse_argv(self, argv: List[str]) -> List[str]:
if len(argv) == 0:
raise_error('No option provided', usage=self.usage)
if argv[0] not in ('on', 'off'):
raise_error(f"Unsupported option provided: {argv[0]}", usage=self.usage)
return argv
def usage(self) -> None:
print_coloured('Usage:\n', 'white', 'bold')
print_coloured('$ ./bluetooth.py <on/off>\n', 'white')
if __name__ == '__main__':
result = Bluetooth(sys.argv[1:]).execute()
if result != '':
raise_error(f"Something went wrong; stdout: {result}")