Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions ma-shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from optparse import OptionParser
from xmlrpclib import ServerProxy, Fault, DateTime, ProtocolError
import socket
import re
try:
import json
except:
Expand All @@ -40,8 +41,11 @@
print "(notice: json not found, please install simplejson)"
try:
import readline
except:
print "(notice: readline support not found)"
readline.parse_and_bind('TAB: complete')
# Press Ctrl+x to list plausible completions (mainly for windows which lacks double-tab)
readline.parse_and_bind('"\\C-x": possible-completions')
except StandardError as e:
print "(notice: readline support not found)", e

class JSONDateEncoder(json.JSONEncoder):
"""JSON Encoder to support DateTime objects"""
Expand Down Expand Up @@ -204,7 +208,9 @@ def execute(self, cmd, quiet=False):
return True

def run(self):

if 'readline' in globals():
self.old_completer = readline.get_completer()
readline.set_completer(self.completer)
if self.args:
self.execute(self.args, quiet=True)
return
Expand All @@ -224,11 +230,27 @@ def run(self):
if not cmd:
continue
else:
cmd = cmd.strip().split(' ')
cmd = re.split(r'\s*', cmd.strip())

if not self.execute(cmd):
break

if hasattr(self, 'old_completer'):
readline.set_completer(self.old_completer)

def completer(self, text, state):
line = readline.get_line_buffer()
start = readline.get_begidx()
# Check that `text` has nothing but possibly some whitespace before it
if start > len(re.match(r'^\s*(?:help)?\s*', line).group(0)):
return

if state == 0:
self.matches = filter((lambda m: m.startswith(text)), self.methods + ['help', 'quit'])
if state < len(self.matches):
return self.matches[state]

return

if __name__ == "__main__":
main = Main()
Expand Down