Skip to content

Commit 2f6db18

Browse files
committed
Make script more robust
1 parent 63f074b commit 2f6db18

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

bin/changewifisettings.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,26 @@ def is_pi3():
8484
dhcpcd_conf_file = "/etc/dhcpcd.conf"
8585

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

105108
# Action functions.
106109

0 commit comments

Comments
 (0)