mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-23 01:16:18 +00:00
CLI: autogenerate do_ and help_ methods from parsers
This commit is contained in:
parent
ff607d506f
commit
91b4412152
230
cli.py
230
cli.py
@ -11,6 +11,47 @@ class CLIParser(argparse.ArgumentParser):
|
|||||||
class CLI(cmd.Cmd):
|
class CLI(cmd.Cmd):
|
||||||
prompt = "supysonic> "
|
prompt = "supysonic> "
|
||||||
|
|
||||||
|
def _make_do(self, command):
|
||||||
|
def method(obj, line):
|
||||||
|
try:
|
||||||
|
args = getattr(obj, command + '_parser').parse_args(line.split())
|
||||||
|
except RuntimeError, e:
|
||||||
|
print >>sys.stderr, e
|
||||||
|
return
|
||||||
|
|
||||||
|
if hasattr(obj.__class__, command + '_subparsers'):
|
||||||
|
try:
|
||||||
|
func = getattr(obj, '{}_{}'.format(command, args.action))
|
||||||
|
except AttributeError:
|
||||||
|
return obj.default(line)
|
||||||
|
return func(** { key: vars(args)[key] for key in vars(args) if key != 'action' })
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
func = getattr(obj, command)
|
||||||
|
except AttributeError:
|
||||||
|
return obj.default(line)
|
||||||
|
return func(**vars(args))
|
||||||
|
|
||||||
|
return method
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
cmd.Cmd.__init__(self)
|
||||||
|
|
||||||
|
# Generate do_* and help_* methods
|
||||||
|
for parser_name in filter(lambda attr: attr.endswith('_parser') and '_' not in attr[:-7], dir(self.__class__)):
|
||||||
|
command = parser_name[:-7]
|
||||||
|
|
||||||
|
if not hasattr(self.__class__, 'do_' + command):
|
||||||
|
setattr(self.__class__, 'do_' + command, self._make_do(command))
|
||||||
|
|
||||||
|
if hasattr(self.__class__, 'do_' + command) and not hasattr(self.__class__, 'help_' + command):
|
||||||
|
setattr(self.__class__, 'help_' + command, getattr(self.__class__, parser_name).print_help)
|
||||||
|
if hasattr(self.__class__, command + '_subparsers'):
|
||||||
|
for action in getattr(self.__class__, command + '_subparsers').choices.keys():
|
||||||
|
subparser = getattr(self.__class__, '{}_{}_parser'.format(command, action), None)
|
||||||
|
if subparser:
|
||||||
|
setattr(self, 'help_{} {}'.format(command, action), subparser.print_help)
|
||||||
|
|
||||||
def do_EOF(self, line):
|
def do_EOF(self, line):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -23,7 +64,7 @@ class CLI(cmd.Cmd):
|
|||||||
|
|
||||||
def completedefault(self, text, line, begidx, endidx):
|
def completedefault(self, text, line, begidx, endidx):
|
||||||
command = line.split()[0]
|
command = line.split()[0]
|
||||||
parsers = getattr(self, command + '_subparsers', None)
|
parsers = getattr(self.__class__, command + '_subparsers', None)
|
||||||
if not parsers:
|
if not parsers:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -38,56 +79,47 @@ class CLI(cmd.Cmd):
|
|||||||
folder_add_parser = folder_subparsers.add_parser('add', help = 'Adds a folder', add_help = False)
|
folder_add_parser = folder_subparsers.add_parser('add', help = 'Adds a folder', add_help = False)
|
||||||
folder_add_parser.add_argument('name', help = 'Name of the folder to add')
|
folder_add_parser.add_argument('name', help = 'Name of the folder to add')
|
||||||
folder_add_parser.add_argument('path', help = 'Path to the directory pointed by the folder')
|
folder_add_parser.add_argument('path', help = 'Path to the directory pointed by the folder')
|
||||||
folder_del_parser = folder_subparsers.add_parser('delete', help = 'Deletes a folder', add_help = False)
|
folder_delete_parser = folder_subparsers.add_parser('delete', help = 'Deletes a folder', add_help = False)
|
||||||
folder_del_parser.add_argument('name', help = 'Name of the folder to delete')
|
folder_delete_parser.add_argument('name', help = 'Name of the folder to delete')
|
||||||
folder_scan_parser = folder_subparsers.add_parser('scan', help = 'Run a scan on specified folders', add_help = False)
|
folder_scan_parser = folder_subparsers.add_parser('scan', help = 'Run a scan on specified folders', add_help = False)
|
||||||
folder_scan_parser.add_argument('folders', metavar = 'folder', nargs = '*', help = 'Folder(s) to be scanned. If ommitted, all folders are scanned')
|
folder_scan_parser.add_argument('folders', metavar = 'folder', nargs = '*', help = 'Folder(s) to be scanned. If ommitted, all folders are scanned')
|
||||||
|
|
||||||
def do_folder(self, line):
|
def folder_list(self):
|
||||||
try:
|
print 'Name\t\tPath\n----\t\t----'
|
||||||
args = self.folder_parser.parse_args(line.split())
|
print '\n'.join('{0: <16}{1}'.format(f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
|
||||||
except RuntimeError, e:
|
|
||||||
print >>sys.stderr, e
|
|
||||||
return
|
|
||||||
|
|
||||||
if args.action == 'list':
|
def folder_add(self, name, path):
|
||||||
print 'Name\t\tPath\n----\t\t----'
|
ret = FolderManager.add(name, path)
|
||||||
print '\n'.join('{0: <16}{1}'.format(f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
|
if ret != FolderManager.SUCCESS:
|
||||||
elif args.action == 'add':
|
print FolderManager.error_str(ret)
|
||||||
ret = FolderManager.add(args.name, args.path)
|
else:
|
||||||
if ret != FolderManager.SUCCESS:
|
print "Folder '{}' added".format(name)
|
||||||
print FolderManager.error_str(ret)
|
|
||||||
else:
|
|
||||||
print "Folder '%s' added" % args.name
|
|
||||||
elif args.action == 'delete':
|
|
||||||
ret = FolderManager.delete_by_name(args.name)
|
|
||||||
if ret != FolderManager.SUCCESS:
|
|
||||||
print FolderManager.error_str(ret)
|
|
||||||
else:
|
|
||||||
print "Deleted folder '%s'" % args.name
|
|
||||||
elif args.action == 'scan':
|
|
||||||
s = Scanner(db.session)
|
|
||||||
if args.folders:
|
|
||||||
folders = map(lambda n: db.Folder.query.filter(db.Folder.name == n and db.Folder.root == True).first() or n, args.folders)
|
|
||||||
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()
|
def folder_delete(self, name):
|
||||||
db.session.commit()
|
ret = FolderManager.delete_by_name(name)
|
||||||
|
if ret != FolderManager.SUCCESS:
|
||||||
|
print FolderManager.error_str(ret)
|
||||||
|
else:
|
||||||
|
print "Deleted folder '{}'".format(name)
|
||||||
|
|
||||||
print "Scanning done"
|
def folder_scan(self, folders):
|
||||||
print 'Added: %i artists, %i albums, %i tracks' % (added[0], added[1], added[2])
|
s = Scanner(db.session)
|
||||||
print 'Deleted: %i artists, %i albums, %i tracks' % (deleted[0], deleted[1], deleted[2])
|
if folders:
|
||||||
|
folders = map(lambda n: db.Folder.query.filter(db.Folder.name == n and db.Folder.root == True).first() or n, folders)
|
||||||
|
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)
|
||||||
|
|
||||||
def help_folder(self):
|
added, deleted = s.stats()
|
||||||
self.folder_parser.print_help()
|
db.session.commit()
|
||||||
#for cmd, parser in self.folder_subparsers.choices.iteritems():
|
|
||||||
# parser.print_help()
|
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])
|
||||||
|
|
||||||
user_parser = CLIParser(prog = 'user', add_help = False)
|
user_parser = CLIParser(prog = 'user', add_help = False)
|
||||||
user_subparsers = user_parser.add_subparsers(dest = 'action')
|
user_subparsers = user_parser.add_subparsers(dest = 'action')
|
||||||
@ -97,68 +129,60 @@ class CLI(cmd.Cmd):
|
|||||||
user_add_parser.add_argument('-a', '--admin', action = 'store_true', help = 'Give admin rights to the new user')
|
user_add_parser.add_argument('-a', '--admin', action = 'store_true', help = 'Give admin rights to the new user')
|
||||||
user_add_parser.add_argument('-p', '--password', help = "Specifies the user's password")
|
user_add_parser.add_argument('-p', '--password', help = "Specifies the user's password")
|
||||||
user_add_parser.add_argument('-e', '--email', default = '', help = "Sets the user's email address")
|
user_add_parser.add_argument('-e', '--email', default = '', help = "Sets the user's email address")
|
||||||
user_del_parser = user_subparsers.add_parser('delete', help = 'Deletes a user', add_help = False)
|
user_delete_parser = user_subparsers.add_parser('delete', help = 'Deletes a user', add_help = False)
|
||||||
user_del_parser.add_argument('name', help = 'Name/login of the user to delete')
|
user_delete_parser.add_argument('name', help = 'Name/login of the user to delete')
|
||||||
user_admin_parser = user_subparsers.add_parser('set-admin', help = 'Enable/disable admin rights for a user', add_help = False)
|
user_setadmin_parser = user_subparsers.add_parser('setadmin', help = 'Enable/disable admin rights for a user', add_help = False)
|
||||||
user_admin_parser.add_argument('name', help = 'Name/login of the user to grant/revoke admin rights')
|
user_setadmin_parser.add_argument('name', help = 'Name/login of the user to grant/revoke admin rights')
|
||||||
user_admin_parser.add_argument('--off', action = 'store_true', help = 'Revoke admin rights if present, grant them otherwise')
|
user_setadmin_parser.add_argument('--off', action = 'store_true', help = 'Revoke admin rights if present, grant them otherwise')
|
||||||
user_pass_parser = user_subparsers.add_parser('changepass', help = "Changes a user's password", add_help = False)
|
user_changepass_parser = user_subparsers.add_parser('changepass', help = "Changes a user's password", add_help = False)
|
||||||
user_pass_parser.add_argument('name', help = 'Name/login of the user to which change the password')
|
user_changepass_parser.add_argument('name', help = 'Name/login of the user to which change the password')
|
||||||
user_pass_parser.add_argument('password', nargs = '?', help = 'New password')
|
user_changepass_parser.add_argument('password', nargs = '?', help = 'New password')
|
||||||
|
|
||||||
def do_user(self, line):
|
def user_list(self):
|
||||||
try:
|
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----'
|
||||||
args = self.user_parser.parse_args(line.split())
|
print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in db.User.query.all())
|
||||||
except RuntimeError, e:
|
|
||||||
print >>sys.stderr, e
|
|
||||||
return
|
|
||||||
|
|
||||||
if args.action == 'list':
|
def user_add(self, name, admin, password, email):
|
||||||
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----'
|
if not password:
|
||||||
print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in db.User.query.all())
|
password = getpass.getpass()
|
||||||
elif args.action == 'add':
|
confirm = getpass.getpass('Confirm password: ')
|
||||||
if not args.password:
|
if password != confirm:
|
||||||
password = getpass.getpass()
|
print >>sys.stderr, "Passwords don't match"
|
||||||
confirm = getpass.getpass('Confirm password: ')
|
return
|
||||||
if password != confirm:
|
status = UserManager.add(name, password, email, admin)
|
||||||
print >>sys.stderr, "Passwords don't match"
|
if status != UserManager.SUCCESS:
|
||||||
return
|
print >>sys.stderr, UserManager.error_str(status)
|
||||||
args.password = password
|
|
||||||
status = UserManager.add(args.name, args.password, args.email, args.admin)
|
|
||||||
if status != UserManager.SUCCESS:
|
|
||||||
print >>sys.stderr, UserManager.error_str(status)
|
|
||||||
elif args.action == 'delete':
|
|
||||||
user = db.User.query.filter(db.User.name == args.name).first()
|
|
||||||
if not user:
|
|
||||||
print >>sys.stderr, 'No such user'
|
|
||||||
else:
|
|
||||||
db.session.delete(user)
|
|
||||||
db.session.commit()
|
|
||||||
print "User '{0.name}' deleted".format(args)
|
|
||||||
elif args.action == 'set-admin':
|
|
||||||
user = db.User.query.filter(db.User.name == args.name).first()
|
|
||||||
if not user:
|
|
||||||
print >>sys.stderr, 'No such user'
|
|
||||||
else:
|
|
||||||
user.admin = not args.off
|
|
||||||
db.session.commit()
|
|
||||||
print "{0} '{1}' admin rights".format('Revoked' if args.off else 'Granted', args.name)
|
|
||||||
elif args.action == 'changepass':
|
|
||||||
if not args.password:
|
|
||||||
password = getpass.getpass()
|
|
||||||
confirm = getpass.getpass('Confirm password: ')
|
|
||||||
if password != confirm:
|
|
||||||
print >>sys.stderr, "Passwords don't match"
|
|
||||||
return
|
|
||||||
args.password = password
|
|
||||||
status = UserManager.change_password2(args.name, args.password)
|
|
||||||
if status != UserManager.SUCCESS:
|
|
||||||
print >>sys.stderr, UserManager.error_str(status)
|
|
||||||
else:
|
|
||||||
print "Successfully changed '{}' password".format(args.name)
|
|
||||||
|
|
||||||
def help_user(self):
|
def user_delete(self, name):
|
||||||
self.user_parser.print_help()
|
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)
|
||||||
|
|
||||||
|
def user_setadmin(self, 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)
|
||||||
|
|
||||||
|
def user_changepass(self, 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)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not config.check():
|
if not config.check():
|
||||||
|
Loading…
Reference in New Issue
Block a user