diff --git a/cli.py b/cli.py index 1b44601..a03fa8a 100755 --- a/cli.py +++ b/cli.py @@ -41,13 +41,32 @@ class CLI(cmd.Cmd): print FolderManager.error_str(ret) else: print "Deleted folder '%s'" % args[0] + elif action == 'scan': + s = Scanner(db.session) + if args: + folders = map(lambda n: db.Folder.query.filter(db.Folder.name == n and db.Folder.root == True).first() or n, args) + if any(map(lambda f: isinstance(f, basestring), folders)): + print "No such folder(s): " + ' '.join(f for f in folders if isinstance(f, basestring)) + for folder in filter(lambda f: isinstance(f, db.Folder), folders): + FolderManager.scan(folder.id, s) + else: + for folder in db.Folder.query.filter(db.Folder.root == True): + FolderManager.scan(folder.id, s) + + added, deleted = s.stats() + db.session.commit() + + print "Scanning done" + print 'Added: %i artists, %i albums, %i tracks' % (added[0], added[1], added[2]) + print 'Deleted: %i artists, %i albums, %i tracks' % (deleted[0], deleted[1], deleted[2]) else: print "Unknown action '%s'" % action def help_folder(self): print "folder\nfolder list\n\tDisplays the list of folders" - print "folder add \n\tAdds a music folder pointing to folder " - print "folder delete \n\tDeletes folder " + print "folder add \n\tAdds a music folder 'name' pointing to folder 'path'" + print "folder delete \n\tDeletes folder 'name'" + print "folder scan [name [name [...]]]\n\tRuns a scan on folder 'name'. If 'name' is ommited, all folders\n\tare scanned" if __name__ == "__main__": if not config.check(): @@ -57,6 +76,7 @@ if __name__ == "__main__": db.init_db() from folder_manager import FolderManager + from scanner import Scanner if len(sys.argv) > 1: CLI().onecmd(' '.join(sys.argv[1:])) diff --git a/folder.py b/folder.py index 8bd8fd4..b11782b 100755 --- a/folder.py +++ b/folder.py @@ -68,26 +68,14 @@ def del_folder(id): def scan_folder(id = None): s = Scanner(session) if id is None: - for folder in Folder.query.filter(Folder.root == True).all(): - s.scan(folder) - s.prune(folder) - s.check_cover_art(folder) + for folder in Folder.query.filter(Folder.root == True): + FolderManager.scan(folder.id, s) else: - try: - idid = uuid.UUID(id) - except ValueError: - flash('Invalid folder id') + status = FolderManager.scan(id, s) + if status != FolderManager.SUCCESS: + flash(FolderManager.error_str(status)) return redirect(url_for('folder_index')) - folder = Folder.query.get(idid) - if folder is None or not folder.root: - flash('No such folder') - return redirect(url_for('folder_index')) - - s.scan(folder) - s.prune(folder) - s.check_cover_art(folder) - added, deleted = s.stats() session.commit() diff --git a/folder_manager.py b/folder_manager.py index d19e3c4..e95a33f 100755 --- a/folder_manager.py +++ b/folder_manager.py @@ -1,14 +1,33 @@ # coding: utf-8 -import os.path +import os.path, uuid from db import Folder, Artist, session class FolderManager: SUCCESS = 0 - NAME_EXISTS = 1 - INVALID_PATH = 2 - PATH_EXISTS = 3 - NO_SUCH_FOLDER = 4 + INVALID_ID = 1 + NAME_EXISTS = 2 + INVALID_PATH = 3 + PATH_EXISTS = 4 + NO_SUCH_FOLDER = 5 + + @staticmethod + def get(uid): + if isinstance(uid, basestring): + try: + uid = uuid.UUID(uid) + except: + return FolderManager.INVALID_ID, None + elif type(uid) is uuid.UUID: + pass + else: + return FolderManager.INVALID_ID, None + + folder = Folder.query.get(uid) + if not folder: + return FolderManager.NO_SUCH_FOLDER, None + + return FolderManager.SUCCESS, folder @staticmethod def add(name, path): @@ -30,8 +49,11 @@ class FolderManager: @staticmethod def delete(uid): - folder = Folder.query.get(uid) - if folder is None or not folder.root: + status, folder = FolderManager.get(uid) + if status != FolderManager.SUCCESS: + return status + + if not folder.root: return FolderManager.NO_SUCH_FOLDER # delete associated tracks and prune empty albums/artists @@ -63,10 +85,23 @@ class FolderManager: return FolderManager.NO_SUCH_FOLDER return FolderManager.delete(folder.id) + @staticmethod + def scan(uid, scanner): + status, folder = FolderManager.get(uid) + if status != FolderManager.SUCCESS: + return status + + scanner.scan(folder) + scanner.prune(folder) + scanner.check_cover_art(folder) + return FolderManager.SUCCESS + @staticmethod def error_str(err): if err == FolderManager.SUCCESS: return 'No error' + elif err == FolderManager.INVALID_ID: + return 'Invalid folder id' elif err == FolderManager.NAME_EXISTS: return 'There is already a folder with that name. Please pick another one.' elif err == FolderManager.INVALID_PATH: