コード例 #1
0
def execute(code):
    isp = IPythonInputSplitter()

    def do_execute():
        #print("CELL", cell )
        result = kernel.shell.run_cell(cell, False)
        if result.error_before_exec is not None:
            err = result.error_before_exec
        else:
            err = result.error_in_exec
        if not result.success:
            for tb in kernel.shell._last_traceback:
                print(tb)
            return False
        return True

    for line in code.splitlines():
        if isp.push_accepts_more():
            isp.push(line.strip("\n"))
            continue
        cell = isp.source_reset()
        if not do_execute():
            return False
    cell = isp.source_reset()
    if len(cell):
        return do_execute()
    else:
        return True
コード例 #2
0
ファイル: ipython.py プロジェクト: xeTaiz/seamless
def execute(code, namespace):
    isp = IPythonInputSplitter()
    kernel_manager = MyInProcessKernelManager()
    kernel_manager.start_kernel(namespace)
    kernel = kernel_manager.kernel

    def do_execute():
        result = kernel.shell.run_cell(cell, False)
        if result.error_before_exec is not None:
            err = result.error_before_exec
        else:
            err = result.error_in_exec
        if not result.success:
            if kernel.shell._last_traceback:
                for tb in kernel.shell._last_traceback:
                    print(tb) #TODO: log

    for line in code.splitlines():
        if isp.push_accepts_more():
            isp.push(line.strip("\n"))
            continue
        cell = isp.source_reset()
        do_execute()
        isp.push(line.strip("\n"))
    cell = isp.source_reset()
    if len(cell):
        do_execute()
    return namespace
コード例 #3
0
class PyDevFrontEnd:

    def __init__(self, *args, **kwargs):        
        # Initialization based on: from IPython.testing.globalipapp import start_ipython
        
        self._curr_exec_line = 0
        # Store certain global objects that IPython modifies
        _displayhook = sys.displayhook
        _excepthook = sys.excepthook
    
        # Create and initialize our IPython instance.
        shell = TerminalInteractiveShell.instance()
        # Create an intput splitter to handle input separation
        self.input_splitter = IPythonInputSplitter()

        shell.showtraceback = _showtraceback
        # IPython is ready, now clean up some global state...
        
        # Deactivate the various python system hooks added by ipython for
        # interactive convenience so we don't confuse the doctest system
        sys.displayhook = _displayhook
        sys.excepthook = _excepthook
    
        # So that ipython magics and aliases can be doctested (they work by making
        # a call into a global _ip object).  Also make the top-level get_ipython
        # now return this without recursively calling here again.
        get_ipython = shell.get_ipython
        try:
            import __builtin__
        except:
            import builtins as __builtin__
        __builtin__._ip = shell
        __builtin__.get_ipython = get_ipython
        
        # We want to print to stdout/err as usual.
        io.stdout = original_stdout
        io.stderr = original_stderr

        self.ipython = shell

    def complete(self, string):
        return self.ipython.complete(None, line=string)

    def is_complete(self, string):
        return  not self.input_splitter.push_accepts_more()

    def getNamespace(self):
        return self.ipython.user_ns

    def addExec(self, line):
        self.input_splitter.push(line)
        if self.is_complete(line):
            self.ipython.run_cell(self.input_splitter.source_reset())
            return False
        else:
            return True
コード例 #4
0
class PyDevFrontEnd:
    def __init__(self, *args, **kwargs):
        # Initialization based on: from IPython.testing.globalipapp import start_ipython

        self._curr_exec_line = 0
        # Store certain global objects that IPython modifies
        _displayhook = sys.displayhook
        _excepthook = sys.excepthook

        # Create and initialize our IPython instance.
        shell = TerminalInteractiveShell.instance()
        # Create an intput splitter to handle input separation
        self.input_splitter = IPythonInputSplitter()

        shell.showtraceback = _showtraceback
        # IPython is ready, now clean up some global state...

        # Deactivate the various python system hooks added by ipython for
        # interactive convenience so we don't confuse the doctest system
        sys.displayhook = _displayhook
        sys.excepthook = _excepthook

        # So that ipython magics and aliases can be doctested (they work by making
        # a call into a global _ip object).  Also make the top-level get_ipython
        # now return this without recursively calling here again.
        get_ipython = shell.get_ipython
        try:
            import __builtin__
        except:
            import builtins as __builtin__
        __builtin__._ip = shell
        __builtin__.get_ipython = get_ipython

        # We want to print to stdout/err as usual.
        io.stdout = original_stdout
        io.stderr = original_stderr

        self.ipython = shell

    def complete(self, string):
        return self.ipython.complete(None, line=string)

    def is_complete(self, string):
        return not self.input_splitter.push_accepts_more()

    def getNamespace(self):
        return self.ipython.user_ns

    def addExec(self, line):
        self.input_splitter.push(line)
        if self.is_complete(line):
            self.ipython.run_cell(self.input_splitter.source_reset())
            return False
        else:
            return True
コード例 #5
0
def ipython2python(code):
    isp = IPythonInputSplitter()
    newcode = ""
    for line in code.splitlines():
        if isp.push_accepts_more():
            isp.push(line.strip("\n"))
            continue
        cell = isp.source_reset()
        if cell.startswith("get_ipython().run_"):
            cell = "_ = " + cell
        newcode += cell + "\n"
        isp.push(line.strip("\n"))
    cell = isp.source_reset()
    if len(cell):
        if cell.startswith("get_ipython().run_"):
            cell = "_ = " + cell
        newcode += cell
    return newcode
コード例 #6
0
class PyDevFrontEnd:

    def __init__(self, pydev_host, pydev_client_port, *args, **kwarg):

        # Create and initialize our IPython instance.
        self.ipython = PyDevTerminalInteractiveShell.instance()

        # Back channel to PyDev to open editors (in the future other
        # info may go back this way. This is the same channel that is
        # used to get stdin, see StdIn in pydev_console_utils)
        self.ipython.set_hook('editor', create_editor_hook(pydev_host, pydev_client_port))

        # Create an input splitter to handle input separation
        self.input_splitter = IPythonInputSplitter()

        # Display the IPython banner, this has version info and
        # help info
        self.ipython.show_banner()

    def complete(self, string):
        return self.ipython.complete(None, line=string)

    def getCompletions(self, text, act_tok):
        # Get completions from IPython and from PyDev and merge the results
        # IPython only gives context free list of completions, while PyDev
        # gives detailed information about completions.
        try:
            TYPE_IPYTHON = '11'
            TYPE_IPYTHON_MAGIC = '12'
            _line, ipython_completions = self.complete(text)

            from _pydev_completer import Completer
            completer = Completer(self.getNamespace(), None)
            ret = completer.complete(act_tok)
            append = ret.append
            ip = self.ipython
            pydev_completions = set([f[0] for f in ret])
            for ipython_completion in ipython_completions:
                if ipython_completion not in pydev_completions:
                    pydev_completions.add(ipython_completion)
                    inf = ip.object_inspect(ipython_completion)
                    if inf['type_name'] == 'Magic function':
                        pydev_type = TYPE_IPYTHON_MAGIC
                    else:
                        pydev_type = TYPE_IPYTHON
                    pydev_doc = inf['docstring']
                    if pydev_doc is None:
                        pydev_doc = ''
                    append((ipython_completion, pydev_doc, '', pydev_type))
            return ret
        except:
            import traceback;traceback.print_exc()
            return []


    def getNamespace(self):
        return self.ipython.user_ns

    def addExec(self, line):
        self.input_splitter.push(line)
        if not self.input_splitter.push_accepts_more():
            self.ipython.run_cell(self.input_splitter.source_reset(), store_history=True)
            return False
        else:
            return True
