2022-10-06 13:44:35 +00:00
|
|
|
# -*- mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*-
|
2016-04-21 19:55:31 +00:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import locale
|
|
|
|
from util import Singleton, curr_directory
|
2022-10-06 13:44:35 +00:00
|
|
|
from wrapper.toxcore_enums_and_consts import *
|
2016-04-21 19:55:31 +00:00
|
|
|
|
2022-10-06 13:44:35 +00:00
|
|
|
global LOG
|
|
|
|
import logging
|
|
|
|
LOG = logging.getLogger(__name__)
|
2016-04-21 19:55:31 +00:00
|
|
|
|
|
|
|
class Settings(Singleton, dict):
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-10-06 13:44:35 +00:00
|
|
|
self.path = os.path.join(curr_directory(), 'settings.json')
|
2016-04-21 19:55:31 +00:00
|
|
|
if os.path.isfile(self.path):
|
|
|
|
with open(self.path) as fl:
|
|
|
|
data = fl.read()
|
|
|
|
super(self.__class__, self).__init__(json.loads(data))
|
|
|
|
else:
|
|
|
|
super(self.__class__, self).__init__(Settings.get_default_settings())
|
2022-10-06 13:44:35 +00:00
|
|
|
self['read'] = list(map(lambda x: x[:TOX_PUBLIC_KEY_SIZE * 2], set(self['read'])))
|
|
|
|
self['write'] = list(map(lambda x: x[:TOX_PUBLIC_KEY_SIZE * 2], set(self['write'])))
|
|
|
|
self['delete'] = list(map(lambda x: x[:TOX_PUBLIC_KEY_SIZE * 2], set(self['delete'])))
|
|
|
|
self['master'] = list(map(lambda x: x[:TOX_PUBLIC_KEY_SIZE * 2], set(self['master'])))
|
2016-04-26 17:42:20 +00:00
|
|
|
if self['folder'][-1] == '/' or self['folder'][-1] == '\\':
|
|
|
|
self['folder'] = self['folder'][:-1]
|
|
|
|
self.save()
|
2016-04-21 19:55:31 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_default_settings():
|
|
|
|
return {
|
|
|
|
'read': [],
|
|
|
|
'write': [],
|
|
|
|
'delete': [],
|
|
|
|
'master': [],
|
|
|
|
'folder': curr_directory(),
|
2022-10-06 13:44:35 +00:00
|
|
|
'folder_save': curr_directory(),
|
2016-10-02 17:19:39 +00:00
|
|
|
'auto_rights': 'r',
|
|
|
|
'size': 500
|
2016-04-21 19:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def save(self):
|
2022-10-06 13:44:35 +00:00
|
|
|
LOG.debug('Saving')
|
2016-04-21 19:55:31 +00:00
|
|
|
text = json.dumps(self)
|
|
|
|
with open(self.path, 'w') as fl:
|
|
|
|
fl.write(text)
|
|
|
|
|
|
|
|
|
|
|
|
class ProfileHelper(object):
|
|
|
|
"""
|
|
|
|
Class with static methods for search, load and save profiles
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def open_profile(path):
|
|
|
|
ProfileHelper._path = path
|
|
|
|
with open(ProfileHelper._path, 'rb') as fl:
|
|
|
|
data = fl.read()
|
|
|
|
if data:
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
raise IOError('Save file has zero size!')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def save_profile(data):
|
|
|
|
with open(ProfileHelper._path, 'wb') as fl:
|
|
|
|
fl.write(data)
|
|
|
|
|