Commit f64d4e53 authored by segfault's avatar segfault

Use exceptions instead of False and None to indicate failure (refs: #17136)

Also fixes a bug which caused the "Settings loaded" infobar to not be
shown if only region settings were loaded (the LocalizationSettingUI.load()
implementation did not return True if a setting was loaded).
parent 8f738eb7
......@@ -5,6 +5,7 @@ import shlex
import subprocess
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.utils import read_settings, write_settings
......@@ -41,14 +42,13 @@ class AdminSetting(object):
if os.path.exists(self.settings_file):
raise
def load(self) -> {True, None}:
def load(self):
# We don't return the stored value, because the UI can't do
# anything with it since it's hashed.
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent admin settings file found (path: %s)", self.settings_file)
return None
raise SettingNotFoundError("No persistent admin settings file found (path: %s)", self.settings_file)
# We don't actually return the stored value, because the UI can't do
# anything with it since it's hashed. Instead, we just return whether
# a value is stored or not.
return bool(settings.get('TAILS_USER_PASSWORD'))
if settings.get('TAILS_USER_PASSWORD') is None:
raise SettingNotFoundError("No admin password setting found in settings file (path: %s)", self.settings_file)
......@@ -2,6 +2,7 @@ import gi
import logging
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.localization import LocalizationSetting, language_from_locale, country_from_locale
from tailsgreeter.settings.utils import read_settings, write_settings
......@@ -23,17 +24,15 @@ class FormatsSetting(LocalizationSetting):
'IS_DEFAULT': is_default,
})
def load(self) -> ({str, None}, bool):
def load(self) -> (str, bool):
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent formats settings file found (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No persistent formats settings file found (path: %s)", self.settings_file)
formats = settings.get('TAILS_FORMATS')
if formats is None:
logging.debug("No formats setting found in settings file (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No formats setting found in settings file (path: %s)", self.settings_file)
is_default = settings.get('IS_DEFAULT') == 'true'
logging.debug("Loaded formats setting '%s' (is default: %s)", formats, is_default)
......
......@@ -2,6 +2,7 @@ import gi
import logging
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.localization import LocalizationSetting, ln_iso639_tri, \
ln_iso639_2_T_to_B, language_from_locale, country_from_locale
from tailsgreeter.settings.utils import read_settings, write_settings
......@@ -36,17 +37,15 @@ class KeyboardSetting(LocalizationSetting):
'IS_DEFAULT': is_default,
})
def load(self) -> ({str, None}, bool):
def load(self) -> (str, bool):
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent keyboard settings file found (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No persistent keyboard settings file found (path: %s)", self.settings_file)
keyboard_layout = settings.get('TAILS_XKBLAYOUT')
if keyboard_layout is None:
logging.debug("No keyboard setting found in settings file (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No keyboard setting found in settings file (path: %s)", self.settings_file)
keyboard_variant = settings.get('TAILS_XKBVARIANT')
if keyboard_variant:
......
......@@ -23,6 +23,7 @@ import logging
import locale
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.localization import LocalizationSetting, \
language_from_locale, country_from_locale
from tailsgreeter.settings.utils import read_settings, write_settings
......@@ -52,17 +53,15 @@ class LanguageSetting(LocalizationSetting):
'IS_DEFAULT': is_default,
})
def load(self) -> ({str, None}, bool):
def load(self) -> (str, bool):
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent language settings file found (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No persistent language settings file found (path: %s)", self.settings_file)
language = settings.get('TAILS_LOCALE_NAME')
if language is None:
logging.debug("No language setting found in settings file (path: %s)", self.settings_file)
return None, False
raise SettingNotFoundError("No language setting found in settings file (path: %s)", self.settings_file)
is_default = settings.get('IS_DEFAULT') == 'true'
logging.debug("Loaded language setting '%s' (is default: %s)", language, is_default)
......
......@@ -47,7 +47,7 @@ class LocalizationSetting(GObject.Object, object):
def save(self, value: str, is_default: bool):
pass
def load(self) -> ({str, None}, bool):
def load(self) -> (str, bool):
pass
......
import logging
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.utils import read_settings, write_settings
......@@ -16,17 +17,16 @@ class MacSpoofSetting(object):
})
logging.debug('macspoof setting written to %s', self.settings_file)
def load(self) -> {bool, None}:
def load(self) -> bool:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent macspoof settings file found (path: %s)", self.settings_file)
return None
raise SettingNotFoundError("No persistent macspoof settings file found (path: %s)", self.settings_file)
value_str = settings.get('TAILS_MACSPOOF_ENABLED')
if value_str is None:
logging.debug("No macspoof setting found in settings file (path: %s)", self.settings_file)
return None
raise SettingNotFoundError("No macspoof setting found in settings file (path: %s)", self.settings_file)
value = value_str == "true"
logging.debug("Loaded macspoof setting '%s'", value)
return value
......@@ -2,6 +2,7 @@ import logging
import tailsgreeter.config
from tailsgreeter.settings.utils import read_settings, write_settings
from tailsgreeter.settings import SettingNotFoundError
NETCONF_DIRECT = "direct"
NETCONF_OBSTACLE = "obstacle"
......@@ -20,17 +21,15 @@ class NetworkSetting(object):
})
logging.debug('network setting written to %s', self.settings_file)
def load(self) -> {bool, None}:
def load(self) -> str:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
logging.debug("No persistent network settings file found (path: %s)", self.settings_file)
return None
raise SettingNotFoundError("No persistent network settings file found (path: %s)" % self.settings_file)
value = settings.get('TAILS_NETCONF')
if value is None:
logging.debug("No network setting found in settings file (path: %s)", self.settings_file)
return None
raise SettingNotFoundError("No network setting found in settings file (path: %s)", self.settings_file)
logging.debug("Loaded network setting '%s'", value)
return value
......@@ -3,6 +3,7 @@ import gi
from tailsgreeter import TRANSLATION_DOMAIN
import tailsgreeter.config
import tailsgreeter.utils
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.network import NETCONF_DIRECT, NETCONF_DISABLED, NETCONF_OBSTACLE
from tailsgreeter.ui import _
from tailsgreeter.ui.setting import GreeterSetting
......@@ -43,7 +44,7 @@ class AdditionalSetting(GreeterSetting):
def on_opened_in_dialog(self):
pass
def load(self) -> bool:
def load(self):
pass
......@@ -129,12 +130,12 @@ class AdminSettingUI(AdditionalSetting):
self._admin_setting.delete()
super().apply()
def load(self) -> bool:
loaded = self._admin_setting.load()
if loaded:
self.password = True
return True
return False
def load(self):
try:
self._admin_setting.load()
except SettingNotFoundError:
raise
self.password = True
def cb_entry_admin_changed(self, editable, user_data=None):
self.update_check_icon()
......@@ -200,14 +201,11 @@ class MACSpoofSettingUI(AdditionalSetting):
self._macspoof_setting.save(self.spoofing_enabled)
super().apply()
def load(self) -> bool:
value = self._macspoof_setting.load()
if value is None:
return False
if value == self.spoofing_enabled:
return False
self.spoofing_enabled = value
return True
def load(self):
try:
self.spoofing_enabled = self._macspoof_setting.load()
except SettingNotFoundError:
raise
def cb_listbox_macspoof_row_activated(self, listbox, row, user_data=None):
self.spoofing_enabled = row == self.listboxrow_macspoof_on
......@@ -268,14 +266,11 @@ class NetworkSettingUI(AdditionalSetting):
self.main_window.set_bridge_infobar_visibility(is_bridge)
super().apply()
def load(self) -> bool:
value = self._network_setting.load()
if value is None:
return False
if value == self.value:
return False
self.value = value
return True
def load(self):
try:
self.value = self._network_setting.load()
except SettingNotFoundError:
raise
def cb_listbox_network_button_press(self, widget, event, user_data=None):
# On double-click: Close the window and apply chosen setting
......
......@@ -24,6 +24,7 @@ import os
import tailsgreeter # NOQA: E402
import tailsgreeter.config # NOQA: E402
import tailsgreeter.utils # NOQA: E402
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.translatable_window import TranslatableWindow
from tailsgreeter.ui.popover import Popover
from tailsgreeter.ui import _
......@@ -184,12 +185,19 @@ class GreeterMainWindow(Gtk.Window, TranslatableWindow):
# settings in reversed order.
settings_loaded = False
for setting in reversed(list(self.settings.region_settings)):
if setting.load():
try:
setting.load()
settings_loaded = True
except SettingNotFoundError as e:
logging.debug(e)
for setting in self.settings.additional_settings:
if setting.load():
try:
setting.load()
self.add_setting(setting.id)
settings_loaded = True
except SettingNotFoundError as e:
logging.debug(e)
if settings_loaded:
self.infobar_settings_loaded.set_visible(True)
......
......@@ -3,6 +3,7 @@ import gi
from tailsgreeter import TRANSLATION_DOMAIN
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.ui import _
from tailsgreeter.ui.setting import GreeterSetting
from tailsgreeter.ui.popover import Popover
......@@ -61,9 +62,10 @@ class LocalizationSettingUI(GreeterSetting):
super().apply()
def load(self):
value, is_default = self._setting.load()
if value is None:
return
try:
value, is_default = self._setting.load()
except SettingNotFoundError:
raise
self.value = value
self.value_changed_by_user = not is_default
self.apply()
......@@ -178,7 +180,10 @@ class LanguageSettingUI(LocalizationSettingUI):
self.changed_cb(self.value)
def load(self):
super().load()
try:
super().load()
except SettingNotFoundError:
raise
self._setting.apply_language(self.value)
self.changed_cb(self.value)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment