|
| 1 | +import sys |
| 2 | + |
| 3 | +from IPython.core.interactiveshell import InteractiveShell |
| 4 | +from IPython.core.shellapp import InteractiveShellApp |
| 5 | +from IPython.core.application import BaseIPythonApplication |
| 6 | +from IPython.core import page, payloadpage |
| 7 | +from IPython.core.completer import provisionalcompleter, rectify_completions |
| 8 | + |
| 9 | +from .compiler import XCachingCompiler |
| 10 | +from .display import XDisplayPublisher, XDisplayHook |
| 11 | + |
| 12 | + |
| 13 | +class XKernel(): |
| 14 | + def __init__(self): |
| 15 | + self.comm_manager = CommManager() |
| 16 | + |
| 17 | + def get_parent(self): |
| 18 | + return get_parent_header() |
| 19 | + |
| 20 | + @property |
| 21 | + def _parent_header(self): |
| 22 | + return self.get_parent() |
| 23 | + |
| 24 | + |
| 25 | +class XPythonShell(InteractiveShell): |
| 26 | + def __init__(self, *args, **kwargs): |
| 27 | + super(XPythonShell, self).__init__(*args, **kwargs) |
| 28 | + |
| 29 | + self.kernel = XKernel() |
| 30 | + |
| 31 | + def enable_gui(self, gui=None): |
| 32 | + """Not implemented yet.""" |
| 33 | + pass |
| 34 | + |
| 35 | + def init_hooks(self): |
| 36 | + super(XPythonShell, self).init_hooks() |
| 37 | + self.set_hook('show_in_pager', page.as_hook(payloadpage.page), 99) |
| 38 | + |
| 39 | + # Workaround for preventing IPython to show error traceback |
| 40 | + # in the console, we catch it and will display it later |
| 41 | + def _showtraceback(self, etype, evalue, stb): |
| 42 | + self.last_error = [str(etype), str(evalue), stb] |
| 43 | + |
| 44 | + def complete_code(self, code, cursor_pos): |
| 45 | + with provisionalcompleter(): |
| 46 | + raw_completions = self.Completer.completions(code, cursor_pos) |
| 47 | + completions = list(rectify_completions(code, raw_completions)) |
| 48 | + |
| 49 | + comps = [] |
| 50 | + for comp in completions: |
| 51 | + comps.append(dict( |
| 52 | + start=comp.start, |
| 53 | + end=comp.end, |
| 54 | + text=comp.text, |
| 55 | + type=comp.type, |
| 56 | + )) |
| 57 | + |
| 58 | + if completions: |
| 59 | + cursor_start = completions[0].start |
| 60 | + cursor_end = completions[0].end |
| 61 | + matches = [c.text for c in completions] |
| 62 | + else: |
| 63 | + cursor_start = cursor_pos |
| 64 | + cursor_end = cursor_pos |
| 65 | + matches = [] |
| 66 | + |
| 67 | + return matches, cursor_start, cursor_end |
| 68 | + |
| 69 | + |
| 70 | +class XPythonShellApp(BaseIPythonApplication, InteractiveShellApp): |
| 71 | + def initialize(self, argv=None): |
| 72 | + super(XPythonShellApp, self).initialize(argv) |
| 73 | + |
| 74 | + self.user_ns = {} |
| 75 | + |
| 76 | + # self.init_io() ? |
| 77 | + |
| 78 | + self.init_path() |
| 79 | + self.init_shell() |
| 80 | + |
| 81 | + self.init_extensions() |
| 82 | + self.init_code() |
| 83 | + |
| 84 | + sys.stdout.flush() |
| 85 | + sys.stderr.flush() |
| 86 | + |
| 87 | + def init_shell(self): |
| 88 | + self.shell = XPythonShell.instance( |
| 89 | + display_pub_class=XDisplayPublisher, |
| 90 | + displayhook_class=XDisplayHook, |
| 91 | + compiler_class=XCachingCompiler, |
| 92 | + user_ns=self.user_ns |
| 93 | + ) |
| 94 | + |
| 95 | + # Overwrite exit logic, this is not part of the kernel protocol |
| 96 | + def exit(self, exit_status=0): |
| 97 | + pass |
0 commit comments