1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-12 21:22:17 +00:00

Switched to Storm

This commit is contained in:
spl0k 2014-08-03 19:30:29 +02:00
parent 18ecf0fc08
commit 77becdb655

View File

@ -26,7 +26,7 @@ from logging.handlers import TimedRotatingFileHandler
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
from supysonic import config from supysonic import config, db
from supysonic.scanner import Scanner from supysonic.scanner import Scanner
class SupysonicWatcherEventHandler(PatternMatchingEventHandler): class SupysonicWatcherEventHandler(PatternMatchingEventHandler):
@ -44,14 +44,15 @@ class SupysonicWatcherEventHandler(PatternMatchingEventHandler):
def on_deleted(self, event): def on_deleted(self, event):
self.__logger.debug("File deleted: '%s'", event.src_path) self.__logger.debug("File deleted: '%s'", event.src_path)
track = db.Track.query.filter(db.Track.path == event.src_path).first() store = db.get_store(config.get('base', 'database_uri'))
track = store.find(db.Track, db.Track.path == event.src_path).one()
if track: if track:
folder = track.root_folder folder = track.root_folder
Scanner(db.session).prune(folder) Scanner(store).prune(folder)
db.session.commit() store.commit()
db.session.remove()
else: else:
self.__logger.debug("Deleted file %s not in the database", event.src_path) self.__logger.debug("Deleted file %s not in the database", event.src_path)
store.close()
def on_modified(self, event): def on_modified(self, event):
self.__logger.debug("File modified: '%s'", event.src_path) self.__logger.debug("File modified: '%s'", event.src_path)
@ -81,7 +82,8 @@ class ScannerProcessingQueue(Thread):
continue continue
self.__logger.debug("Instantiating scanner") self.__logger.debug("Instantiating scanner")
scanner = Scanner(db.session) store = db.get_store(config.get('base', 'database_uri'))
scanner = Scanner(store)
path = self.__next_item() path = self.__next_item()
while path: while path:
@ -89,8 +91,8 @@ class ScannerProcessingQueue(Thread):
scanner.scan_file(path) scanner.scan_file(path)
path = self.__next_item() path = self.__next_item()
db.session.commit() store.commit()
db.session.remove() store.close()
self.__logger.debug("Freeing scanner") self.__logger.debug("Freeing scanner")
del scanner del scanner
@ -148,21 +150,23 @@ if __name__ == "__main__":
} }
logger.setLevel(mapping.get(config.get('daemon', 'log_level').upper(), logging.NOTSET)) logger.setLevel(mapping.get(config.get('daemon', 'log_level').upper(), logging.NOTSET))
from supysonic import db store = db.get_store(config.get('base', 'database_uri'))
db.init_db() folders = store.find(db.Folder, db.Folder.root == True)
if not db.Folder.query.filter(db.Folder.root == True).count(): if not folders.count():
logger.info("No folder set. Exiting.") logger.info("No folder set. Exiting.")
store.close()
sys.exit(0) sys.exit(0)
queue = ScannerProcessingQueue(logger) queue = ScannerProcessingQueue(logger)
handler = SupysonicWatcherEventHandler(queue, logger) handler = SupysonicWatcherEventHandler(queue, logger)
observer = Observer() observer = Observer()
for folder in db.Folder.query.filter(db.Folder.root == True): for folder in folders:
logger.info("Starting watcher for %s", folder.path) logger.info("Starting watcher for %s", folder.path)
observer.schedule(handler, folder.path, recursive = True) observer.schedule(handler, folder.path, recursive = True)
store.close()
queue.start() queue.start()
observer.start() observer.start()
try: try: