Skip to content

Commit

Permalink
Add more help, tab completion, error handling to query command.
Browse files Browse the repository at this point in the history
  • Loading branch information
tohojo committed Apr 26, 2012
1 parent e7485fb commit 0953034
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions interface.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def gen_help(self, method):


def do_help(self, arg): def do_help(self, arg):
if arg in ('status', 'query', 'result'): if arg in ('status', 'query', 'result'):
super(Interface, self).do_help(arg) Console.do_help(self, arg)
else: else:
print "\n".join(['These are the accepted commands.', print "\n".join(['These are the accepted commands.',
'Type help <command> to get help on a specific command.', 'Type help <command> to get help on a specific command.',
Expand Down Expand Up @@ -78,25 +78,69 @@ def help_status(self):
print self.gen_help("do_status") print self.gen_help("do_status")


def do_query(self, arg): def do_query(self, arg):
"""Manipulate the query.
query [show] Show current query.
query reset Reset query to be empty.
query set <key> <value> Set query attribute <key> to <value>.
query unset <key> Unset query attribute <key>.
query keys List possible query keys.
query run Run the current query."""
if arg in ('', 'show'): if arg in ('', 'show'):
if self.query: if self.query:
print_table([self.query], ["Attribute", "Value"]) print_table([self.query], ["Attribute", "Value"])
else: else:
print "No current query." print "No current query."
elif arg == "reset": elif arg == "reset":
self.query = Case() self.query = Case()
print "Query reset."
elif arg.startswith('set'): elif arg.startswith('set'):
arg,key,val = arg.split() parts = arg.split(None, 2)
self.query[key] = val if len(parts) < 3:
print "Set '%s' with value '%s' to query." % (key,val) print "Usage: query set <key> <value>."
return
arg,key,val = parts
try:
self.query[key] = val
except KeyError:
print "Invalid attribute name '%s'." % key
print "Possible attribute keys:"
print "\n".join([" "+i for i in possible_attributes])
except ValueError, e:
print str(e)
elif arg.startswith('unset'): elif arg.startswith('unset'):
arg,key = arg.split() parts = arg.split(None)
if len(parts) < 2:
print "Usage: query unset <key>."
return
arg,key = parts[:2]
if not key in self.query:
print "Attribute '%s' not found." % key
return
del self.query[key] del self.query[key]
print "Unset attribute '%s' from query." % key elif arg.startswith('keys'):
print "Possible attribute keys:"
print "\n".join([" "+i for i in possible_attributes])
else: else:
print "Unrecognised argument. Type 'help query' for help." print "Unrecognised argument. Type 'help query' for help."


def help_query(self):
print self.gen_help("do_query")

def complete_query(self, text, line, begidx, endidx):
if re.match(r"\s*query\s+set", line):
args = possible_attributes
elif re.match(r"\s*query\s+unset", line):
args = self.query.keys()
elif re.match(r"\s*query\s+([^\s]+)?$", line):
args = ['show','reset','set','unset','keys','run']
else:
return []
if not text:
return args
else:
return [i for i in args if i.startswith(text)]


def default(self, line): def default(self, line):
print "Invalid command. Type 'help' for a list of commands." print "Invalid command. Type 'help' for a list of commands."


Expand Down

0 comments on commit 0953034

Please sign in to comment.