1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Fixed scanner folder lookup

Closes #33, #35
This commit is contained in:
spl0k 2015-08-06 20:51:28 +02:00
parent 1282a5ab12
commit 5fdd28290b

View File

@ -21,13 +21,37 @@
import os, os.path import os, os.path
import time, mimetypes import time, mimetypes
import mutagen import mutagen
from storm.expr import Like, SQL
from storm.expr import ComparableExpr, compile, Like
from storm.exceptions import NotSupportedError
from supysonic import config from supysonic import config
from supysonic.db import Folder, Artist, Album, Track from supysonic.db import Folder, Artist, Album, Track
def get_mime(ext): def get_mime(ext):
return mimetypes.guess_type('dummy.' + ext, False)[0] or config.get('mimetypes', ext) or 'application/octet-stream' return mimetypes.guess_type('dummy.' + ext, False)[0] or config.get('mimetypes', ext) or 'application/octet-stream'
# Hacking in support for a concatenation expression
class Concat(ComparableExpr):
__slots__ = ("left", "right", "db")
def __init__(self, left, right, db):
self.left = left
self.right = right
self.db = db
@compile.when(Concat)
def compile_concat(compile, concat, state):
left = compile(concat.left, state)
right = compile(concat.right, state)
if concat.db in ('sqlite', 'postgres'):
statement = "%s||%s"
elif concat.db == 'mysql':
statement = "CONCAT(%s, %s)"
else:
raise NotSupportedError("Unspported database (%s)" % concat.db)
return statement % (left, right)
class Scanner: class Scanner:
def __init__(self, store): def __init__(self, store):
self.__store = store self.__store = store
@ -207,7 +231,8 @@ class Scanner:
def __find_root_folder(self, path): def __find_root_folder(self, path):
path = os.path.dirname(path) path = os.path.dirname(path)
folders = self.__store.find(Folder, Like(path, SQL("folder.path||'%'")), Folder.root == True) db = self.__store.get_database().__module__[len('storm.databases.'):]
folders = self.__store.find(Folder, Like(path, Concat(Folder.path, u'%', db)), Folder.root == True)
count = folders.count() count = folders.count()
if count > 1: if count > 1:
raise Exception("Found multiple root folders for '{}'.".format(path)) raise Exception("Found multiple root folders for '{}'.".format(path))
@ -224,7 +249,8 @@ class Scanner:
elif count == 1: elif count == 1:
return folders.one() return folders.one()
folder = self.__store.find(Folder, Like(path, SQL("folder.path||'%'"))).order_by(Folder.path).last() db = self.__store.get_database().__module__[len('storm.databases.'):]
folder = self.__store.find(Folder, Like(path, Concat(Folder.path, u'%', db))).order_by(Folder.path).last()
full_path = folder.path full_path = folder.path
path = path[len(folder.path) + 1:] path = path[len(folder.path) + 1:]