1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

CLI: autogenerate do_ and help_ methods from parsers

This commit is contained in:
spl0k 2013-09-08 17:23:38 +02:00
parent ff607d506f
commit 91b4412152

144
cli.py
View File

@ -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,37 +79,33 @@ 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:
args = self.folder_parser.parse_args(line.split())
except RuntimeError, e:
print >>sys.stderr, e
return
if args.action == 'list':
print 'Name\t\tPath\n----\t\t----' 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)) print '\n'.join('{0: <16}{1}'.format(f.name, f.path) for f in db.Folder.query.filter(db.Folder.root == True))
elif args.action == 'add':
ret = FolderManager.add(args.name, args.path) def folder_add(self, name, path):
ret = FolderManager.add(name, path)
if ret != FolderManager.SUCCESS: if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret) print FolderManager.error_str(ret)
else: else:
print "Folder '%s' added" % args.name print "Folder '{}' added".format(name)
elif args.action == 'delete':
ret = FolderManager.delete_by_name(args.name) def folder_delete(self, name):
ret = FolderManager.delete_by_name(name)
if ret != FolderManager.SUCCESS: if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret) print FolderManager.error_str(ret)
else: else:
print "Deleted folder '%s'" % args.name print "Deleted folder '{}'".format(name)
elif args.action == 'scan':
def folder_scan(self, folders):
s = Scanner(db.session) s = Scanner(db.session)
if args.folders: if folders:
folders = map(lambda n: db.Folder.query.filter(db.Folder.name == n and db.Folder.root == True).first() or n, args.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)): if any(map(lambda f: isinstance(f, basestring), folders)):
print "No such folder(s): " + ' '.join(f for f in folders if isinstance(f, basestring)) 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): for folder in filter(lambda f: isinstance(f, db.Folder), folders):
@ -84,11 +121,6 @@ class CLI(cmd.Cmd):
print 'Added: %i artists, %i albums, %i tracks' % (added[0], added[1], added[2]) 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]) print 'Deleted: %i artists, %i albums, %i tracks' % (deleted[0], deleted[1], deleted[2])
def help_folder(self):
self.folder_parser.print_help()
#for cmd, parser in self.folder_subparsers.choices.iteritems():
# parser.print_help()
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')
user_subparsers.add_parser('list', help = 'List users', add_help = False) user_subparsers.add_parser('list', help = 'List users', add_help = False)
@ -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:
args = self.user_parser.parse_args(line.split())
except RuntimeError, e:
print >>sys.stderr, e
return
if args.action == 'list':
print 'Name\t\tAdmin\tEmail\n----\t\t-----\t-----' 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()) print '\n'.join('{0: <16}{1}\t{2}'.format(u.name, '*' if u.admin else '', u.mail) for u in db.User.query.all())
elif args.action == 'add':
if not args.password: def user_add(self, name, admin, password, email):
if not password:
password = getpass.getpass() password = getpass.getpass()
confirm = getpass.getpass('Confirm password: ') confirm = getpass.getpass('Confirm password: ')
if password != confirm: if password != confirm:
print >>sys.stderr, "Passwords don't match" print >>sys.stderr, "Passwords don't match"
return return
args.password = password status = UserManager.add(name, password, email, admin)
status = UserManager.add(args.name, args.password, args.email, args.admin)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status) print >>sys.stderr, UserManager.error_str(status)
elif args.action == 'delete':
user = db.User.query.filter(db.User.name == args.name).first() def user_delete(self, name):
user = db.User.query.filter(db.User.name == name).first()
if not user: if not user:
print >>sys.stderr, 'No such user' print >>sys.stderr, 'No such user'
else: else:
db.session.delete(user) db.session.delete(user)
db.session.commit() db.session.commit()
print "User '{0.name}' deleted".format(args) print "User '{}' deleted".format(name)
elif args.action == 'set-admin':
user = db.User.query.filter(db.User.name == args.name).first() def user_setadmin(self, name, off):
user = db.User.query.filter(db.User.name == name).first()
if not user: if not user:
print >>sys.stderr, 'No such user' print >>sys.stderr, 'No such user'
else: else:
user.admin = not args.off user.admin = not off
db.session.commit() db.session.commit()
print "{0} '{1}' admin rights".format('Revoked' if args.off else 'Granted', args.name) print "{0} '{1}' admin rights".format('Revoked' if off else 'Granted', name)
elif args.action == 'changepass':
if not args.password: def user_changepass(self, name, password):
if not password:
password = getpass.getpass() password = getpass.getpass()
confirm = getpass.getpass('Confirm password: ') confirm = getpass.getpass('Confirm password: ')
if password != confirm: if password != confirm:
print >>sys.stderr, "Passwords don't match" print >>sys.stderr, "Passwords don't match"
return return
args.password = password status = UserManager.change_password2(name, password)
status = UserManager.change_password2(args.name, args.password)
if status != UserManager.SUCCESS: if status != UserManager.SUCCESS:
print >>sys.stderr, UserManager.error_str(status) print >>sys.stderr, UserManager.error_str(status)
else: else:
print "Successfully changed '{}' password".format(args.name) print "Successfully changed '{}' password".format(name)
def help_user(self):
self.user_parser.print_help()
if __name__ == "__main__": if __name__ == "__main__":
if not config.check(): if not config.check():