From f61a3a7937b3409b36b737757a347a864c7a1647 Mon Sep 17 00:00:00 2001 From: spl0k Date: Sun, 31 Aug 2014 16:32:17 +0200 Subject: [PATCH] Added basic file move support --- bin/supysonic-watcher | 23 +++++++++++++++++------ supysonic/scanner.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/bin/supysonic-watcher b/bin/supysonic-watcher index 179e2cf..2904212 100755 --- a/bin/supysonic-watcher +++ b/bin/supysonic-watcher @@ -62,18 +62,19 @@ class SupysonicWatcherEventHandler(PatternMatchingEventHandler): def on_moved(self, event): self.__logger.debug("File moved: '%s' -> '%s'", event.src_path, event.dest_path) - self.__queue.put(event.src_path, OP_MOVE) + self.__queue.put(event.src_path, OP_MOVE, event.dest_path) class Event(object): - def __init__(self, path, operation): + def __init__(self, path, operation, dst_path = None): if operation & (OP_SCAN | OP_REMOVE) == (OP_SCAN | OP_REMOVE): raise Exception("Flags SCAN and REMOVE both set") self.__path = path self.__time = time.time() self.__op = operation + self.__dst = dst_path - def set(self, operation): + def set(self, operation, dst_path = None): if operation & (OP_SCAN | OP_REMOVE) == (OP_SCAN | OP_REMOVE): raise Exception("Flags SCAN and REMOVE both set") @@ -84,6 +85,9 @@ class Event(object): self.__op &= ~OP_SCAN self.__op |= operation + if dst_path: + self.__dst = dst_path + @property def path(self): return self.__path @@ -96,6 +100,10 @@ class Event(object): def operation(self): return self.__op + @property + def dst_path(self): + return self.__dst + class ScannerProcessingQueue(Thread): def __init__(self, logger): super(ScannerProcessingQueue, self).__init__() @@ -128,6 +136,9 @@ class ScannerProcessingQueue(Thread): item = self.__next_item() while item: + if item.operation & OP_MOVE: + self.__logger.info("Moving: '%s' -> '%s'", item.path, item.dst_path) + scanner.move_file(item.path, item.dst_path) if item.operation & OP_SCAN: self.__logger.info("Scanning: '%s'", item.path) scanner.scan_file(item.path) @@ -147,15 +158,15 @@ class ScannerProcessingQueue(Thread): with self.__cond: self.__cond.notify() - def put(self, path, operation): + def put(self, path, operation, *args): if not self.__running: raise RuntimeError("Trying to put an item in a stopped queue") with self.__cond: if path in self.__queue: - self.__queue[path].set(operation) + self.__queue[path].set(operation, *args) else: - self.__queue[path] = Event(path, operation) + self.__queue[path] = Event(path, operation, *args) if self.__timer: self.__timer.cancel() diff --git a/supysonic/scanner.py b/supysonic/scanner.py index 006b300..72e3a28 100644 --- a/supysonic/scanner.py +++ b/supysonic/scanner.py @@ -159,6 +159,24 @@ class Scanner: self.__store.remove(tr) self.__deleted_tracks += 1 + def move_file(self, src_path, dst_path): + tr = self.__store.find(Track, Track.path == src_path).one() + if not tr: + return + + self.__folders_to_check.add(tr.folder) + tr_dst = self.__store.find(Track, Track.path == dst_path).one() + if tr_dst: + tr.root_folder = tr_dst.root_folder + tr.folder = tr_dst.folder + self.remove_file(dst_path) + else: + root = self.__find_root_folder(dst_path) + folder = self.__find_folder(dst_path) + tr.root_folder = root + tr.folder = folder + tr.path = dst_path + def __find_album(self, artist, album): ar = self.__find_artist(artist) al = ar.albums.find(name = album).one()