1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-15 06:32:16 +00:00
supysonic/cli.py

147 lines
4.0 KiB
Python
Raw Normal View History

2013-09-05 15:17:25 +00:00
# coding: utf-8
2013-09-05 15:17:25 +00:00
import config
config.check()
from web import app
import db
from flask.ext.script import Manager, Command, Option, prompt_pass
import os.path
from managers.folder import FolderManager
from managers.user import UserManager
from scanner import Scanner
manager = Manager(app)
@manager.command
def folder_list():
"Lists all Folders to Scan"
print 'Name\t\tPath\n----\t\t----'
print '\n'.join('{0: <16}{1}'.format(f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
@manager.command
def folder_add(name, path):
"Add a folder to the Library"
ret = FolderManager.add(path)
if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret)
else:
print "Folder '{}' added".format(name)
@manager.command
def folder_delete(path):
"Delete folder from Library"
s = Scanner(db.session)
ret = FolderManager.delete_by_name(path, s)
if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret)
else:
print "Deleted folder" + path
@manager.command
def folder_scan():
s = Scanner(db.session)
folders = db.Folder.query.filter(db.Folder.root == True)
if folders:
for folder in folders:
print "Scanning: " + folder.path
FolderManager.scan(folder.id, s)
added, deleted = s.stats()
print "\a"
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])
@manager.command
def folder_prune():
s = Scanner(db.session)
folders = db.Folder.query.filter(db.Folder.root == True)
if folders:
for folder in folders:
print "Pruning: " + folder.path
FolderManager.prune(folder.id, s)
added, deleted = s.stats()
print "\a"
print "Pruning 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])
@manager.command
def user_list():
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----'
print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in db.User.query.all())
@manager.command
def user_add(name, admin=False, email=None):
password = prompt_pass("Please enter a password")
if password:
status = UserManager.add(name, password, email, admin)
if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status)
@manager.command
def user_delete(name):
user = db.User.query.filter(db.User.name == name).first()
if not user:
print >>sys.stderr, 'No such user'
else:
db.session.delete(user)
db.session.commit()
print "User '{}' deleted".format(name)
@manager.command
def user_setadmin(name, off):
user = db.User.query.filter(db.User.name == name).first()
if not user:
print >>sys.stderr, 'No such user'
else:
user.admin = not off
db.session.commit()
print "{0} '{1}' admin rights".format('Revoked' if off else 'Granted', name)
@manager.command
def user_changepass(name, password):
if not password:
password = getpass.getpass()
confirm = getpass.getpass('Confirm password: ')
if password != confirm:
print >>sys.stderr, "Passwords don't match"
return
status = UserManager.change_password2(name, password)
if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status)
else:
print "Successfully changed '{}' password".format(name)
@manager.command
def init_db():
db.init_db()
@manager.command
def recreate_db():
db.recreate_db()
2013-09-05 15:17:25 +00:00
2013-09-07 10:58:28 +00:00
2013-09-05 15:17:25 +00:00
if __name__ == "__main__":
import config
2013-09-05 15:17:25 +00:00
if not config.check():
sys.exit(1)
2013-09-05 15:17:25 +00:00
if not os.path.exists(config.get('base', 'cache_dir')):
os.makedirs(config.get('base', 'cache_dir'))
2013-09-05 15:17:25 +00:00
manager.run()