mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
Added scanning to the CLI
This commit is contained in:
parent
5d7be6c416
commit
cb5e7bf2f7
24
cli.py
24
cli.py
@ -41,13 +41,32 @@ class CLI(cmd.Cmd):
|
|||||||
print FolderManager.error_str(ret)
|
print FolderManager.error_str(ret)
|
||||||
else:
|
else:
|
||||||
print "Deleted folder '%s'" % args[0]
|
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:
|
else:
|
||||||
print "Unknown action '%s'" % action
|
print "Unknown action '%s'" % action
|
||||||
|
|
||||||
def help_folder(self):
|
def help_folder(self):
|
||||||
print "folder\nfolder list\n\tDisplays the list of folders"
|
print "folder\nfolder list\n\tDisplays the list of folders"
|
||||||
print "folder add <name> <path>\n\tAdds a music folder <name> pointing to folder <path>"
|
print "folder add <name> <path>\n\tAdds a music folder 'name' pointing to folder 'path'"
|
||||||
print "folder delete <name>\n\tDeletes folder <name>"
|
print "folder delete <name>\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 __name__ == "__main__":
|
||||||
if not config.check():
|
if not config.check():
|
||||||
@ -57,6 +76,7 @@ if __name__ == "__main__":
|
|||||||
db.init_db()
|
db.init_db()
|
||||||
|
|
||||||
from folder_manager import FolderManager
|
from folder_manager import FolderManager
|
||||||
|
from scanner import Scanner
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
CLI().onecmd(' '.join(sys.argv[1:]))
|
CLI().onecmd(' '.join(sys.argv[1:]))
|
||||||
|
22
folder.py
22
folder.py
@ -68,26 +68,14 @@ def del_folder(id):
|
|||||||
def scan_folder(id = None):
|
def scan_folder(id = None):
|
||||||
s = Scanner(session)
|
s = Scanner(session)
|
||||||
if id is None:
|
if id is None:
|
||||||
for folder in Folder.query.filter(Folder.root == True).all():
|
for folder in Folder.query.filter(Folder.root == True):
|
||||||
s.scan(folder)
|
FolderManager.scan(folder.id, s)
|
||||||
s.prune(folder)
|
|
||||||
s.check_cover_art(folder)
|
|
||||||
else:
|
else:
|
||||||
try:
|
status = FolderManager.scan(id, s)
|
||||||
idid = uuid.UUID(id)
|
if status != FolderManager.SUCCESS:
|
||||||
except ValueError:
|
flash(FolderManager.error_str(status))
|
||||||
flash('Invalid folder id')
|
|
||||||
return redirect(url_for('folder_index'))
|
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()
|
added, deleted = s.stats()
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@ -1,14 +1,33 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
|
||||||
import os.path
|
import os.path, uuid
|
||||||
from db import Folder, Artist, session
|
from db import Folder, Artist, session
|
||||||
|
|
||||||
class FolderManager:
|
class FolderManager:
|
||||||
SUCCESS = 0
|
SUCCESS = 0
|
||||||
NAME_EXISTS = 1
|
INVALID_ID = 1
|
||||||
INVALID_PATH = 2
|
NAME_EXISTS = 2
|
||||||
PATH_EXISTS = 3
|
INVALID_PATH = 3
|
||||||
NO_SUCH_FOLDER = 4
|
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
|
@staticmethod
|
||||||
def add(name, path):
|
def add(name, path):
|
||||||
@ -30,8 +49,11 @@ class FolderManager:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete(uid):
|
def delete(uid):
|
||||||
folder = Folder.query.get(uid)
|
status, folder = FolderManager.get(uid)
|
||||||
if folder is None or not folder.root:
|
if status != FolderManager.SUCCESS:
|
||||||
|
return status
|
||||||
|
|
||||||
|
if not folder.root:
|
||||||
return FolderManager.NO_SUCH_FOLDER
|
return FolderManager.NO_SUCH_FOLDER
|
||||||
|
|
||||||
# delete associated tracks and prune empty albums/artists
|
# delete associated tracks and prune empty albums/artists
|
||||||
@ -63,10 +85,23 @@ class FolderManager:
|
|||||||
return FolderManager.NO_SUCH_FOLDER
|
return FolderManager.NO_SUCH_FOLDER
|
||||||
return FolderManager.delete(folder.id)
|
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
|
@staticmethod
|
||||||
def error_str(err):
|
def error_str(err):
|
||||||
if err == FolderManager.SUCCESS:
|
if err == FolderManager.SUCCESS:
|
||||||
return 'No error'
|
return 'No error'
|
||||||
|
elif err == FolderManager.INVALID_ID:
|
||||||
|
return 'Invalid folder id'
|
||||||
elif err == FolderManager.NAME_EXISTS:
|
elif err == FolderManager.NAME_EXISTS:
|
||||||
return 'There is already a folder with that name. Please pick another one.'
|
return 'There is already a folder with that name. Please pick another one.'
|
||||||
elif err == FolderManager.INVALID_PATH:
|
elif err == FolderManager.INVALID_PATH:
|
||||||
|
Loading…
Reference in New Issue
Block a user