Skip to content

Commit 8b7eab7

Browse files
Claudeclaude
andcommitted
Fix lint issues and indentation style
- Fix linting errors by removing unused imports - Update indentation to use 2 spaces to match project style - Reformat code for consistency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 76930e6 commit 8b7eab7

File tree

2 files changed

+218
-220
lines changed

2 files changed

+218
-220
lines changed

gonotego/common/wifi.py

Lines changed: 149 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -6,171 +6,170 @@
66
import subprocess
77
import re
88
import time
9-
import os
109
from gonotego.settings import settings
1110

1211
def get_wifi_status():
13-
"""Get the current WiFi connection status.
12+
"""Get the current WiFi connection status.
13+
14+
Returns:
15+
dict: A dictionary containing WiFi status information including:
16+
- connected (bool): Whether we're connected to WiFi
17+
- ssid (str): The SSID of the connected network, if any
18+
- signal_strength (str): Signal strength as a percentage, if connected
19+
- ip_address (str): The IP address assigned to the WiFi interface, if connected
20+
"""
21+
status = {
22+
'connected': False,
23+
'ssid': None,
24+
'signal_strength': None,
25+
'ip_address': None
26+
}
27+
28+
try:
29+
# Use iwconfig to check WiFi status
30+
output = subprocess.check_output(['iwconfig', 'wlan0'], stderr=subprocess.STDOUT).decode('utf-8')
1431

15-
Returns:
16-
dict: A dictionary containing WiFi status information including:
17-
- connected (bool): Whether we're connected to WiFi
18-
- ssid (str): The SSID of the connected network, if any
19-
- signal_strength (str): Signal strength as a percentage, if connected
20-
- ip_address (str): The IP address assigned to the WiFi interface, if connected
21-
"""
22-
status = {
23-
'connected': False,
24-
'ssid': None,
25-
'signal_strength': None,
26-
'ip_address': None
27-
}
28-
29-
try:
30-
# Use iwconfig to check WiFi status
31-
output = subprocess.check_output(['iwconfig', 'wlan0'], stderr=subprocess.STDOUT).decode('utf-8')
32-
33-
# Check if connected to an ESSID
34-
ssid_match = re.search(r'ESSID:"([^"]*)"', output)
35-
if ssid_match and ssid_match.group(1) != "off/any":
36-
status['connected'] = True
37-
status['ssid'] = ssid_match.group(1)
38-
39-
# Get signal strength
40-
signal_match = re.search(r'Signal level=([0-9-]+)', output)
41-
if signal_match:
42-
# Convert dBm to percentage (rough estimation)
43-
dbm = int(signal_match.group(1))
44-
if dbm <= -100:
45-
percentage = 0
46-
elif dbm >= -50:
47-
percentage = 100
48-
else:
49-
percentage = 2 * (dbm + 100)
50-
status['signal_strength'] = f"{percentage}%"
51-
52-
# Get IP address
53-
try:
54-
ip_output = subprocess.check_output(['hostname', '-I']).decode('utf-8').strip()
55-
if ip_output:
56-
status['ip_address'] = ip_output.split()[0] # First IP is usually WiFi
57-
except subprocess.CalledProcessError:
58-
pass
59-
except (subprocess.CalledProcessError, FileNotFoundError, IndexError) as e:
60-
# Log the error but return empty status
61-
print(f"Error getting WiFi status: {e}")
62-
63-
return status
32+
# Check if connected to an ESSID
33+
ssid_match = re.search(r'ESSID:"([^"]*)"', output)
34+
if ssid_match and ssid_match.group(1) != "off/any":
35+
status['connected'] = True
36+
status['ssid'] = ssid_match.group(1)
37+
38+
# Get signal strength
39+
signal_match = re.search(r'Signal level=([0-9-]+)', output)
40+
if signal_match:
41+
# Convert dBm to percentage (rough estimation)
42+
dbm = int(signal_match.group(1))
43+
if dbm <= -100:
44+
percentage = 0
45+
elif dbm >= -50:
46+
percentage = 100
47+
else:
48+
percentage = 2 * (dbm + 100)
49+
status['signal_strength'] = f"{percentage}%"
50+
51+
# Get IP address
52+
try:
53+
ip_output = subprocess.check_output(['hostname', '-I']).decode('utf-8').strip()
54+
if ip_output:
55+
status['ip_address'] = ip_output.split()[0] # First IP is usually WiFi
56+
except subprocess.CalledProcessError:
57+
pass
58+
except (subprocess.CalledProcessError, FileNotFoundError, IndexError) as e:
59+
# Log the error but return empty status
60+
print(f"Error getting WiFi status: {e}")
61+
62+
return status
6463

