##// END OF EJS Templates
Merge pull request #8305 from minrk/rm-notebook...
Merge pull request #8305 from minrk/rm-notebook remove notebook

File last commit:

r21113:348b2930
r21252:64649235 merge
Show More
io.py
246 lines | 7.6 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""
IO related utilities.
"""
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 from __future__ import print_function
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 from __future__ import absolute_import
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 import codecs
Thomas Kluyver
Implement atomic save...
r17557 from contextlib import contextmanager
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 import io
Brandon Parsons
moved getdefaultencoding from text to py3compat
r6653 import os
Thomas Kluyver
Copy file metadata in atomic save...
r17831 import shutil
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import sys
import tempfile
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111 import warnings
MinRK
capture rich output as well as stdout/err in capture_output...
r12223 from .capture import CapturedIO, capture_output
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 from .py3compat import string_types, input, PY3
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
class IOStream:
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 def __init__(self,stream, fallback=None):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if not hasattr(stream,'write') or not hasattr(stream,'flush'):
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 if fallback is not None:
stream = fallback
else:
raise ValueError("fallback required, but not specified")
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 self.stream = stream
self._swrite = stream.write
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 # clone all methods not overridden:
def clone(meth):
return not hasattr(self, meth) and not meth.startswith('_')
for meth in filter(clone, dir(stream)):
setattr(self, meth, getattr(stream, meth))
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Mechanism for testing terminal interact loop in-process....
r13528 def __repr__(self):
cls = self.__class__
tpl = '{mod}.{cls}({args})'
return tpl.format(mod=cls.__module__, cls=cls.__name__, args=self.stream)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def write(self,data):
try:
self._swrite(data)
except:
try:
# print handles some unicode issues which may trip a plain
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 # write() call. Emulate write() by using an empty end
# argument.
print(data, end='', file=self.stream)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 except:
# if we get here, something is seriously broken.
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 print('ERROR - failed to write data to stream:', self.stream,
file=sys.stderr)
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 def writelines(self, lines):
Thomas Kluyver
Replace references to unicode and basestring
r13353 if isinstance(lines, string_types):
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 lines = [lines]
for line in lines:
self.write(line)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Brian Granger
Fixed bug in ultratb.py and io.py....
r2504 # This class used to have a writeln method, but regular files and streams
# in Python don't have this method. We need to keep this completely
# compatible so we removed it.
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 @property
def closed(self):
return self.stream.closed
Bernardo B. Marques
remove all trailling spaces
r4872
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def close(self):
pass
Brandon Parsons
ensure a fallback exists, so use local std{in,out,err}...
r6652 # setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
Doug Blank
Minimal changes to import IPython from IronPython
r15154 devnull = open(os.devnull, 'w')
Brandon Parsons
ensure a fallback exists, so use local std{in,out,err}...
r6652 stdin = IOStream(sys.stdin, fallback=devnull)
stdout = IOStream(sys.stdout, fallback=devnull)
stderr = IOStream(sys.stderr, fallback=devnull)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
class IOTerm:
""" Term holds the file or file-like objects for handling I/O operations.
These are normally just sys.stdin, sys.stdout and sys.stderr but for
Windows they can can replaced to allow editing the strings before they are
displayed."""
# In the future, having IPython channel all its I/O operations through
# this class will make it easier to embed it into other environments which
# are not a normal terminal (such as a GUI-based shell)
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 def __init__(self, stdin=None, stdout=None, stderr=None):
Brandon Parsons
ensure a fallback exists, so use local std{in,out,err}...
r6652 mymodule = sys.modules[__name__]
self.stdin = IOStream(stdin, mymodule.stdin)
self.stdout = IOStream(stdout, mymodule.stdout)
self.stderr = IOStream(stderr, mymodule.stderr)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
class Tee(object):
"""A class to duplicate an output stream to stdout/err.
This works in a manner very similar to the Unix 'tee' command.
When the object is closed or deleted, it closes the original file given to
it for duplication.
"""
# Inspired by:
# http://mail.python.org/pipermail/python-list/2007-May/442737.html
Thomas Kluyver
Fixes to utils.io.Tee
r4763 def __init__(self, file_or_name, mode="w", channel='stdout'):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Construct a new Tee object.
Parameters
----------
file_or_name : filename or open filehandle (writable)
File that will be duplicated
mode : optional, valid mode for open().
If a filename was give, open with this mode.
Bernardo B. Marques
remove all trailling spaces
r4872 channel : str, one of ['stdout', 'stderr']
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """
if channel not in ['stdout', 'stderr']:
raise ValueError('Invalid channel spec %s' % channel)
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Fixes to utils.io.Tee
r4763 if hasattr(file_or_name, 'write') and hasattr(file_or_name, 'seek'):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 self.file = file_or_name
else:
self.file = open(file_or_name, mode)
self.channel = channel
self.ostream = getattr(sys, channel)
setattr(sys, channel, self)
self._closed = False
def close(self):
"""Close the file and restore the channel."""
self.flush()
setattr(sys, self.channel, self.ostream)
self.file.close()
self._closed = True
def write(self, data):
"""Write data to both channels."""
self.file.write(data)
self.ostream.write(data)
self.ostream.flush()
def flush(self):
"""Flush both channels."""
self.file.flush()
self.ostream.flush()
def __del__(self):
if not self._closed:
self.close()
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 def ask_yes_no(prompt, default=None, interrupt=None):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 """Asks a question and returns a boolean (y/n) answer.
If default is given (one of 'y','n'), it is used if the user input is
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 empty. If interrupt is given (one of 'y','n'), it is used if the user
presses Ctrl-C. Otherwise the question is repeated until an answer is
given.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
An EOF is treated as the default answer. If there is no default, an
exception is raised to prevent infinite loops.
Valid answers are: y/yes/n/no (match is not case sensitive)."""
answers = {'y':True,'n':False,'yes':True,'no':False}
ans = None
while ans not in answers.keys():
try:
Thomas Kluyver
Fix references to raw_input()
r13355 ans = input(prompt+' ').lower()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if not ans: # response was an empty string
ans = default
except KeyboardInterrupt:
Paul Ivanov
custom keyboard interrupt handling in ask_yes_no...
r13609 if interrupt:
ans = interrupt
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 except EOFError:
if default in answers.keys():
ans = default
Thomas Spura
Fix bare print calls that were missing '()'....
r4430 print()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 else:
raise
return answers[ans]
def temp_pyfile(src, ext='.py'):
"""Make a temporary python file, return filename and filehandle.
Parameters
----------
src : string or list of strings (no need for ending newlines if list)
Source code to be written to the file.
ext : optional, string
Extension for the generated file.
Returns
-------
(filename, open filehandle)
It is the caller's responsibility to close the open file and unlink it.
"""
fname = tempfile.mkstemp(ext)[1]
f = open(fname,'w')
f.write(src)
f.flush()
return fname, f
Min RK
move utils.io.atomic_writing to contents.fileio
r21113 def atomic_writing(*args, **kwargs):
"""DEPRECATED: moved to IPython.html.services.contents.fileio"""
warn("IPython.utils.io.atomic_writing has moved to IPython.html.services.contents.fileio")
from IPython.html.services.contents.fileio import atomic_writing
return atomic_writing(*args, **kwargs)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Fernando Perez
Provide cleaner names for raw print utilities.
r2874 def raw_print(*args, **kw):
"""Raw print to sys.__stdout__, otherwise identical interface to print()."""
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
file=sys.__stdout__)
sys.__stdout__.flush()
Fernando Perez
Provide cleaner names for raw print utilities.
r2874 def raw_print_err(*args, **kw):
"""Raw print to sys.__stderr__, otherwise identical interface to print()."""
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improve io.rprint* interface, unify usage in ipkernel....
r2856 print(*args, sep=kw.get('sep', ' '), end=kw.get('end', '\n'),
file=sys.__stderr__)
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 sys.__stderr__.flush()
Fernando Perez
Provide cleaner names for raw print utilities.
r2874
# Short aliases for quick debugging, do NOT use these in production code.
rprint = raw_print
rprinte = raw_print_err
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690
Min RK
move io.unicode_std_stream to nbconvert.utils.io
r21111 def unicode_std_stream(stream='stdout'):
"""DEPRECATED, moved to jupyter_nbconvert.utils.io"""
warn("IPython.utils.io.unicode_std_stream has moved to jupyter_nbconvert.utils.io")
from jupyter_nbconvert.utils.io import unicode_std_stream
return unicode_std_stream(stream)