Allow the user to specify the broker hostname and port, and make exception
debugging a selectable option.
Signed-off-by: Zane Bitter <zbitter(a)redhat.com>
---
src/python/matahari/api.py | 2 +
src/python/matahari/shell/__init__.py | 37 +++++++++++++++++---
.../matahari/shell/interpreter/interpreter.py | 10 +----
src/python/mhsh | 26 ++++++++++++--
4 files changed, 58 insertions(+), 17 deletions(-)
diff --git a/src/python/matahari/api.py b/src/python/matahari/api.py
index d48ab35..6583103 100644
--- a/src/python/matahari/api.py
+++ b/src/python/matahari/api.py
@@ -20,7 +20,7 @@ import core
class BrokerConnection(object):
def __init__(self, host='localhost', port=49000, ssl=False):
self.host = host
- self.port = 49000
+ self.port = port
self.ssl = ssl
class Proxy(object):
diff --git a/src/python/matahari/shell/__init__.py
b/src/python/matahari/shell/__init__.py
index 2cf3855..a72c559 100644
--- a/src/python/matahari/shell/__init__.py
+++ b/src/python/matahari/shell/__init__.py
@@ -15,15 +15,42 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
import matahari.api
import mode
import interpreter
+
class MatahariShell(object):
- def __init__(self, prompt='mhsh'):
- self.state = matahari.api.Matahari()
+ """A Matahari Shell object, with its own API state."""
+
+ def __init__(self, connection=None, debug=False):
+ """
+ Initialise with optional connection parameters.
+ Specify debug=True to print all exceptions.
+ """
+ args = []
+ if connection is not None:
+ args.append(connection)
+ self.state = matahari.api.Matahari(*args)
initial_mode = mode.RootMode(self.state)
- self.interpreter = interpreter.Interpreter(prompt, initial_mode, debug=True)
+ self.interpreter = interpreter.Interpreter('mhsh', initial_mode,
+ debug=debug)
+
+ def __call__(self, *args):
+ """
+ Run the shell, either in interactive mode or executing the specified
+ scripts.
+ """
+ def getscript(arg):
+ if arg == '-':
+ import sys
+ return sys.stdin
+ return file(arg)
- def __call__(self):
- self.interpreter.cmdloop()
+ if args:
+ for arg in args:
+ with getscript(arg) as script:
+ self.interpreter.runscript(script)
+ else:
+ self.interpreter.cmdloop()
diff --git a/src/python/matahari/shell/interpreter/interpreter.py
b/src/python/matahari/shell/interpreter/interpreter.py
index f641237..a8722c7 100644
--- a/src/python/matahari/shell/interpreter/interpreter.py
+++ b/src/python/matahari/shell/interpreter/interpreter.py
@@ -22,6 +22,7 @@ The commands supported by the interpreter are supplied by the current
mode,
which can be changed by passing a new mode to the set_mode() method.
"""
+import sys
import cmd
import mode
@@ -85,7 +86,7 @@ class Interpreter(cmd.Cmd):
if self.debug:
import traceback
- self.stdout.write(traceback.format_exc())
+ sys.stderr.write(traceback.format_exc())
def cmdloop(self, *args, **kwargs):
while True:
@@ -133,10 +134,3 @@ class Interpreter(cmd.Cmd):
return (filter(lambda n: n != 'do_EOF', cmd.Cmd.get_names(self)) +
['%s_%s' % (p, c) for p in ['do', 'complete',
'help']
for c in self.mode])
-
-
-if __name__ == '__main__':
- mode = mode.Mode()
- shell = Interpreter('mhsh', mode)
- shell.cmdloop()
-
diff --git a/src/python/mhsh b/src/python/mhsh
index ff26b30..9e2f216 100755
--- a/src/python/mhsh
+++ b/src/python/mhsh
@@ -1,10 +1,30 @@
#!/usr/bin/env python
-import matahari.shell
+import matahari.shell, matahari.api
+
+
+def getoptions():
+ from optparse import OptionParser
+
+ parser = OptionParser('usage: %prog [-d] [-b BROKER] [-p PORT] [SCRIPT]')
+ parser.add_option('-d', '--debug', dest='debug',
action='store_true',
+ default=False, help='Enable debugging output')
+ parser.add_option('-b', '--broker', dest='broker',
action='store',
+ default='localhost', type='string',
+ help='Specify the broker to connect to')
+ parser.add_option('-p', '--port', dest='port',
action='store',
+ default=49000, type='int',
+ help='Specify the broker port to connect to')
+ return parser.parse_args()
def main():
- shell = matahari.shell.MatahariShell()
- shell()
+ options, args = getoptions()
+ connection = matahari.api.BrokerConnection(options.broker, options.port)
+
+ shell = matahari.shell.MatahariShell(connection, debug=options.debug)
+
+ shell(*args)
+
if __name__ == '__main__':
main()