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,10 +43,51 @@ 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):
return None
with open(p) as fl: with open(p) as fl:
data = fl.read() data = fl.read()
try: try:
@ -62,7 +101,6 @@ class Settings(dict):
path = join_path(path, curr_directory(__file__)) path = join_path(path, curr_directory(__file__))
if os.path.isfile(path): if os.path.isfile(path):
return path return path
return None
@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]