6564
def scan_networks():
66-
"""Scan for available WiFi networks.
65+
"""Scan for available WiFi networks.
66+
67+
Returns:
68+
list: A list of dictionaries, each containing information about an available network:
69+
- ssid (str): The network name
70+
- signal_strength (str): Signal strength as a percentage
71+
- encryption (str): The encryption type (WPA, WEP, Open)
72+
"""
73+
networks = []
74+
75+
try:
76+
# Use iwlist to scan for networks
77+
output = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
6778

68-
Returns:
69-
list: A list of dictionaries, each containing information about an available network:
70-
- ssid (str): The network name
71-
- signal_strength (str): Signal strength as a percentage
72-
- encryption (str): The encryption type (WPA, WEP, Open)
73-
"""
74-
networks = []
75-
76-
try:
77-
# Use iwlist to scan for networks
78-
output = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
79+
# Parse the output to extract networks
80+
cells = output.split('Cell ')
81+
for cell in cells[1:]: # Skip the first element which is header
82+
ssid_match = re.search(r'ESSID:"([^"]*)"', cell)
83+
signal_match = re.search(r'Quality=([0-9]+)/([0-9]+)', cell)
84+
encryption_match = re.search(r'Encryption key:(on|off)', cell)
85+
86+
if ssid_match:
87+
network = {
88+
'ssid': ssid_match.group(1),
89+
'signal_strength': None,
90+
'encryption': 'Unknown'
91+
}
7992

80-
# Parse the output to extract networks
81-
cells = output.split('Cell ')
82-
for cell in cells[1:]: # Skip the first element which is header
83-
ssid_match = re.search(r'ESSID:"([^"]*)"', cell)
84-
signal_match = re.search(r'Quality=([0-9]+)/([0-9]+)', cell)
85-
encryption_match = re.search(r'Encryption key:(on|off)', cell)
86-
87-
if ssid_match:
88-
network = {
89-
'ssid': ssid_match.group(1),
90-
'signal_strength': None,
91-
'encryption': 'Unknown'
92-
}
93-
94-
# Parse signal strength
95-
if signal_match:
96-
quality = int(signal_match.group(1))
97-
max_quality = int(signal_match.group(2))
98-
percentage = int((quality / max_quality) * 100)
99-
network['signal_strength'] = f"{percentage}%"
100-
101-
# Parse encryption
102-
if encryption_match:
103-
if encryption_match.group(1) == 'on':
104-
if 'WPA' in cell:
105-
network['encryption'] = 'WPA'
106-
else:
107-
network['encryption'] = 'WEP'
108-
else:
109-
network['encryption'] = 'Open'
110-
111-
networks.append(network)
112-
except (subprocess.CalledProcessError, FileNotFoundError) as e:
113-
print(f"Error scanning WiFi networks: {e}")
114-
115-
return networks
93+
# Parse signal strength
94+
if signal_match:
95+
quality = int(signal_match.group(1))
96+
max_quality = int(signal_match.group(2))
97+
percentage = int((quality / max_quality) * 100)
98+
network['signal_strength'] = f"{percentage}%"
99+
100+
# Parse encryption
101+
if encryption_match:
102+
if encryption_match.group(1) == 'on':
103+
if 'WPA' in cell:
104+
network['encryption'] = 'WPA'
105+
else:
106+
network['encryption'] = 'WEP'
107+
else:
108+
network['encryption'] = 'Open'
109+
110+
networks.append(network)
111+
except (subprocess.CalledProcessError, FileNotFoundError) as e:
112+
print(f"Error scanning WiFi networks: {e}")
113+
114+
return networks
116115

