Make it possible to use hooks before and after every sync operation

Excluding media sync.
This commit is contained in:
flan 2017-11-05 23:34:23 +01:00
parent 65bef264be
commit 9ffd380e64

View File

@ -340,10 +340,9 @@ class SyncApp:
self.base_url = config.get("sync_app", "base_url")
self.base_media_url = config.get("sync_app", "base_media_url")
self.setup_new_collection = None
self.hook_pre_sync = None
self.hook_post_sync = None
self.hook_download = None
self.hook_upload = None
self.prehooks = {}
self.posthooks = {}
if config.has_option("sync_app", "session_db_path"):
self.session_manager = SqliteSessionManager(config.get("sync_app", "session_db_path"))
@ -364,6 +363,39 @@ class SyncApp:
if not self.base_media_url.endswith('/'):
self.base_media_url += '/'
# backwards compat
@property
def hook_pre_sync(self):
return self.prehooks.get("start")
@hook_pre_sync.setter
def hook_pre_sync(self, value):
self.prehooks['start'] = value
@property
def hook_post_sync(self):
return self.posthooks.get("finish")
@hook_post_sync.setter
def hook_post_sync(self, value):
self.posthooks['finish'] = value
@property
def hook_upload(self):
return self.prehooks.get("upload")
@hook_upload.setter
def hook_upload(self, value):
self.prehooks['upload'] = value
@property
def hook_download(self):
return self.posthooks.get("download")
@hook_download.setter
def hook_download(self, value):
self.posthooks['download'] = value
def generateHostKey(self, username):
"""Generates a new host key to be used by the given username to identify their session.
This values is random."""
@ -429,17 +461,9 @@ class SyncApp:
col.reopen()
col.load()
# If everything went fine, run hook_upload if one is defined.
if self.hook_upload is not None:
self.hook_upload(col, session)
return "OK"
def operation_download(self, col, session):
# run hook_download if one is defined
if self.hook_download is not None:
self.hook_download(col, session)
col.close()
try:
data = open(session.get_collection_path(), 'rb').read()
@ -515,10 +539,8 @@ class SyncApp:
thread = session.get_thread()
# run hook_pre_sync if one is defined
if url == 'start':
if self.hook_pre_sync is not None:
thread.execute(self.hook_pre_sync, [session])
if url in self.prehooks:
thread.execute(self.prehooks[url], [session])
result = self._execute_handler_method_in_thread(url, data, session)
@ -526,25 +548,27 @@ class SyncApp:
if type(result) not in (str, bytes):
result = json.dumps(result)
if url == 'finish':
# TODO: Apparently 'finish' isn't when we're done because 'mediaList' comes
# after it... When can we possibly delete the session?
#self.session_manager.delete(hkey)
# run hook_post_sync if one is defined
if self.hook_post_sync is not None:
thread.execute(self.hook_post_sync, [session])
if url in self.posthooks:
thread.execute(self.posthooks[url], [session])
return result
elif url == 'upload':
thread = session.get_thread()
if url in self.prehooks:
thread.execute(self.prehooks[url], [session])
result = thread.execute(self.operation_upload, [data['data'], session])
if url in self.posthooks:
thread.execute(self.posthooks[url], [session])
return result
elif url == 'download':
thread = session.get_thread()
if url in self.prehooks:
thread.execute(self.prehooks[url], [session])
result = thread.execute(self.operation_download, [session])
if url in self.posthooks:
thread.execute(self.posthooks[url], [session])
return result
# This was one of our operations but it didn't get handled... Oops!