Show More
@@ -1,93 +1,88 b'' | |||
|
1 | import logging | |
|
2 | 1 |
|
|
3 | 2 | import time |
|
4 | 3 | from io import StringIO |
|
5 | 4 | |
|
6 | 5 | from session import extract_header, Message |
|
7 | 6 | |
|
8 | 7 | from IPython.utils import io |
|
9 | 8 | |
|
10 | 9 | #----------------------------------------------------------------------------- |
|
11 | 10 | # Globals |
|
12 | 11 | #----------------------------------------------------------------------------- |
|
13 | 12 | |
|
14 | # Module-level logger | |
|
15 | logger = logging.getLogger(__name__) | |
|
16 | ||
|
17 | 13 | #----------------------------------------------------------------------------- |
|
18 | 14 | # Stream classes |
|
19 | 15 | #----------------------------------------------------------------------------- |
|
20 | 16 | |
|
21 | 17 | class OutStream(object): |
|
22 | 18 | """A file like object that publishes the stream to a 0MQ PUB socket.""" |
|
23 | 19 | |
|
24 | 20 | # The time interval between automatic flushes, in seconds. |
|
25 | 21 | flush_interval = 0.05 |
|
26 | 22 | topic=None |
|
27 | 23 | |
|
28 | 24 | def __init__(self, session, pub_socket, name): |
|
29 | 25 | self.session = session |
|
30 | 26 | self.pub_socket = pub_socket |
|
31 | 27 | self.name = name |
|
32 | 28 | self.parent_header = {} |
|
33 | 29 | self._new_buffer() |
|
34 | 30 | |
|
35 | 31 | def set_parent(self, parent): |
|
36 | 32 | self.parent_header = extract_header(parent) |
|
37 | 33 | |
|
38 | 34 | def close(self): |
|
39 | 35 | self.pub_socket = None |
|
40 | 36 | |
|
41 | 37 | def flush(self): |
|
42 | 38 | #io.rprint('>>>flushing output buffer: %s<<<' % self.name) # dbg |
|
43 | 39 | if self.pub_socket is None: |
|
44 | 40 | raise ValueError(u'I/O operation on closed file') |
|
45 | 41 | else: |
|
46 | 42 | data = self._buffer.getvalue() |
|
47 | 43 | if data: |
|
48 | 44 | content = {u'name':self.name, u'data':data} |
|
49 | 45 | msg = self.session.send(self.pub_socket, u'stream', content=content, |
|
50 | 46 | parent=self.parent_header, ident=self.topic) |
|
51 | logger.debug(msg) | |
|
52 | 47 | |
|
53 | 48 | self._buffer.close() |
|
54 | 49 | self._new_buffer() |
|
55 | 50 | |
|
56 | 51 | def isatty(self): |
|
57 | 52 | return False |
|
58 | 53 | |
|
59 | 54 | def next(self): |
|
60 | 55 | raise IOError('Read not supported on a write only stream.') |
|
61 | 56 | |
|
62 | 57 | def read(self, size=-1): |
|
63 | 58 | raise IOError('Read not supported on a write only stream.') |
|
64 | 59 | |
|
65 | 60 | def readline(self, size=-1): |
|
66 | 61 | raise IOError('Read not supported on a write only stream.') |
|
67 | 62 | |
|
68 | 63 | def write(self, string): |
|
69 | 64 | if self.pub_socket is None: |
|
70 | 65 | raise ValueError('I/O operation on closed file') |
|
71 | 66 | else: |
|
72 | 67 | # Make sure that we're handling unicode |
|
73 | 68 | if not isinstance(string, unicode): |
|
74 | 69 | enc = sys.stdin.encoding or sys.getdefaultencoding() |
|
75 | 70 | string = string.decode(enc, 'replace') |
|
76 | 71 | |
|
77 | 72 | self._buffer.write(string) |
|
78 | 73 | current_time = time.time() |
|
79 | 74 | if self._start <= 0: |
|
80 | 75 | self._start = current_time |
|
81 | 76 | elif current_time - self._start > self.flush_interval: |
|
82 | 77 | self.flush() |
|
83 | 78 | |
|
84 | 79 | def writelines(self, sequence): |
|
85 | 80 | if self.pub_socket is None: |
|
86 | 81 | raise ValueError('I/O operation on closed file') |
|
87 | 82 | else: |
|
88 | 83 | for string in sequence: |
|
89 | 84 | self.write(string) |
|
90 | 85 | |
|
91 | 86 | def _new_buffer(self): |
|
92 | 87 | self._buffer = StringIO() |
|
93 | 88 | self._start = -1 |
General Comments 0
You need to be logged in to leave comments.
Login now