Skip to content

Commit bdd6e86

Browse files
dbieberclaude
andcommitted
Move WiFi commands to separate module and fix circular imports
- Create wifi_commands.py for all WiFi-related commands - Remove WiFi commands from system_commands.py - Update commands.py to import wifi_commands - Update wifi.py to import system_commands directly - Use shell = system_commands.shell for cleaner code 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 837b45a commit bdd6e86

File tree

4 files changed

+129
-122
lines changed

4 files changed

+129
-122
lines changed

gonotego/command_center/commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from gonotego.command_center import settings_commands # noqa: F401
1212
from gonotego.command_center import system_commands # noqa: F401
1313
from gonotego.command_center import twitter_commands # noqa: F401
14+
from gonotego.command_center import wifi_commands # noqa: F401
1415
from gonotego.command_center import registry
1516

1617
register_command = registry.register_command

gonotego/command_center/system_commands.py

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from gonotego.command_center import note_commands
1212
from gonotego.command_center import registry
1313
from gonotego.settings import settings
14-
from gonotego.settings import wifi
1514

1615
register_command = registry.register_command
1716

@@ -148,125 +147,6 @@ def check_internet():
148147
say('yes' if internet.is_internet_available() else 'no')
149148

150149

151-
@register_command('wifi {} {}')
152-
@register_command('wpa {} {}')
153-
def add_wpa_wifi(ssid, psk):
154-
if '"' in ssid or '"' in psk:
155-
say('WiFi not set.')
156-
return
157-
158-
# Load existing networks
159-
networks = wifi.get_networks()
160-
161-
# Check if this network already exists
162-
for network in networks:
163-
if network.get('ssid') == ssid:
164-
network['psk'] = psk
165-
break
166-
else:
167-
# Network doesn't exist, add it
168-
networks.append({'ssid': ssid, 'psk': psk})
169-
170-
# Save updated networks
171-
wifi.save_networks(networks)
172-
173-
# Update wpa_supplicant.conf
174-
if wifi.update_wpa_supplicant_config():
175-
wifi.reconfigure_wifi()
176-
say(f'WiFi network {ssid} added.')
177-
else:
178-
say('Failed to update WiFi configuration.')
179-
180-
181-
@register_command('wifi {}')
182-
def add_wifi_no_psk(ssid):
183-
if '"' in ssid:
184-
say('WiFi not set.')
185-
return
186-
187-
# Load existing networks
188-
networks = wifi.get_networks()
189-
190-
# Check if this network already exists
191-
for network in networks:
192-
if network.get('ssid') == ssid:
193-
network.pop('psk', None) # Remove password if it exists
194-
break
195-
else:
196-
# Network doesn't exist, add it
197-
networks.append({'ssid': ssid})
198-
199-
# Save updated networks
200-
wifi.save_networks(networks)
201-
202-
# Update wpa_supplicant.conf
203-
if wifi.update_wpa_supplicant_config():
204-
wifi.reconfigure_wifi()
205-
say(f'Open WiFi network {ssid} added.')
206-
else:
207-
say('Failed to update WiFi configuration.')
208-
209-
210-
@register_command('wifi-list')
211-
def list_wifi_networks():
212-
networks = wifi.get_networks()
213-
if not networks:
214-
say('No WiFi networks configured.')
215-
return
216-
217-
network_list = [f"{i+1}. {network['ssid']}" for i, network in enumerate(networks)]
218-
say('Configured WiFi networks: ' + ', '.join(network_list))
219-
220-
221-
@register_command('wifi-migrate')
222-
def migrate_wifi_networks():
223-
"""Scan wpa_supplicant.conf and migrate existing networks to Redis."""
224-
networks = wifi.migrate_networks_from_wpa_supplicant()
225-
226-
if networks is None:
227-
say('WiFi configuration file not found or error reading it.')
228-
return
229-
230-
# Save the extracted networks to Redis
231-
if networks:
232-
wifi.save_networks(networks)
233-
say(f'Migrated {len(networks)} WiFi networks to settings.')
234-
235-
# Update wpa_supplicant.conf
236-
wifi.update_wpa_supplicant_config()
237-
else:
238-
say('No WiFi networks found to migrate.')
239-
240-
241-
@register_command('wifi-remove {}')
242-
def remove_wifi_network(ssid):
243-
networks = wifi.get_networks()
244-
initial_count = len(networks)
245-
246-
# Remove networks with matching SSID
247-
networks = [network for network in networks if network.get('ssid') != ssid]
248-
249-
if len(networks) < initial_count:
250-
# Save updated networks
251-
wifi.save_networks(networks)
252-
253-
# Update wpa_supplicant.conf
254-
if wifi.update_wpa_supplicant_config():
255-
wifi.reconfigure_wifi()
256-
say(f'WiFi network {ssid} removed.')
257-
else:
258-
say('Failed to update WiFi configuration.')
259-
else:
260-
say(f'WiFi network {ssid} not found.')
261-
262-
263-
@register_command('reconnect')
264-
@register_command('wifi-refresh')
265-
@register_command('wifi-reconfigure')
266-
def reconfigure_wifi():
267-
wifi.reconfigure_wifi()
268-
269-
270150
@register_command('server')
271151
@register_command('settings')
272152
@register_command('configure')
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""WiFi command handlers for Go Note Go."""
2+
from gonotego.command_center import registry
3+
from gonotego.settings import wifi
4+
from gonotego.command_center.system_commands import say, shell
5+
6+
register_command = registry.register_command
7+
8+
9+
@register_command('wifi {} {}')
10+
@register_command('wpa {} {}')
11+
def add_wpa_wifi(ssid, psk):
12+
if '"' in ssid or '"' in psk:
13+
say('WiFi not set.')
14+
return
15+
16+
# Load existing networks
17+
networks = wifi.get_networks()
18+
19+
# Check if this network already exists
20+
for network in networks:
21+
if network.get('ssid') == ssid:
22+
network['psk'] = psk
23+
break
24+
else:
25+
# Network doesn't exist, add it
26+
networks.append({'ssid': ssid, 'psk': psk})
27+
28+
# Save updated networks
29+
wifi.save_networks(networks)
30+
31+
# Update wpa_supplicant.conf
32+
if wifi.update_wpa_supplicant_config():
33+
wifi.reconfigure_wifi()
34+
say(f'WiFi network {ssid} added.')
35+
else:
36+
say('Failed to update WiFi configuration.')
37+
38+
39+
@register_command('wifi {}')
40+
def add_wifi_no_psk(ssid):
41+
if '"' in ssid:
42+
say('WiFi not set.')
43+
return
44+
45+
# Load existing networks
46+
networks = wifi.get_networks()
47+
48+
# Check if this network already exists
49+
for network in networks:
50+
if network.get('ssid') == ssid:
51+
network.pop('psk', None) # Remove password if it exists
52+
break
53+
else:
54+
# Network doesn't exist, add it
55+
networks.append({'ssid': ssid})
56+
57+
# Save updated networks
58+
wifi.save_networks(networks)
59+
60+
# Update wpa_supplicant.conf
61+
if wifi.update_wpa_supplicant_config():
62+
wifi.reconfigure_wifi()
63+
say(f'Open WiFi network {ssid} added.')
64+
else:
65+
say('Failed to update WiFi configuration.')
66+
67+
68+
@register_command('wifi-list')
69+
def list_wifi_networks():
70+
networks = wifi.get_networks()
71+
if not networks:
72+
say('No WiFi networks configured.')
73+
return
74+
75+
network_list = [f"{i+1}. {network['ssid']}" for i, network in enumerate(networks)]
76+
say('Configured WiFi networks: ' + ', '.join(network_list))
77+
78+
79+
@register_command('wifi-migrate')
80+
def migrate_wifi_networks():
81+
"""Scan wpa_supplicant.conf and migrate existing networks to Redis."""
82+
networks = wifi.migrate_networks_from_wpa_supplicant()
83+
84+
if networks is None:
85+
say('WiFi configuration file not found or error reading it.')
86+
return
87+
88+
# Save the extracted networks to Redis
89+
if networks:
90+
wifi.save_networks(networks)
91+
say(f'Migrated {len(networks)} WiFi networks to settings.')
92+
93+
# Update wpa_supplicant.conf
94+
wifi.update_wpa_supplicant_config()
95+
else:
96+
say('No WiFi networks found to migrate.')
97+
98+
99+
@register_command('wifi-remove {}')
100+
def remove_wifi_network(ssid):
101+
networks = wifi.get_networks()
102+
initial_count = len(networks)
103+
104+
# Remove networks with matching SSID
105+
networks = [network for network in networks if network.get('ssid') != ssid]
106+
107+
if len(networks) < initial_count:
108+
# Save updated networks
109+
wifi.save_networks(networks)
110+
111+
# Update wpa_supplicant.conf
112+
if wifi.update_wpa_supplicant_config():
113+
wifi.reconfigure_wifi()
114+
say(f'WiFi network {ssid} removed.')
115+
else:
116+
say('Failed to update WiFi configuration.')
117+
else:
118+
say(f'WiFi network {ssid} not found.')
119+
120+
121+
@register_command('reconnect')
122+
@register_command('wifi-refresh')
123+
@register_command('wifi-reconfigure')
124+
def reconfigure_wifi():
125+
wifi.reconfigure_wifi()

gonotego/settings/wifi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import re
55
from gonotego.settings import settings
66
from gonotego.common import interprocess
7+
from gonotego.command_center import system_commands
8+
9+
shell = system_commands.shell
710

811

912
def get_networks():
@@ -82,13 +85,11 @@ def update_wpa_supplicant_config():
8285

8386
def reconfigure_wifi():
8487
"""Reconfigure WiFi to apply changes."""
85-
from gonotego.command_center.system_commands import shell # Import here to avoid circular import
8688
shell('wpa_cli -i wlan0 reconfigure')
8789

8890

8991
def migrate_networks_from_wpa_supplicant():
9092
"""Scan wpa_supplicant.conf and migrate existing networks to Redis."""
91-
from gonotego.command_center.system_commands import shell # Import here to avoid circular import
9293

9394
filepath = '/etc/wpa_supplicant/wpa_supplicant.conf'
9495

0 commit comments

Comments
 (0)