From 8f9b57325316b0b9d47a55dfe055cb14d850a9fd Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sun, 5 Aug 2018 11:35:24 +0300 Subject: [PATCH] settings.py refactoring --- toxygen/user_data/profile_manager.py | 3 +- toxygen/user_data/settings.py | 124 +++++++++++++++------------ 2 files changed, 68 insertions(+), 59 deletions(-) diff --git a/toxygen/user_data/profile_manager.py b/toxygen/user_data/profile_manager.py index 3324fce..12e998f 100644 --- a/toxygen/user_data/profile_manager.py +++ b/toxygen/user_data/profile_manager.py @@ -49,7 +49,7 @@ class ProfileManager: if use_new_path: self._path = new_path + os.path.basename(self._path) self._directory = new_path - self._settings.update_path() + self._settings.update_path(new_path) @staticmethod def find_profiles(): @@ -72,4 +72,3 @@ class ProfileManager: name = fl[:-4] result.append((path + '/', name)) return result - diff --git a/toxygen/user_data/settings.py b/toxygen/user_data/settings.py index d375f92..768a07e 100644 --- a/toxygen/user_data/settings.py +++ b/toxygen/user_data/settings.py @@ -1,7 +1,6 @@ import json from utils.util import * import pyaudio -import smileys.smileys as smileys class Settings(dict): @@ -24,11 +23,10 @@ class Settings(dict): info = Settings.get_default_settings() log('Parsing settings error: ' + str(ex)) super().__init__(info) - self.upgrade() + self._upgrade() else: super().__init__(Settings.get_default_settings()) - self.save() - smileys.SmileyLoader(self) + self.save() self.locked = False self.closing = False self.unlockScreen = False @@ -45,24 +43,64 @@ class Settings(dict): 'enabled': input_devices and output_devices} self.video = {'device': -1, 'width': 640, 'height': 480, 'x': 0, 'y': 0} + # ----------------------------------------------------------------------------------------------------------------- + # Public methods + # ----------------------------------------------------------------------------------------------------------------- + + def save(self): + text = json.dumps(self) + if self._toxes.has_password(): + text = bytes(self._toxes.pass_encrypt(bytes(text, 'utf-8'))) + else: + text = bytes(text, 'utf-8') + with open(self._path, 'wb') as fl: + fl.write(text) + + def close(self): + path = self._profile_path + '.lock' + if os.path.isfile(path): + os.remove(path) + + def set_active_profile(self): + """ + Mark current profile as active + """ + path = self._profile_path + '.lock' + with open(path, 'w') as fl: + fl.write('active') + + def export(self, path): + text = json.dumps(self) + name = os.path.basename(self._path) + with open(join_path(path, str(name)), 'w') as fl: + fl.write(text) + + def update_path(self, new_path): + self._path = new_path + self.save() + + # ----------------------------------------------------------------------------------------------------------------- + # Static methods + # ----------------------------------------------------------------------------------------------------------------- + @staticmethod def get_auto_profile(): p = Settings.get_global_settings_path() - if os.path.isfile(p): - with open(p) as fl: - data = fl.read() - try: - auto = json.loads(data) - except Exception as ex: - log(str(ex)) - auto = {} - if 'profile_path' in auto: - path = str(auto['profile_path']) - if not os.path.isabs(path): - path = join_path(path, curr_directory(__file__)) - if os.path.isfile(path): - return path - return None + if not os.path.isfile(p): + return None + with open(p) as fl: + data = fl.read() + try: + auto = json.loads(data) + except Exception as ex: + log(str(ex)) + auto = {} + if 'profile_path' in auto: + path = str(auto['profile_path']) + if not os.path.isabs(path): + path = join_path(path, curr_directory(__file__)) + if os.path.isfile(path): + return path @staticmethod def set_auto_profile(path): @@ -167,44 +205,6 @@ class Settings(dict): 'default': 'style.qss' } - def upgrade(self): - default = Settings.get_default_settings() - for key in default: - if key not in self: - print(key) - self[key] = default[key] - self.save() - - def save(self): - text = json.dumps(self) - if self._toxes.has_password(): - text = bytes(self._toxes.pass_encrypt(bytes(text, 'utf-8'))) - else: - text = bytes(text, 'utf-8') - with open(self._path, 'wb') as fl: - fl.write(text) - - def close(self): - path = self._profile_path + '.lock' - if os.path.isfile(path): - os.remove(path) - - def set_active_profile(self): - """ - Mark current profile as active - """ - path = self._profile_path + '.lock' - with open(path, 'w') as fl: - fl.write('active') - - def export(self, path): - text = json.dumps(self) - with open(path + str(self.name) + '.json', 'w') as fl: - fl.write(text) - - def update_path(self): - self.path = ProfileManager.get_path() + self.name + '.json' - @staticmethod def get_global_settings_path(): return os.path.join(get_base_directory(), 'toxygen.json') @@ -219,3 +219,13 @@ class Settings(dict): else: return os.getenv('HOME') + '/.config/tox/' + # ----------------------------------------------------------------------------------------------------------------- + # Private methods + # ----------------------------------------------------------------------------------------------------------------- + + def _upgrade(self): + default = Settings.get_default_settings() + for key in default: + if key not in self: + print(key) + self[key] = default[key]