Bring code up to PEP8 standard
This commit is contained in:
+40
-41
@@ -1,69 +1,68 @@
|
||||
from datetime import datetime
|
||||
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 stat import S_IFDIR, S_IFREG
|
||||
from threading import Event, Thread
|
||||
from time import sleep
|
||||
|
||||
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
|
||||
from itemmanager import ItemManager
|
||||
from fuse import FuseOSError, LoggingMixIn, Operations
|
||||
import iso8601
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from itemmanager import ItemManager
|
||||
|
||||
class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
def __init__(self, sn_api, sync_sec, path='.'):
|
||||
self.item_manager = ItemManager(sn_api)
|
||||
self.notes = self.item_manager.getNotes()
|
||||
self.notes = self.item_manager.get_notes()
|
||||
|
||||
self.uid = os.getuid()
|
||||
self.gid = os.getgid()
|
||||
|
||||
now = datetime.now().timestamp()
|
||||
self.dir_stat = dict(st_mode=(S_IFDIR | 0o755), st_ctime=now,
|
||||
st_mtime=now, st_atime=now, st_nlink=2,
|
||||
st_uid=self.uid, st_gid=self.gid)
|
||||
st_mtime=now, st_atime=now, st_nlink=2,
|
||||
st_uid=self.uid, st_gid=self.gid)
|
||||
|
||||
self.note_stat = dict(st_mode=(S_IFREG | 0o644), st_ctime=now,
|
||||
st_mtime=now, st_atime=now, st_nlink=1,
|
||||
st_uid=self.uid, st_gid=self.gid)
|
||||
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 = Thread(target=self._sync_thread)
|
||||
|
||||
def init(self, path):
|
||||
self.sync_thread.start()
|
||||
|
||||
def destroy(self, path):
|
||||
self._syncNow()
|
||||
self._sync_now()
|
||||
logging.info('Stopping sync thread.')
|
||||
self.stop_sync.set()
|
||||
self.sync_thread.join()
|
||||
return 0
|
||||
|
||||
def _syncThread(self):
|
||||
def _sync_thread(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()
|
||||
sleep(0.1) # fixes race condition of quick create() then write()
|
||||
try:
|
||||
self.item_manager.syncItems()
|
||||
self.item_manager.sync_items()
|
||||
except ConnectionError:
|
||||
logging.error('Unable to connect to sync server. Retrying...')
|
||||
|
||||
def _syncNow(self):
|
||||
def _sync_now(self):
|
||||
self.run_sync.set()
|
||||
|
||||
def _pathToNote(self, path):
|
||||
def _path_to_note(self, path):
|
||||
pp = PurePath(path)
|
||||
note_name = pp.parts[1]
|
||||
self.notes = self.item_manager.getNotes()
|
||||
self.notes = self.item_manager.get_notes()
|
||||
note = self.notes[note_name]
|
||||
return note, note['uuid']
|
||||
|
||||
@@ -72,7 +71,7 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
return self.dir_stat
|
||||
|
||||
try:
|
||||
note, uuid = self._pathToNote(path)
|
||||
note, uuid = self._path_to_note(path)
|
||||
st = self.note_stat
|
||||
st['st_size'] = len(note['text'])
|
||||
st['st_ctime'] = iso8601.parse_date(note['created']).timestamp()
|
||||
@@ -89,27 +88,27 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
return dirents
|
||||
|
||||
def read(self, path, size, offset, fh):
|
||||
note, uuid = self._pathToNote(path)
|
||||
note, uuid = self._path_to_note(path)
|
||||
return note['text'][offset : offset + size]
|
||||
|
||||
def truncate(self, path, length, fh=None):
|
||||
note, uuid = self._pathToNote(path)
|
||||
note, uuid = self._path_to_note(path)
|
||||
text = note['text'][:length]
|
||||
self.item_manager.writeNote(uuid, text)
|
||||
self._syncNow()
|
||||
self.item_manager.write_note(uuid, text)
|
||||
self._sync_now()
|
||||
return 0
|
||||
|
||||
def write(self, path, data, offset, fh):
|
||||
note, uuid = self._pathToNote(path)
|
||||
note, uuid = self._path_to_note(path)
|
||||
text = note['text'][:offset] + data
|
||||
|
||||
try:
|
||||
self.item_manager.writeNote(uuid, text)
|
||||
self.item_manager.write_note(uuid, text)
|
||||
except UnicodeError:
|
||||
logging.error('Unable to parse non-unicode data.')
|
||||
raise FuseOSError(errno.EIO)
|
||||
|
||||
self._syncNow()
|
||||
self._sync_now()
|
||||
return len(data)
|
||||
|
||||
def create(self, path, mode):
|
||||
@@ -123,14 +122,14 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
|
||||
now = datetime.utcnow().isoformat()[:-3] + 'Z' # hack
|
||||
|
||||
self.item_manager.createNote(note_name, now)
|
||||
self._syncNow()
|
||||
self.item_manager.create_note(note_name, now)
|
||||
self._sync_now()
|
||||
return 0
|
||||
|
||||
def unlink(self, path):
|
||||
note, uuid = self._pathToNote(path)
|
||||
self.item_manager.deleteNote(uuid)
|
||||
self._syncNow()
|
||||
note, uuid = self._path_to_note(path)
|
||||
self.item_manager.delete_note(uuid)
|
||||
self._sync_now()
|
||||
return 0
|
||||
|
||||
def mkdir(self, path, mode):
|
||||
@@ -138,17 +137,17 @@ class StandardNotesFUSE(LoggingMixIn, Operations):
|
||||
raise FuseOSError(errno.EPERM)
|
||||
|
||||
def utimens(self, path, times=None):
|
||||
note, uuid = self._pathToNote(path)
|
||||
self.item_manager.touchNote(uuid)
|
||||
self._syncNow()
|
||||
note, uuid = self._path_to_note(path)
|
||||
self.item_manager.touch_note(uuid)
|
||||
self._sync_now()
|
||||
return 0
|
||||
|
||||
def rename(self, old, new):
|
||||
note, uuid = self._pathToNote(old)
|
||||
note, uuid = self._path_to_note(old)
|
||||
new_path_parts = new.split('/')
|
||||
new_note_name = new_path_parts[1]
|
||||
self.item_manager.renameNote(uuid, new_note_name)
|
||||
self._syncNow()
|
||||
self.item_manager.rename_note(uuid, new_note_name)
|
||||
self._sync_now()
|
||||
return 0
|
||||
|
||||
def chmod(self, path, mode):
|
||||
|
||||
Reference in New Issue
Block a user