コード例 #7
0
class PyDevFrontEnd:
    def __init__(self, pydev_host, pydev_client_port, *args, **kwarg):

        # Create and initialize our IPython instance.
        self.ipython = PyDevTerminalInteractiveShell.instance()

        # Back channel to PyDev to open editors (in the future other
        # info may go back this way. This is the same channel that is
        # used to get stdin, see StdIn in pydev_console_utils)
        self.ipython.set_hook(
            'editor', create_editor_hook(pydev_host, pydev_client_port))

        # Create an input splitter to handle input separation
        self.input_splitter = IPythonInputSplitter()

        # Display the IPython banner, this has version info and
        # help info
        self.ipython.show_banner()

    def complete(self, string):
        return self.ipython.complete(None, line=string)

    def getCompletions(self, text, act_tok):
        # Get completions from IPython and from PyDev and merge the results
        # IPython only gives context free list of completions, while PyDev
        # gives detailed information about completions.
        try:
            TYPE_IPYTHON = '11'
            TYPE_IPYTHON_MAGIC = '12'
            _line, ipython_completions = self.complete(text)

            from _pydev_completer import Completer
            completer = Completer(self.getNamespace(), None)
            ret = completer.complete(act_tok)
            append = ret.append
            ip = self.ipython
            pydev_completions = set([f[0] for f in ret])
            for ipython_completion in ipython_completions:
                if ipython_completion not in pydev_completions:
                    pydev_completions.add(ipython_completion)
                    inf = ip.object_inspect(ipython_completion)
                    if inf['type_name'] == 'Magic function':
                        pydev_type = TYPE_IPYTHON_MAGIC
                    else:
                        pydev_type = TYPE_IPYTHON
                    pydev_doc = inf['docstring']
                    if pydev_doc is None:
                        pydev_doc = ''
                    append((ipython_completion, pydev_doc, '', pydev_type))
            return ret
        except:
            import traceback
            traceback.print_exc()
            return []

    def getNamespace(self):
        return self.ipython.user_ns

    def addExec(self, line):
        self.input_splitter.push(line)
        if not self.input_splitter.push_accepts_more():
            self.ipython.run_cell(self.input_splitter.source_reset(),
                                  store_history=True)
            return False
        else:
            return True
コード例 #8
0
class PyDevFrontEnd:

    def __init__(self, pydev_host, pydev_client_port, exec_queue, *args, **kwarg):
        self.exec_queue = exec_queue
        self._curr_exec_line = 0
        # Store certain global objects that IPython modifies
        _displayhook = sys.displayhook
        _excepthook = sys.excepthook

        # Create and initialize our IPython instance.
        self.ipython = PyDevTerminalInteractiveShell.instance()

        # Back channel to PyDev to open editors (in the future other
        # info may go back this way. This is the same channel that is
        # used to get stdin, see StdIn in pydev_console_utils)
        self.ipython.set_hook('editor', create_editor_hook(pydev_host, pydev_client_port))

        # Create an input splitter to handle input separation
        self.input_splitter = IPythonInputSplitter()

        # Display the IPython banner, this has version info and
        # help info
        self.ipython.show_banner()

        # IPython is ready, now clean up some global state...

        # Deactivate the various python system hooks added by ipython for
        # interactive convenience so we don't confuse the doctest system
        sys.displayhook = _displayhook
        sys.excepthook = _excepthook

        # So that ipython magics and aliases can be doctested (they work by making
        # a call into a global _ip object).  Also make the top-level get_ipython
        # now return this without recursively calling here again.
        try:
            import __builtin__
        except:
            import builtins as __builtin__
        __builtin__._ip = self.ipython
        __builtin__.get_ipython = self.ipython.get_ipython

        # We want to print to stdout/err as usual.
        io.stdout = original_stdout
        io.stderr = original_stderr

    def complete(self, string):
        return self.ipython.complete(None, line=string)

    def getCompletions(self, text, act_tok):
        # Get completions from IPython and from PyDev and merge the results
        # IPython only gives context free list of completions, while PyDev
        # gives detailed information about completions.
        try:
            TYPE_IPYTHON = '11'
            TYPE_IPYTHON_MAGIC = '12'
            _line, ipython_completions = self.complete(text)

            from _pydev_completer import Completer
            completer = Completer(self.getNamespace(), None)
            ret = completer.complete(act_tok)
            append = ret.append
            ip = self.ipython
            pydev_completions = set([f[0] for f in ret])
            for ipython_completion in ipython_completions:
                if ipython_completion not in pydev_completions:
                    pydev_completions.add(ipython_completion)
                    inf = ip.object_inspect(ipython_completion)
                    if inf['type_name'] == 'Magic function':
                        pydev_type = TYPE_IPYTHON_MAGIC
                    else:
                        pydev_type = TYPE_IPYTHON
                    pydev_doc = inf['docstring']
                    if pydev_doc is None:
                        pydev_doc = ''
                    append((ipython_completion, pydev_doc, '', pydev_type))
            return ret
        except:
            import traceback;traceback.print_exc()
            return []

    def interrupt(self):
        self.input_splitter.reset()

    def getNamespace(self):
        return self.ipython.user_ns

    def addExec(self, line):
        self.input_splitter.push(line)
        if not self.input_splitter.push_accepts_more():
            self.exec_queue.put(partial(self.ipython.run_cell, self.input_splitter.source_reset(), store_history=True))
            return False
        else:
            return True