toxygen/src/settings.py

184 lines
5.8 KiB
Python
Raw Normal View History

2016-02-16 20:03:37 +00:00
from platform import system
2016-02-16 18:11:56 +00:00
import json
2016-02-16 19:57:45 +00:00
import os
2016-04-03 20:51:46 +00:00
from util import Singleton, curr_directory
2016-02-16 18:11:56 +00:00
class Settings(Singleton, dict):
2016-02-16 18:11:56 +00:00
2016-03-02 16:05:03 +00:00
def __init__(self, name=''):
2016-04-03 20:51:46 +00:00
self.path = ProfileHelper.get_path() + str(name) + '.json'
2016-03-13 12:06:06 +00:00
self.name = name
if os.path.isfile(self.path):
2016-02-17 19:04:39 +00:00
with open(self.path) as fl:
data = fl.read()
2016-02-17 21:16:44 +00:00
super(self.__class__, self).__init__(json.loads(data))
2016-02-17 19:04:39 +00:00
else:
2016-02-17 21:16:44 +00:00
super(self.__class__, self).__init__(Settings.get_default_settings())
2016-02-17 20:47:43 +00:00
self.save()
2016-02-17 19:04:39 +00:00
2016-03-02 16:05:03 +00:00
@staticmethod
def get_auto_profile():
path = Settings.get_default_path() + 'toxygen.json'
if os.path.isfile(path):
with open(path) as fl:
data = fl.read()
auto = json.loads(data)
2016-04-03 20:51:46 +00:00
if 'path' in auto and 'name' in auto:
return auto['path'], auto['name']
2016-03-02 16:05:03 +00:00
@staticmethod
def set_auto_profile(path, name):
p = Settings.get_default_path() + 'toxygen.json'
data = json.dumps({'path': path, 'name': name})
with open(p, 'w') as fl:
fl.write(data)
2016-02-17 21:16:44 +00:00
@staticmethod
def get_default_settings():
return {
2016-02-18 16:15:38 +00:00
'theme': 'default',
'ipv6_enabled': True,
'udp_enabled': True,
'proxy_type': 0,
'proxy_host': '0',
'proxy_port': 0,
'start_port': 0,
'end_port': 0,
'tcp_port': 0,
'notifications': True,
'sound_notifications': False,
2016-04-04 09:20:32 +00:00
'language': 'English',
2016-02-18 16:15:38 +00:00
'save_history': False,
'allow_inline': True,
'allow_auto_accept': False,
2016-03-18 16:33:54 +00:00
'auto_accept_path': None,
2016-02-22 21:18:58 +00:00
'show_online_friends': False,
2016-02-18 16:15:38 +00:00
'auto_accept_from_friends': [],
'friends_aliases': [],
'typing_notifications': True,
2016-02-25 11:22:15 +00:00
'calls_sound': True
2016-02-17 19:04:39 +00:00
}
2016-02-16 18:11:56 +00:00
2016-04-04 09:20:32 +00:00
@staticmethod
def supported_languages():
return [
('English', 'en_EN'),
('Russian', 'ru_RU')
]
2016-02-16 19:57:45 +00:00
def save(self):
2016-02-17 21:16:44 +00:00
text = json.dumps(self)
2016-02-16 19:57:45 +00:00
with open(self.path, 'w') as fl:
fl.write(text)
2016-04-03 20:51:46 +00:00
def close(self):
path = Settings.get_default_path() + 'toxygen.json'
if os.path.isfile(path):
with open(path) as fl:
data = fl.read()
app_settings = json.loads(data)
app_settings['active_profile'].remove(ProfileHelper.get_path() + self.name + '.tox')
data = json.dumps(app_settings)
with open(path, 'w') as fl:
fl.write(data)
def set_active_profile(self):
path = Settings.get_default_path() + 'toxygen.json'
if os.path.isfile(path):
with open(path) as fl:
data = fl.read()
app_settings = json.loads(data)
else:
app_settings = {}
if 'active_profile' not in app_settings:
app_settings['active_profile'] = []
app_settings['active_profile'].append(ProfileHelper.get_path() + str(self.name) + '.tox')
data = json.dumps(app_settings)
with open(path, 'w') as fl:
fl.write(data)
2016-03-13 12:06:06 +00:00
def export(self, path):
text = json.dumps(self)
with open(path + str(self.name) + '.json', 'w') as fl:
fl.write(text)
2016-02-16 18:11:56 +00:00
@staticmethod
def get_default_path():
2016-02-16 20:03:37 +00:00
if system() == 'Linux':
2016-02-16 19:57:45 +00:00
return os.getenv('HOME') + '/.config/tox/'
2016-02-16 20:03:37 +00:00
elif system() == 'Windows':
2016-02-16 19:57:45 +00:00
return os.getenv('APPDATA') + '/Tox/'
2016-04-03 20:51:46 +00:00
class ProfileHelper(object):
"""
Class with static methods for search, load and save profiles
"""
@staticmethod
def find_profiles():
path = Settings.get_default_path()
result = []
# check default path
if not os.path.exists(path):
os.makedirs(path)
for fl in os.listdir(path):
if fl.endswith('.tox'):
name = fl[:-4]
result.append((path, name))
path = curr_directory()
# check current directory
for fl in os.listdir(path):
if fl.endswith('.tox'):
name = fl[:-4]
result.append((path + '/', name))
return result
@staticmethod
def is_active_profile(path, name):
path = path + name + '.tox'
settings = Settings.get_default_path() + 'toxygen.json'
if os.path.isfile(settings):
with open(settings) as fl:
data = fl.read()
data = json.loads(data)
if 'active_profile' in data:
return path in data['active_profile']
else:
return False
@staticmethod
def open_profile(path, name):
ProfileHelper._path = path + name + '.tox'
ProfileHelper._directory = path
with open(ProfileHelper._path, 'rb') as fl:
data = fl.read()
if data:
print 'Data loaded from: {}'.format(ProfileHelper._path)
return data
else:
raise IOError('Save file not found. Path: {}'.format(ProfileHelper._path))
@staticmethod
def save_profile(data, name=None):
if name is not None:
ProfileHelper._path = Settings.get_default_path() + name + '.tox'
ProfileHelper._directory = Settings.get_default_path()
with open(ProfileHelper._path, 'wb') as fl:
fl.write(data)
print 'Data saved to: {}'.format(ProfileHelper._path)
@staticmethod
def export_profile(new_path):
new_path += os.path.basename(ProfileHelper._path)
with open(ProfileHelper._path, 'rb') as fin:
data = fin.read()
with open(new_path, 'wb') as fout:
fout.write(data)
print 'Data exported to: {}'.format(new_path)
@staticmethod
def get_path():
return ProfileHelper._directory