예제 #1
0
def _run_shell(handler, module=None):
    def _handler(shell, etype, evalue, traceback, tb_offset=None):
        print("Sorry, {0}".format(str(evalue)))
        return None

    try:
        from IPython.frontend.terminal.embed import InteractiveShellEmbed
        import quantities as q

        print("Welcome to Concert {0}".format(concert.__version__))

        if module:
            print(module.__doc__)

        globals().update(_get_module_variables(module))

        with handler.applicationbound():
            shell = InteractiveShellEmbed(banner1='')

            exceptions = (UnitError, LimitError, ParameterError,
                          ReadAccessError, WriteAccessError)
            shell.set_custom_exc(exceptions, _handler)
            shell()
    except ImportError as exception:
        msg = "You must install IPython to run the Concert shell: {0}"
        print(msg.format(exception))
예제 #2
0
def init_ipython():
    from IPython.config.loader import Config
    from IPython.frontend.terminal.embed import InteractiveShellEmbed

    try:
        get_ipython
    except NameError:
        nested = 0
        cfg = Config()
    else:
        print("Running nested copies of IPython.")
        print("The prompts for the nested copy have been modified")
        cfg = Config()
        nested = 1

    ipshell = InteractiveShellEmbed(
        config=cfg, banner1='Stopping IO Loop and dropping to ipython')

    class shell_wrapper(object):
        def __init__(self):
            self.user_wants_out = False

        def __call__(self):
            ipshell(
                'Ctrl-D, quit, exit all exit interpreter and continue program\n'
                'If you need to kill the program %kill',
                stack_depth=3)
            return self.user_wants_out

    _shell_wrapper = shell_wrapper()

    def kill_program(self, parameter_s=''):
        _shell_wrapper.user_wants_out = True
        ipshell.exit()

    def really_die(self, etype, value, tb, tb_offset=None):
        _shell_wrapper.user_wants_out = True
        return None

    ipshell.define_magic("kill", kill_program)
    ipshell.confirm_exit = False
    ipshell.set_custom_exc((SystemExit, ), really_die)

    return _shell_wrapper