iostream.py
102 lines
| 3.2 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2754 | import sys | ||
import time | ||||
Thomas Kluyver
|
r4070 | from io import StringIO | ||
Brian Granger
|
r2754 | |||
from session import extract_header, Message | ||||
Brandon Parsons
|
r6655 | from IPython.utils import io, text, encoding | ||
Bradley M. Froehle
|
r7847 | from IPython.utils import py3compat | ||
Fernando Perez
|
r2938 | |||
Brian Granger
|
r2754 | #----------------------------------------------------------------------------- | ||
Fernando Perez
|
r3322 | # Globals | ||
Brian Granger
|
r2754 | #----------------------------------------------------------------------------- | ||
Fernando Perez
|
r3322 | |||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2754 | # Stream classes | ||
#----------------------------------------------------------------------------- | ||||
class OutStream(object): | ||||
"""A file like object that publishes the stream to a 0MQ PUB socket.""" | ||||
# The time interval between automatic flushes, in seconds. | ||||
flush_interval = 0.05 | ||||
MinRK
|
r3602 | topic=None | ||
Brian Granger
|
r2754 | def __init__(self, session, pub_socket, name): | ||
self.session = session | ||||
self.pub_socket = pub_socket | ||||
self.name = name | ||||
self.parent_header = {} | ||||
self._new_buffer() | ||||
Jason Grout
|
r7949 | self.metadata = {} | ||
Brian Granger
|
r2754 | |||
def set_parent(self, parent): | ||||
self.parent_header = extract_header(parent) | ||||
Jason Grout
|
r7949 | def set_metadata(self, metadata): | ||
self.flush() | ||||
self.metadata = metadata | ||||
Brian Granger
|
r2754 | def close(self): | ||
self.pub_socket = None | ||||
def flush(self): | ||||
Fernando Perez
|
r2938 | #io.rprint('>>>flushing output buffer: %s<<<' % self.name) # dbg | ||
Brian Granger
|
r2754 | if self.pub_socket is None: | ||
raise ValueError(u'I/O operation on closed file') | ||||
else: | ||||
data = self._buffer.getvalue() | ||||
if data: | ||||
content = {u'name':self.name, u'data':data} | ||||
Jason Grout
|
r7949 | if self.metadata: | ||
content['metadata'] = self.metadata | ||||
MinRK
|
r3602 | msg = self.session.send(self.pub_socket, u'stream', content=content, | ||
parent=self.parent_header, ident=self.topic) | ||||
MinRK
|
r4476 | if hasattr(self.pub_socket, 'flush'): | ||
# socket itself has flush (presumably ZMQStream) | ||||
self.pub_socket.flush() | ||||
Brian Granger
|
r2754 | self._buffer.close() | ||
self._new_buffer() | ||||
def isatty(self): | ||||
return False | ||||
Bradley M. Froehle
|
r7847 | def __next__(self): | ||
Brian Granger
|
r2754 | raise IOError('Read not supported on a write only stream.') | ||
Bradley M. Froehle
|
r7847 | if not py3compat.PY3: | ||
next = __next__ | ||||
Brian Granger
|
r2754 | def read(self, size=-1): | ||
raise IOError('Read not supported on a write only stream.') | ||||
def readline(self, size=-1): | ||||
raise IOError('Read not supported on a write only stream.') | ||||
def write(self, string): | ||||
if self.pub_socket is None: | ||||
raise ValueError('I/O operation on closed file') | ||||
else: | ||||
Thomas Kluyver
|
r4070 | # Make sure that we're handling unicode | ||
if not isinstance(string, unicode): | ||||
Brandon Parsons
|
r6717 | enc = encoding.DEFAULT_ENCODING | ||
Thomas Kluyver
|
r4070 | string = string.decode(enc, 'replace') | ||
Brian Granger
|
r2754 | self._buffer.write(string) | ||
current_time = time.time() | ||||
if self._start <= 0: | ||||
self._start = current_time | ||||
elif current_time - self._start > self.flush_interval: | ||||
self.flush() | ||||
def writelines(self, sequence): | ||||
if self.pub_socket is None: | ||||
raise ValueError('I/O operation on closed file') | ||||
else: | ||||
for string in sequence: | ||||
self.write(string) | ||||
def _new_buffer(self): | ||||
self._buffer = StringIO() | ||||
Fernando Perez
|
r2938 | self._start = -1 | ||