profile backup - initial infrastructure

This commit is contained in:
ingvar1995 2018-09-14 19:10:35 +03:00
parent 9f7de204d4
commit 02b2d07b6d
4 changed files with 75 additions and 2 deletions

View File

@ -34,6 +34,7 @@ from groups.groups_service import GroupsService
from ui.create_profile_screen import CreateProfileScreen
from common.provider import Provider
from contacts.group_peer_factory import GroupPeerFactory
from user_data.backup_service import BackupService
import styles.style # TODO: dynamic loading
@ -45,7 +46,7 @@ class App:
self._tox = self._ms = self._init = self._main_loop = self._av_loop = None
self._uri = self._toxes = self._tray = self._file_transfer_handler = self._contacts_provider = None
self._friend_factory = self._calls_manager = self._contacts_manager = self._smiley_loader = None
self._group_peer_factory = self._tox_dns = None
self._group_peer_factory = self._tox_dns = self._backup_service = None
self._group_factory = self._groups_service = self._profile = None
if uri is not None and uri.startswith('tox:'):
self._uri = uri[4:]
@ -341,6 +342,7 @@ class App:
self._init_callbacks()
def _create_dependencies(self):
self._backup_service = BackupService(self._settings, self._profile_manager)
self._smiley_loader = SmileyLoader(self._settings)
self._tox_dns = ToxDns(self._settings)
self._ms = MainWindow(self._settings, self._tray)

View File

@ -0,0 +1,40 @@
import os.path
from utils.util import get_profile_name_from_path, join_path
class BackupService:
def __init__(self, settings, profile_manager):
self._settings = settings
self._profile_name = get_profile_name_from_path(profile_manager.get_path())
settings.settings_saved_event.add_callback(self._settings_saved)
profile_manager.profile_saved_event.add_callback(self._profile_saved)
def _settings_saved(self, data):
if not self._check_if_should_save_backup():
return
file_path = join_path(self._get_backup_directory(), self._profile_name + '.json')
with open(file_path, 'wt') as fl:
fl.write(data)
def _profile_saved(self, data):
if not self._check_if_should_save_backup():
return
file_path = join_path(self._get_backup_directory(), self._profile_name + '.tox')
with open(file_path, 'wb') as fl:
fl.write(data)
def _check_if_should_save_backup(self):
backup_directory = self._get_backup_directory()
if backup_directory is None:
return False
return os.path.exists(backup_directory) and os.path.isdir(backup_directory)
def _get_backup_directory(self):
return self._settings['backup_directory']

View File

@ -1,6 +1,7 @@
import utils.util as util
import os
from user_data.settings import Settings
from common.event import Event
class ProfileManager:
@ -11,11 +12,25 @@ class ProfileManager:
self._toxes = toxes
self._path = path
self._directory = os.path.dirname(path)
self._profile_saved_event = Event()
# create /avatars if not exists:
avatars_directory = util.join_path(self._directory, 'avatars')
if not os.path.exists(avatars_directory):
os.makedirs(avatars_directory)
# -----------------------------------------------------------------------------------------------------------------
# Properties
# -----------------------------------------------------------------------------------------------------------------
def get_profile_saved_event(self):
return self._profile_saved_event
profile_saved_event = property(get_profile_saved_event)
# -----------------------------------------------------------------------------------------------------------------
# Public methods
# -----------------------------------------------------------------------------------------------------------------
def open_profile(self):
with open(self._path, 'rb') as fl:
data = fl.read()
@ -37,6 +52,8 @@ class ProfileManager:
fl.write(data)
print('Profile saved successfully')
self._profile_saved_event(data)
def export_profile(self, settings, new_path, use_new_path):
path = new_path + os.path.basename(self._path)
with open(self._path, 'rb') as fin:

View File

@ -1,6 +1,7 @@
import json
from utils.util import *
import pyaudio
from common.event import Event
class Settings(dict):
@ -12,6 +13,7 @@ class Settings(dict):
self._path = path
self._profile_path = path.replace('.json', '.tox')
self._toxes = toxes
self._settings_saved_event = Event()
if os.path.isfile(path):
with open(path, 'rb') as fl:
data = fl.read()
@ -43,6 +45,15 @@ class Settings(dict):
'enabled': input_devices and output_devices}
self.video = {'device': -1, 'width': 640, 'height': 480, 'x': 0, 'y': 0}
# -----------------------------------------------------------------------------------------------------------------
# Properties
# -----------------------------------------------------------------------------------------------------------------
def get_settings_saved_event(self):
return self._settings_saved_event
settings_saved_event = property(get_settings_saved_event)
# -----------------------------------------------------------------------------------------------------------------
# Public methods
# -----------------------------------------------------------------------------------------------------------------
@ -56,6 +67,8 @@ class Settings(dict):
with open(self._path, 'wb') as fl:
fl.write(text)
self._settings_saved_event(text)
def close(self):
path = self._profile_path + '.lock'
if os.path.isfile(path):
@ -186,7 +199,8 @@ class Settings(dict):
'group_notifications': True,
'download_nodes_list': False,
'notify_all_gc': False,
'lan_discovery': True
'lan_discovery': True,
'backup_directory': None
}
@staticmethod