settings.py refactoring

This commit is contained in:
ingvar1995 2018-08-05 11:35:24 +03:00
parent 9f702339dd
commit 8f9b573253
2 changed files with 68 additions and 59 deletions

View File

@ -49,7 +49,7 @@ class ProfileManager:
if use_new_path: if use_new_path:
self._path = new_path + os.path.basename(self._path) self._path = new_path + os.path.basename(self._path)
self._directory = new_path self._directory = new_path
self._settings.update_path() self._settings.update_path(new_path)
@staticmethod @staticmethod
def find_profiles(): def find_profiles():
@ -72,4 +72,3 @@ class ProfileManager:
name = fl[:-4] name = fl[:-4]
result.append((path + '/', name)) result.append((path + '/', name))
return result return result

View File

@ -1,7 +1,6 @@
import json import json
from utils.util import * from utils.util import *
import pyaudio import pyaudio
import smileys.smileys as smileys
class Settings(dict): class Settings(dict):
@ -24,11 +23,10 @@ class Settings(dict):
info = Settings.get_default_settings() info = Settings.get_default_settings()
log('Parsing settings error: ' + str(ex)) log('Parsing settings error: ' + str(ex))
super().__init__(info) super().__init__(info)
self.upgrade() self._upgrade()
else: else:
super().__init__(Settings.get_default_settings()) super().__init__(Settings.get_default_settings())
self.save() self.save()
smileys.SmileyLoader(self)
self.locked = False self.locked = False
self.closing = False self.closing = False
self.unlockScreen = False self.unlockScreen = False
@ -45,24 +43,64 @@ class Settings(dict):
'enabled': input_devices and output_devices} 'enabled': input_devices and output_devices}
self.video = {'device': -1, 'width': 640, 'height': 480, 'x': 0, 'y': 0} 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 @staticmethod
def get_auto_profile(): def get_auto_profile():
p = Settings.get_global_settings_path() p = Settings.get_global_settings_path()
if os.path.isfile(p): if not os.path.isfile(p):
with open(p) as fl: return None
data = fl.read() with open(p) as fl:
try: data = fl.read()
auto = json.loads(data) try:
except Exception as ex: auto = json.loads(data)
log(str(ex)) except Exception as ex:
auto = {} log(str(ex))
if 'profile_path' in auto: auto = {}
path = str(auto['profile_path']) if 'profile_path' in auto:
if not os.path.isabs(path): path = str(auto['profile_path'])
path = join_path(path, curr_directory(__file__)) if not os.path.isabs(path):
if os.path.isfile(path): path = join_path(path, curr_directory(__file__))
return path if os.path.isfile(path):
return None return path
@staticmethod @staticmethod
def set_auto_profile(path): def set_auto_profile(path):
@ -167,44 +205,6 @@ class Settings(dict):
'default': 'style.qss' '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 @staticmethod
def get_global_settings_path(): def get_global_settings_path():
return os.path.join(get_base_directory(), 'toxygen.json') return os.path.join(get_base_directory(), 'toxygen.json')
@ -219,3 +219,13 @@ class Settings(dict):
else: else:
return os.getenv('HOME') + '/.config/tox/' 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]