commit 2a27816329f7a925396bc3543249509be3387db4
parent d8e25c8bc840fdb89624244d979395d89de8f69d
Author: Eric Davis <edavis@insanum.com>
Date: Wed, 9 Jul 2014 13:24:41 -0700
verify all notes saved to disk before quit
Diffstat:
2 files changed, 40 insertions(+), 20 deletions(-)
diff --git a/notes_db.py b/notes_db.py
@@ -2,7 +2,7 @@
# copyright 2012 by Charl P. Botha <cpbotha@vxlabs.com>
# new BSD license
-import os, time, re, glob, json, copy
+import os, time, re, glob, json, copy, threading
import utils
import simplenote
simplenote.NOTE_FETCH_LENGTH=100
@@ -20,7 +20,9 @@ class NotesDB():
def __init__(self, config, log):
self.config = config
self.log = log
+
self.last_sync = 0 # set to zero to trigger a full sync
+ self.sync_lock = threading.Lock()
# create db dir if it does not exist
if not os.path.exists(self.config.get_config('db_path')):
@@ -294,22 +296,6 @@ def get_note_tags(self, key):
def get_note_content(self, key):
return self.notes[key].get('content')
- def get_note_status(self, key):
- n = self.notes[key]
- o = utils.KeyValueObject(saved=False, synced=False, modified=False)
- modifydate = float(n['modifydate'])
- savedate = float(n['savedate'])
-
- if savedate > modifydate:
- o.saved = True
- else:
- o.modified = True
-
- if float(n['syncdate']) > modifydate:
- o.synced = True
-
- return o
-
def flag_what_changed(self, note, what_changed):
if 'what_changed' not in note:
note['what_changed'] = []
@@ -574,12 +560,42 @@ def sync_notes(self, server_sync=True, full_sync=True):
return sync_errors
+ def get_note_status(self, key):
+ n = self.notes[key]
+ o = utils.KeyValueObject(saved=False, synced=False, modified=False)
+ modifydate = float(n['modifydate'])
+ savedate = float(n['savedate'])
+ syncdate = float(n['syncdate'])
+
+ if savedate > modifydate:
+ o.saved = True
+ else:
+ o.modified = True
+
+ if syncdate > modifydate:
+ o.synced = True
+
+ return o
+
+ def verify_all_saved(self):
+ all_saved = True
+ self.sync_lock.acquire()
+ for k in self.notes.keys():
+ o = self.get_note_status(k)
+ if not o.saved:
+ all_saved = False
+ break
+ self.sync_lock.release()
+ return all_saved
+
# sync worker thread...
def sync_worker(self, do_sync):
time.sleep(1) # give some time to wait for GUI initialization
self.log('Sync worker: started')
while True:
+ self.sync_lock.acquire()
self.sync_notes(server_sync=do_sync,
full_sync=True if not self.last_sync else False)
+ self.sync_lock.release()
time.sleep(5)
diff --git a/sncli.py b/sncli.py
@@ -628,9 +628,13 @@ def gui_reset(self):
self.sncli_loop.draw_screen()
def gui_stop(self):
- # clear the screen and exit the urwid run loop
- self.gui_clear()
- raise urwid.ExitMainLoop()
+ # don't exit if there are any notes not yet saved to the disk
+ if self.ndb.verify_all_saved():
+ # clear the screen and exit the urwid run loop
+ self.gui_clear()
+ raise urwid.ExitMainLoop()
+ else:
+ self.log(u'WARNING: Not all notes saved to disk (wait for sync worker)')
def gui(self, do_sync):