Sync notes automatically and on changes
This commit is contained in:
+35
-8
@@ -1,20 +1,20 @@
|
||||
from __future__ import print_function, absolute_import, division
|
||||
|
||||
import errno
|
||||
import iso8601
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
|
||||
from stat import S_IFDIR, S_IFREG
|
||||
from sys import argv, exit
|
||||
from datetime import datetime
|
||||
from pathlib import PurePath
|
||||
from threading import Thread, Event
|
||||
|
||||
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
|
||||
from itemmanager import ItemManager
|
||||
|
||||
class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
def __init__(self, sn_api, path='.'):
|
||||
def __init__(self, sn_api, sync_sec, path='.'):
|
||||
self.item_manager = ItemManager(sn_api)
|
||||
self.notes = self.item_manager.getNotes()
|
||||
|
||||
@@ -30,15 +30,38 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
st_mtime=now, st_atime=now, st_nlink=1,
|
||||
st_uid=self.uid, st_gid=self.gid)
|
||||
|
||||
self.sync_sec = sync_sec
|
||||
self.run_sync = Event()
|
||||
self.stop_sync = Event()
|
||||
self.sync_thread = Thread(target=self._syncThread)
|
||||
self.sync_thread.start()
|
||||
|
||||
def destroy(self, path):
|
||||
self._syncNow()
|
||||
logging.info('Stopping sync thread.')
|
||||
self.stop_sync.set()
|
||||
self.sync_thread.join()
|
||||
return 0
|
||||
|
||||
def _syncThread(self):
|
||||
while not self.stop_sync.is_set():
|
||||
self.run_sync.clear()
|
||||
manually_synced = self.run_sync.wait(timeout=self.sync_sec)
|
||||
if not manually_synced: logging.info('Auto-syncing items...')
|
||||
time.sleep(0.1) # fixes race condition of quick create() then write()
|
||||
self.item_manager.syncItems()
|
||||
|
||||
def _syncNow(self):
|
||||
self.run_sync.set()
|
||||
|
||||
def _pathToNote(self, path):
|
||||
pp = PurePath(path)
|
||||
note_name = pp.parts[1]
|
||||
self.notes = self.item_manager.getNotes()
|
||||
note = self.notes[note_name]
|
||||
return note, note['uuid']
|
||||
|
||||
def getattr(self, path, fh=None):
|
||||
self.notes = self.item_manager.getNotes()
|
||||
|
||||
if path == '/':
|
||||
return self.dir_stat
|
||||
|
||||
@@ -67,6 +90,8 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
note, uuid = self._pathToNote(path)
|
||||
text = note['text'][:length]
|
||||
self.item_manager.writeNote(uuid, text)
|
||||
self._syncNow()
|
||||
return 0
|
||||
|
||||
def write(self, path, data, offset, fh):
|
||||
note, uuid = self._pathToNote(path)
|
||||
@@ -78,6 +103,7 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
raise FuseOSError(errno.EIO)
|
||||
|
||||
self.item_manager.writeNote(uuid, text)
|
||||
self._syncNow()
|
||||
return len(data)
|
||||
|
||||
def create(self, path, mode):
|
||||
@@ -92,11 +118,13 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
now = datetime.utcnow().isoformat()[:-3] + 'Z' # hack
|
||||
|
||||
self.item_manager.createNote(note_name, now)
|
||||
self._syncNow()
|
||||
return 0
|
||||
|
||||
def unlink(self, path):
|
||||
note, uuid = self._pathToNote(path)
|
||||
self.item_manager.deleteNote(uuid)
|
||||
self._syncNow()
|
||||
return 0
|
||||
|
||||
def mkdir(self, path, mode):
|
||||
@@ -106,6 +134,7 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
def utimens(self, path, times=None):
|
||||
note, uuid = self._pathToNote(path)
|
||||
self.item_manager.touchNote(uuid)
|
||||
self._syncNow()
|
||||
return 0
|
||||
|
||||
def rename(self, old, new):
|
||||
@@ -113,6 +142,7 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
new_path_parts = new.split('/')
|
||||
new_note_name = new_path_parts[1]
|
||||
self.item_manager.renameNote(uuid, new_note_name)
|
||||
self._syncNow()
|
||||
return 0
|
||||
|
||||
def chmod(self, path, mode):
|
||||
@@ -123,9 +153,6 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
logging.error('chown is disabled.')
|
||||
raise FuseOSError(errno.EPERM)
|
||||
|
||||
def destroy(self, path):
|
||||
return 0
|
||||
|
||||
def readlink(self, path):
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user