#! /usr/bin/env mpscript # # This script starts an interactive Python shell with the MX database # already set up. # import code import os try: import atexit except: pass if ( os.name == "posix" ): try: import readline # Derived from the example in the Python documentation. class ReadlineConsole(code.InteractiveConsole): def __init__( self, locals=None, filename="", histfile=os.path.expanduser("~/.mx/mpshell.history") ): code.InteractiveConsole.__init__( self, locals, filename ) self.init_history( histfile ) def init_history( self, histfile ): readline.parse_and_bind( "tab: complete" ) if hasattr( readline, "read_history_file" ): try: readline.read_history_file( histfile ) except IOError: pass try: atexit.register( self.save_history, histfile ) except: pass def save_history( self, histfile ): try: readline.write_history_file( histfile ) except IOError: dir = os.path.expanduser("~/.mx") os.mkdir( dir ) readline.write_history_file( histfile ) console_class = ReadlineConsole except: # Readline is not present, so use the plain vanilla console instead. console_class = code.InteractiveConsole else: console_class = code.InteractiveConsole def main( mx_database, argv ): namespace = globals().copy() namespace.update( locals() ) s = console_class( namespace ) s.interact( "(MX Python Interactive Console)" )