forked from guilleijo/wificli-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (57 loc) · 1.73 KB
/
main.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
import sys
import typer
import click_spinner
from tabulate import tabulate
from utils import get_networks, get_wifi_port, run_cmd
from constants import TURN_WIFI_ON, TURN_WIFI_OFF, CONNECT_TO_NETWORK
app = typer.Typer(help="CLI wifi manager.")
@app.command()
def list():
"""
List available networks.
"""
headers, networks = get_networks()
typer.echo(tabulate(networks, headers, tablefmt="grid"))
@app.command()
def conn(ssid: str = '', password: str = ''):
"""
Connect to a wifi network.
"""
wifi_port = get_wifi_port()
if not ssid:
headers, networks = get_networks()
typer.echo(tabulate(networks, headers, tablefmt="grid"))
input_ssid = int(typer.prompt("SSID"))
if input_ssid not in range(1, len(networks) + 1):
typer.echo('Invalid option.')
sys.exit()
ssid = networks[input_ssid - 1][1]
if not password:
password = typer.prompt("Password", hide_input=True)
with click_spinner.spinner():
typer.echo(f"Connecting to {ssid}.")
output = run_cmd(CONNECT_TO_NETWORK.format(port=wifi_port, ssid=ssid, password=password))
if not output:
typer.echo("Connected to wifi.")
else:
typer.echo("Wrong ssid or password.")
@app.command()
def on():
"""
Turn wifi on.
"""
with click_spinner.spinner():
wifi_port = get_wifi_port()
run_cmd(TURN_WIFI_ON.format(port=wifi_port))
typer.echo("Wifi is on.")
@app.command()
def off():
"""
Turn wifi off.
"""
with click_spinner.spinner():
wifi_port = get_wifi_port()
run_cmd(TURN_WIFI_OFF.format(port=wifi_port))
typer.echo("Wifi is off.")
if __name__ == "__main__":
app()