Show More
@@ -715,7 +715,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):' | |||
|
715 | 715 | 'is not visible!') |
|
716 | 716 | |
|
717 | 717 | self._reading = True |
|
718 | self._show_prompt(prompt) | |
|
718 | self._show_prompt(prompt, newline=False) | |
|
719 | 719 | |
|
720 | 720 | if callback is None: |
|
721 | 721 | self._reading_callback = None |
@@ -739,18 +739,25 b' class ConsoleWidget(QtGui.QPlainTextEdit):' | |||
|
739 | 739 | """ |
|
740 | 740 | self.setTextCursor(self._get_selection_cursor(start, end)) |
|
741 | 741 | |
|
742 | def _show_prompt(self, prompt=None): | |
|
743 |
""" Writes a new prompt at the end of the buffer. |
|
|
744 | specified, the previous prompt is used. | |
|
745 | """ | |
|
746 | # Use QTextDocumentFragment intermediate object because it strips | |
|
747 | # out the Unicode line break characters that Qt insists on inserting. | |
|
748 | cursor = self._get_end_cursor() | |
|
749 | if cursor.position() > 0: | |
|
750 | cursor.movePosition(QtGui.QTextCursor.Left, | |
|
751 | QtGui.QTextCursor.KeepAnchor) | |
|
752 | if str(cursor.selection().toPlainText()) != '\n': | |
|
753 | self.appendPlainText('\n') | |
|
742 | def _show_prompt(self, prompt=None, newline=True): | |
|
743 | """ Writes a new prompt at the end of the buffer. | |
|
744 | ||
|
745 | Parameters | |
|
746 | ---------- | |
|
747 | prompt : str, optional | |
|
748 | The prompt to show. If not specified, the previous prompt is used. | |
|
749 | ||
|
750 | newline : bool, optional (default True) | |
|
751 | If set, a new line will be written before showing the prompt if | |
|
752 | there is not already a newline at the end of the buffer. | |
|
753 | """ | |
|
754 | if newline: | |
|
755 | cursor = self._get_end_cursor() | |
|
756 | if cursor.position() > 0: | |
|
757 | cursor.movePosition(QtGui.QTextCursor.Left, | |
|
758 | QtGui.QTextCursor.KeepAnchor) | |
|
759 | if str(cursor.selection().toPlainText()) != '\n': | |
|
760 | self.appendPlainText('\n') | |
|
754 | 761 | |
|
755 | 762 | if prompt is not None: |
|
756 | 763 | self._prompt = prompt |
@@ -143,12 +143,12 b' class FrontendWidget(HistoryConsoleWidget):' | |||
|
143 | 143 | # 'ConsoleWidget' protected interface |
|
144 | 144 | #--------------------------------------------------------------------------- |
|
145 | 145 | |
|
146 | def _show_prompt(self, prompt=None): | |
|
146 | def _show_prompt(self, prompt=None, newline=True): | |
|
147 | 147 | """ Reimplemented to set a default prompt. |
|
148 | 148 | """ |
|
149 | 149 | if prompt is None: |
|
150 | 150 | prompt = '>>> ' |
|
151 | super(FrontendWidget, self)._show_prompt(prompt) | |
|
151 | super(FrontendWidget, self)._show_prompt(prompt, newline) | |
|
152 | 152 | |
|
153 | 153 | #--------------------------------------------------------------------------- |
|
154 | 154 | # 'FrontendWidget' interface |
@@ -297,6 +297,10 b' class FrontendWidget(HistoryConsoleWidget):' | |||
|
297 | 297 | self._call_tip() |
|
298 | 298 | |
|
299 | 299 | def _handle_req(self, req): |
|
300 | # Make sure that all output from the SUB channel has been processed | |
|
301 | # before entering readline mode. | |
|
302 | self.kernel_manager.sub_channel.flush() | |
|
303 | ||
|
300 | 304 | def callback(line): |
|
301 | 305 | self.kernel_manager.rep_channel.readline(line) |
|
302 | 306 | self._readline(callback=callback) |
@@ -13,11 +13,11 b' Things to do:' | |||
|
13 | 13 | |
|
14 | 14 | # Standard library imports. |
|
15 | 15 | import __builtin__ |
|
16 | from code import CommandCompiler | |
|
16 | 17 | import os |
|
17 | 18 | import sys |
|
18 | 19 | import time |
|
19 | 20 | import traceback |
|
20 | from code import CommandCompiler | |
|
21 | 21 | |
|
22 | 22 | # System library imports. |
|
23 | 23 | import zmq |
@@ -40,7 +40,7 b' class InStream(object):' | |||
|
40 | 40 | |
|
41 | 41 | def flush(self): |
|
42 | 42 | if self.socket is None: |
|
43 |
raise ValueError( |
|
|
43 | raise ValueError('I/O operation on closed file') | |
|
44 | 44 | |
|
45 | 45 | def isatty(self): |
|
46 | 46 | return False |
@@ -49,19 +49,33 b' class InStream(object):' | |||
|
49 | 49 | raise IOError('Seek not supported.') |
|
50 | 50 | |
|
51 | 51 | def read(self, size=-1): |
|
52 | raise NotImplementedError | |
|
52 | # FIXME: Do we want another request for this? | |
|
53 | string = '\n'.join(self.readlines()) | |
|
54 | return self._truncate(string, size) | |
|
53 | 55 | |
|
54 | 56 | def readline(self, size=-1): |
|
55 | 57 | if self.socket is None: |
|
56 |
raise ValueError( |
|
|
58 | raise ValueError('I/O operation on closed file') | |
|
57 | 59 | else: |
|
58 | 60 | content = dict(size=size) |
|
59 | 61 | msg = self.session.msg('readline_request', content=content) |
|
60 | 62 | reply = self._request(msg) |
|
61 |
|
|
|
63 | line = reply['content']['line'] | |
|
64 | return self._truncate(line, size) | |
|
62 | 65 | |
|
63 | def readlines(self, size=-1): | |
|
64 | raise NotImplementedError | |
|
66 | def readlines(self, sizehint=-1): | |
|
67 | # Sizehint is ignored, as is permitted. | |
|
68 | if self.socket is None: | |
|
69 | raise ValueError('I/O operation on closed file') | |
|
70 | else: | |
|
71 | lines = [] | |
|
72 | while True: | |
|
73 | line = self.readline() | |
|
74 | if line: | |
|
75 | lines.append(line) | |
|
76 | else: | |
|
77 | break | |
|
78 | return lines | |
|
65 | 79 | |
|
66 | 80 | def seek(self, offset, whence=None): |
|
67 | 81 | raise IOError('Seek not supported.') |
@@ -73,6 +87,11 b' class InStream(object):' | |||
|
73 | 87 | raise IOError('Write not supported on a read only stream.') |
|
74 | 88 | |
|
75 | 89 | def _request(self, msg): |
|
90 | # Flush output before making the request. This ensures, for example, | |
|
91 | # that raw_input(prompt) actually gets a prompt written. | |
|
92 | sys.stderr.flush() | |
|
93 | sys.stdout.flush() | |
|
94 | ||
|
76 | 95 | self.socket.send_json(msg) |
|
77 | 96 | while True: |
|
78 | 97 | try: |
@@ -86,6 +105,15 b' class InStream(object):' | |||
|
86 | 105 | break |
|
87 | 106 | return reply |
|
88 | 107 | |
|
108 | def _truncate(self, string, size): | |
|
109 | if size >= 0: | |
|
110 | if isinstance(string, str): | |
|
111 | return string[:size] | |
|
112 | elif isinstance(string, unicode): | |
|
113 | encoded = string.encode('utf-8')[:size] | |
|
114 | return encoded.decode('utf-8', 'ignore') | |
|
115 | return string | |
|
116 | ||
|
89 | 117 | |
|
90 | 118 | class OutStream(object): |
|
91 | 119 | """A file like object that publishes the stream to a 0MQ PUB socket.""" |
General Comments 0
You need to be logged in to leave comments.
Login now