Skip to content

Commit 63f074b

Browse files
committed
Make script more robust
1 parent 9f5e0af commit 63f074b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

bin/changewifisettings.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,22 @@ def is_pi3():
8585

8686
# New values taken from settings_file.
8787
with open(settings_file, 'r') as f:
88-
config_string = '[dummy_section]\n' + f.read()
88+
# Inject section manually and read all file.
89+
# Needed as configparser 3.11 cannot read INI files without sections.
90+
config_string = '[wifi]\n' + f.read()
8991
settings = configparser.ConfigParser()
90-
settings.read_string(config_string)
91-
new_channel = settings['dummy_section']['channel']
92-
new_country = settings['dummy_section']['country']
93-
new_password = settings['dummy_section']['password']
94-
new_ssid = settings['dummy_section']['ssid']
95-
password_protected = settings['dummy_section']['passwordprotected']
96-
ssid_hidden_state = settings['dummy_section']['ssidhiddenstate']
97-
new_static_ip = settings['dummy_section']['ipaddress']
92+
try:
93+
settings.read_string(config_string)
94+
section = settings['wifi']
95+
new_channel = section.get('channel', default_channel)
96+
new_country = section.get('country', default_country)
97+
new_password = section.get('password', default_password)
98+
new_ssid = section.get('ssid', default_ssid)
99+
password_protected = section.get('passwordprotected', '1')
100+
ssid_hidden_state = section.get('ssidhiddenstate', '0')
101+
new_static_ip = section.get('ipaddress', default_ip_address)
102+
except (KeyError, configparser.Error) as e:
103+
sys.exit(f"Invalid settings file: {e}")
98104

99105
# Action functions.
100106

0 commit comments

Comments
 (0)