Commit 3526ead8 authored by segfault's avatar segfault

Extract StringSetting and BooleanSetting classes (refs: #17136)

parent 698871b6
import logging
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.utils import read_settings, write_settings
from tailsgreeter.settings.setting import BooleanSetting
class MacSpoofSetting(object):
class MacSpoofSetting(BooleanSetting):
"""Setting controlling whether the MAC address is spoofed or not"""
def __init__(self):
self.settings_file = tailsgreeter.config.macspoof_setting_path
def save(self, value: bool):
write_settings(self.settings_file, {
'TAILS_MACSPOOF_ENABLED': value,
})
logging.debug('macspoof setting written to %s', self.settings_file)
def load(self) -> bool:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
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:
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
super().__init__(tailsgreeter.config.macspoof_setting_path, "TAILS_MACSPOOF_ENABLED")
import logging
import tailsgreeter.config
from tailsgreeter.settings.utils import read_settings, write_settings
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.setting import StringSetting
NETCONF_DIRECT = "direct"
NETCONF_OBSTACLE = "obstacle"
NETCONF_DISABLED = "disabled"
class NetworkSetting(object):
class NetworkSetting(StringSetting):
"""Setting controlling how Tails connects to Tor"""
def __init__(self):
self.settings_file = tailsgreeter.config.network_setting_path
def save(self, value: str):
write_settings(self.settings_file, {
'TAILS_NETCONF': value,
})
logging.debug('network setting written to %s', self.settings_file)
def load(self) -> str:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
raise SettingNotFoundError("No persistent network settings file found (path: %s)" % self.settings_file)
value = settings.get('TAILS_NETCONF')
if value is None:
raise SettingNotFoundError("No network setting found in settings file (path: %s)" % self.settings_file)
super().__init__(tailsgreeter.config.network_setting_path, "TAILS_NETCONF")
logging.debug("Loaded network setting '%s'", value)
return value
import logging
from typing import Union
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.utils import read_settings, write_settings
class Setting(object):
def __init__(self, settings_file, setting_name):
self.settings_file = settings_file
self.setting_name = setting_name
def save(self, value: Union[str, bool]):
write_settings(self.settings_file, {
self.setting_name: value,
})
logging.debug('setting %s written to %s', self.setting_name, self.settings_file)
def load(self) -> str:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
raise SettingNotFoundError("No persistent settings file found for %s (path: %s)" %
(self.setting_name, self.settings_file))
value_str = settings.get(self.setting_name)
if value_str is None:
raise SettingNotFoundError("No setting %s found in settings file (path: %s)" %
(self.setting_name, self.settings_file))
return value_str
class StringSetting(Setting):
def load(self) -> str:
value = super().load()
logging.debug("Loaded setting %s: '%s'", self.setting_name, value)
return value
class BooleanSetting(StringSetting):
def save(self, value: bool):
super().save(value)
def load(self) -> bool:
value_str = super().load()
value = value_str == "true"
logging.debug("Loaded setting %s: '%s'", self.setting_name, value)
return value
import logging
import tailsgreeter.config
from tailsgreeter.settings import SettingNotFoundError
from tailsgreeter.settings.utils import read_settings, write_settings
from tailsgreeter.settings.setting import BooleanSetting
class UnsafeBrowserSetting(object):
class UnsafeBrowserSetting(BooleanSetting):
"""Setting controlling whether the Unsafe Browser is available or not"""
def __init__(self):
self.settings_file = tailsgreeter.config.unsafe_browser_setting_path
def save(self, value: bool):
write_settings(self.settings_file, {
'TAILS_UNSAFE_BROWSER_ENABLED': value,
})
logging.debug('unsafe-browser setting written to %s', self.settings_file)
def load(self) -> bool:
try:
settings = read_settings(self.settings_file)
except FileNotFoundError:
raise SettingNotFoundError("No persistent unsafe-browser settings file found (path: %s)" % self.settings_file)
value_str = settings.get('TAILS_UNSAFE_BROWSER_ENABLED')
if value_str is None:
raise SettingNotFoundError("No unsafe-browser setting found in settings file (path: %s)" % self.settings_file)
value = value_str == "true"
logging.debug("Loaded unsafe-browser setting '%s'", value)
return value
super().__init__(tailsgreeter.config.unsafe_browser_setting_path, "TAILS_UNSAFE_BROWSER_ENABLED")
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