1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

CLI: use argparse?

This commit is contained in:
spl0k 2013-09-06 18:22:52 +02:00
parent 1994e1e1a2
commit 4f12fc25f0

70
cli.py
View File

@ -1,8 +1,13 @@
# coding: utf-8 # coding: utf-8
import sys, cmd import sys, cmd, argparse
import config import config
class CLIParser(argparse.ArgumentParser):
def error(self, message):
self.print_usage(sys.stderr)
raise RuntimeError(message)
class CLI(cmd.Cmd): class CLI(cmd.Cmd):
prompt = "supysonic> " prompt = "supysonic> "
@ -16,35 +21,43 @@ class CLI(cmd.Cmd):
def postloop(self): def postloop(self):
print print
def do_folder(self, line): folder_parser = CLIParser(prog = 'folder', add_help = False)
action = line.split()[0] if line else 'list' folder_subparsers = folder_parser.add_subparsers(dest = 'action')
args = line.split()[1:] if line else None folder_subparsers.add_parser('list', help = 'Lists folders', 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('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_del_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.add_argument('folders', metavar = 'folder', nargs = '*', help = 'Folder(s) to be scanned. If ommitted, all folders are scanned')
if action == 'list': def do_folder(self, line):
try:
args = self.folder_parser.parse_args(line.split())
except RuntimeError, e:
print >>sys.stderr, e.message
return
if args.action == 'list':
print 'Name\t\tPath\n----\t\t----' 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)) 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': elif args.action == 'add':
if len(args) < 2: ret = FolderManager.add(args.name, args.path)
print 'Missing argument. folder add <name> <path>' if ret != FolderManager.SUCCESS:
print FolderManager.error_str(ret)
else: else:
ret = FolderManager.add(args[0], args[1]) print "Folder '%s' added" % args.name
if ret != FolderManager.SUCCESS: elif args.action == 'delete':
print FolderManager.error_str(ret) ret = FolderManager.delete_by_name(args.name)
else: if ret != FolderManager.SUCCESS:
print "Folder '%s' added" % args[0] print FolderManager.error_str(ret)
elif action == 'delete':
if len(args) < 1:
print 'Missing argument. folder delete <name>'
else: else:
ret = FolderManager.delete_by_name(args[0]) print "Deleted folder '%s'" % args.name
if ret != FolderManager.SUCCESS: elif args.action == 'scan':
print FolderManager.error_str(ret)
else:
print "Deleted folder '%s'" % args[0]
elif action == 'scan':
s = Scanner(db.session) s = Scanner(db.session)
if args: 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 = 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)): 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):
@ -59,14 +72,11 @@ class CLI(cmd.Cmd):
print "Scanning done" print "Scanning done"
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])
else:
print "Unknown action '%s'" % action
def help_folder(self): def help_folder(self):
print "folder\nfolder list\n\tDisplays the list of folders" self.folder_parser.print_help()
print "folder add <name> <path>\n\tAdds a music folder 'name' pointing to folder 'path'" #for cmd, parser in self.folder_subparsers.choices.iteritems():
print "folder delete <name>\n\tDeletes folder 'name'" # parser.print_help()
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():