mirror of
https://github.com/spl0k/supysonic.git
synced 2024-12-22 17:06:17 +00:00
CLI: use argparse?
This commit is contained in:
parent
1994e1e1a2
commit
4f12fc25f0
70
cli.py
70
cli.py
@ -1,8 +1,13 @@
|
||||
# coding: utf-8
|
||||
|
||||
import sys, cmd
|
||||
import sys, cmd, argparse
|
||||
import config
|
||||
|
||||
class CLIParser(argparse.ArgumentParser):
|
||||
def error(self, message):
|
||||
self.print_usage(sys.stderr)
|
||||
raise RuntimeError(message)
|
||||
|
||||
class CLI(cmd.Cmd):
|
||||
prompt = "supysonic> "
|
||||
|
||||
@ -16,35 +21,43 @@ class CLI(cmd.Cmd):
|
||||
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
|
||||
folder_parser = CLIParser(prog = 'folder', add_help = False)
|
||||
folder_subparsers = folder_parser.add_subparsers(dest = 'action')
|
||||
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 '\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>'
|
||||
elif args.action == 'add':
|
||||
ret = FolderManager.add(args.name, args.path)
|
||||
if ret != FolderManager.SUCCESS:
|
||||
print FolderManager.error_str(ret)
|
||||
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>'
|
||||
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:
|
||||
ret = FolderManager.delete_by_name(args[0])
|
||||
if ret != FolderManager.SUCCESS:
|
||||
print FolderManager.error_str(ret)
|
||||
else:
|
||||
print "Deleted folder '%s'" % args[0]
|
||||
elif action == 'scan':
|
||||
print "Deleted folder '%s'" % args.name
|
||||
elif args.action == 'scan':
|
||||
s = Scanner(db.session)
|
||||
if args:
|
||||
folders = map(lambda n: db.Folder.query.filter(db.Folder.name == n and db.Folder.root == True).first() or n, 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)
|
||||
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):
|
||||
@ -59,14 +72,11 @@ class CLI(cmd.Cmd):
|
||||
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])
|
||||
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'"
|
||||
print "folder scan [name [name [...]]]\n\tRuns a scan on folder 'name'. If 'name' is ommited, all folders\n\tare scanned"
|
||||
self.folder_parser.print_help()
|
||||
#for cmd, parser in self.folder_subparsers.choices.iteritems():
|
||||
# parser.print_help()
|
||||
|
||||
if __name__ == "__main__":
|
||||
if not config.check():
|
||||
|
Loading…
Reference in New Issue
Block a user