117116
def connect_to_wifi(ssid, password):
118-
"""Connect to a WiFi network.
117+
"""Connect to a WiFi network.
118+
119+
Args:
120+
ssid (str): The name of the network to connect to
121+
password (str): The password for the network
119122
120-
Args:
121-
ssid (str): The name of the network to connect to
122-
password (str): The password for the network
123-
124-
Returns:
125-
bool: True if the connection was successful, False otherwise
126-
"""
127-
try:
128-
# Create a wpa_supplicant configuration
129-
wpa_config = f'''
123+
Returns:
124+
bool: True if the connection was successful, False otherwise
125+
"""
126+
try:
127+
# Create a wpa_supplicant configuration
128+
wpa_config = f'''
130129
network={{
131-
ssid="{ssid}"
132-
psk="{password}"
130+
ssid="{ssid}"
131+
psk="{password}"
133132
}}
134133
'''
135-
# Write the configuration to a temporary file
136-
with open('/tmp/wpa_supplicant.conf', 'w') as f:
137-
f.write(wpa_config)
138-
139-
# Apply the configuration
140-
subprocess.check_call(['sudo', 'cp', '/tmp/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf'])
141-
142-
# Restart networking
143-
subprocess.check_call(['sudo', 'systemctl', 'restart', 'wpa_supplicant'])
144-
subprocess.check_call(['sudo', 'systemctl', 'restart', 'networking'])
145-
146-
# Wait for the connection to establish
147-
time.sleep(5)
148-
149-
# Check if connection was successful
150-
status = get_wifi_status()
151-
return status['connected'] and status['ssid'] == ssid
152-
except Exception as e:
153-
print(f"Error connecting to WiFi: {e}")
154-
return False
134+
# Write the configuration to a temporary file
135+
with open('/tmp/wpa_supplicant.conf', 'w') as f:
136+
f.write(wpa_config)
137+
138+
# Apply the configuration
139+
subprocess.check_call(['sudo', 'cp', '/tmp/wpa_supplicant.conf', '/etc/wpa_supplicant/wpa_supplicant.conf'])
140+
141+
# Restart networking
142+
subprocess.check_call(['sudo', 'systemctl', 'restart', 'wpa_supplicant'])
143+
subprocess.check_call(['sudo', 'systemctl', 'restart', 'networking'])
144+
145+
# Wait for the connection to establish
146+
time.sleep(5)
147+
148+
# Check if connection was successful
149+
status = get_wifi_status()
150+
return status['connected'] and status['ssid'] == ssid
151+
except Exception as e:
152+
print(f"Error connecting to WiFi: {e}")
153+
return False
155154

156155
def apply_wifi_settings():
157-
"""Apply WiFi settings from the settings module.
156+
"""Apply WiFi settings from the settings module.
157+
158+
This function retrieves the WIFI_SSID and WIFI_PASSWORD from settings
159+
and attempts to connect to the specified network.
160+
161+
Returns:
162+
bool: True if the connection was successful, False otherwise
163+
"""
164+
try:
165+
ssid = settings.get('WIFI_SSID')
166+
password = settings.get('WIFI_PASSWORD')
158167

159-
This function retrieves the WIFI_SSID and WIFI_PASSWORD from settings
160-
and attempts to connect to the specified network.
168+
if not ssid:
169+
print("No WiFi SSID configured in settings")
170+
return False
161171

162-
Returns:
163-
bool: True if the connection was successful, False otherwise
164-
"""
165-
try:
166-
ssid = settings.get('WIFI_SSID')
167-
password = settings.get('WIFI_PASSWORD')
168-
169-
if not ssid:
170-
print("No WiFi SSID configured in settings")
171-
return False
172-
173-
return connect_to_wifi(ssid, password)
174-
except Exception as e:
175-
print(f"Error applying WiFi settings: {e}")
176-
return False
172+
return connect_to_wifi(ssid, password)
173+
except Exception as e:
174+
print(f"Error applying WiFi settings: {e}")
175+
return False

0 commit comments

Comments
 (0)