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

Beginning of a command-line interface

This commit is contained in:
spl0k 2013-09-05 17:17:25 +02:00
parent e523e01760
commit 5d7be6c416
3 changed files with 156 additions and 42 deletions

65
cli.py Executable file
View File

@ -0,0 +1,65 @@
# coding: utf-8
import sys, cmd
import config
class CLI(cmd.Cmd):
prompt = "supysonic> "
def do_EOF(self, line):
return True
def default(self, line):
print 'Unknown command %s' % line.split()[0]
self.do_help(None)
def postloop(self):
print
def do_folder(self, line):
action = line.split()[0] if line else 'list'
args = line.split()[1:] if line else None
if action == 'list':
print 'Name\t\tPath\n----\t\t----'
print '\n'.join('%s\t\t%s' % (f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
elif action == 'add':
if len(args) < 2:
print 'Missing argument. folder add <name> <path>'
else:
ret = FolderManager.add(args[0], args[1])
if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret)
else:
print "Folder '%s' added" % args[0]
elif action == 'delete':
if len(args) < 1:
print 'Missing argument. folder delete <name>'
else:
ret = FolderManager.delete_by_name(args[0])
if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret)
else:
print "Deleted folder '%s'" % args[0]
else:
print "Unknown action '%s'" % action
def help_folder(self):
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 delete <name>\n\tDeletes folder <name>"
if __name__ == "__main__":
if not config.check():
sys.exit(1)
import db
db.init_db()
from folder_manager import FolderManager
if len(sys.argv) > 1:
CLI().onecmd(' '.join(sys.argv[1:]))
else:
CLI().cmdloop()

View File

@ -5,9 +5,10 @@ import os.path
import uuid import uuid
from web import app from web import app
from db import session, Folder, Artist from db import session, Folder
from scanner import Scanner from scanner import Scanner
from user_manager import UserManager from user_manager import UserManager
from folder_manager import FolderManager
@app.before_request @app.before_request
def check_admin(): def check_admin():
@ -31,27 +32,17 @@ def add_folder():
if name in (None, ''): if name in (None, ''):
flash('The name is required.') flash('The name is required.')
error = True error = True
elif Folder.query.filter(Folder.name == name and Folder.root).first():
flash('There is already a folder with that name. Please pick another one.')
error = True
if path in (None, ''): if path in (None, ''):
flash('The path is required.') flash('The path is required.')
error = True error = True
else:
path = os.path.abspath(path)
if not os.path.isdir(path):
flash("The path '%s' doesn't exists or isn't a directory" % path)
error = True
folder = Folder.query.filter(Folder.path == path).first()
if folder:
flash("This path is already registered")
error = True
if error: if error:
return render_template('addfolder.html') return render_template('addfolder.html')
folder = Folder(root = True, name = name, path = path) ret = FolderManager.add(name, path)
session.add(folder) if ret != FolderManager.SUCCESS:
session.commit() flash(FolderManager.error_str(ret))
return render_template('addfolder.html')
flash("Folder '%s' created. You should now run a scan" % name) flash("Folder '%s' created. You should now run a scan" % name)
return redirect(url_for('folder_index')) return redirect(url_for('folder_index'))
@ -64,32 +55,11 @@ def del_folder(id):
flash('Invalid folder id') flash('Invalid folder id')
return redirect(url_for('folder_index')) return redirect(url_for('folder_index'))
folder = Folder.query.get(idid) ret = FolderManager.delete(idid)
if folder is None or not folder.root: if ret != FolderManager.SUCCESS:
flash('No such folder') flash(FolderManager.error_str(ret))
return redirect(url_for('folder_index')) else:
flash('Deleted folder')
# delete associated tracks and prune empty albums/artists
for artist in Artist.query.all():
for album in artist.albums[:]:
for track in filter(lambda t: t.root_folder.id == folder.id, album.tracks):
album.tracks.remove(track)
session.delete(track)
if len(album.tracks) == 0:
artist.albums.remove(album)
session.delete(album)
if len(artist.albums) == 0:
session.delete(artist)
def cleanup_folder(folder):
for f in folder.children:
cleanup_folder(f)
session.delete(folder)
cleanup_folder(folder)
session.commit()
flash("Deleted folder '%s'" % folder.name)
return redirect(url_for('folder_index')) return redirect(url_for('folder_index'))

79
folder_manager.py Executable file
View File

@ -0,0 +1,79 @@
# coding: utf-8
import os.path
from db import Folder, Artist, session
class FolderManager:
SUCCESS = 0
NAME_EXISTS = 1
INVALID_PATH = 2
PATH_EXISTS = 3
NO_SUCH_FOLDER = 4
@staticmethod
def add(name, path):
if Folder.query.filter(Folder.name == name and Folder.root == True).first():
return FolderManager.NAME_EXISTS
path = os.path.abspath(path)
if not os.path.isdir(path):
return FolderManager.INVALID_PATH
folder = Folder.query.filter(Folder.path == path).first()
if folder:
return FolderManager.PATH_EXISTS
folder = Folder(root = True, name = name, path = path)
session.add(folder)
session.commit()
return FolderManager.SUCCESS
@staticmethod
def delete(uid):
folder = Folder.query.get(uid)
if folder is None or not folder.root:
return FolderManager.NO_SUCH_FOLDER
# delete associated tracks and prune empty albums/artists
for artist in Artist.query.all():
for album in artist.albums[:]:
for track in filter(lambda t: t.root_folder.id == folder.id, album.tracks):
album.tracks.remove(track)
session.delete(track)
if len(album.tracks) == 0:
artist.albums.remove(album)
session.delete(album)
if len(artist.albums) == 0:
session.delete(artist)
def cleanup_folder(folder):
for f in folder.children:
cleanup_folder(f)
session.delete(folder)
cleanup_folder(folder)
session.commit()
return FolderManager.SUCCESS
@staticmethod
def delete_by_name(name):
folder = Folder.query.filter(Folder.name == name and Folder.root == True).first()
if not folder:
return FolderManager.NO_SUCH_FOLDER
return FolderManager.delete(folder.id)
@staticmethod
def error_str(err):
if err == FolderManager.SUCCESS:
return 'No error'
elif err == FolderManager.NAME_EXISTS:
return 'There is already a folder with that name. Please pick another one.'
elif err == FolderManager.INVALID_PATH:
return "The path doesn't exists or isn't a directory"
elif err == FolderManager.PATH_EXISTS:
return 'This path is already registered'
elif err == FolderManager.NO_SUCH_FOLDER:
return 'No such folder'
return 'Unknown error'