##// END OF EJS Templates
Merge pull request #13712 from mkoeppe/patch-1...
Merge pull request #13712 from mkoeppe/patch-1 setup.cfg: Remove setuptools from install_requires

File last commit:

r27495:1a9d9554
r27733:bc543bc2 merge
Show More
page.py
348 lines | 11.5 KiB | text/x-python | PythonLexer
Brian Granger
Continuing a massive refactor of everything.
r2205 # encoding: utf-8
"""
Paging capabilities for IPython.core
Notes
-----
Min RK
add InteractiveShell.display_page config...
r19387 For now this uses IPython hooks, so it can't be in IPython.utils. If we can get
Brian Granger
Continuing a massive refactor of everything.
r2205 rid of that dependency, we could move it there.
-----
"""
Min RK
add InteractiveShell.display_page config...
r19387 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Continuing a massive refactor of everything.
r2205
import os
Matthias Bussonnier
Use proper subprocess commands instead of modifying the command line....
r25239 import io
Brian Granger
Continuing a massive refactor of everything.
r2205 import re
import sys
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import tempfile
Matthias Bussonnier
Use proper subprocess commands instead of modifying the command line....
r25239 import subprocess
Brian Granger
Continuing a massive refactor of everything.
r2205
Thomas Kluyver
Add simple smoketest for page._detect_screen_size.
r4923 from io import UnsupportedOperation
Inception95
Use Pathlib on page.py
r26023 from pathlib import Path
Thomas Kluyver
Add simple smoketest for page._detect_screen_size.
r4923
MinRK
don't use deprecated ipapi.get...
r10581 from IPython import get_ipython
Matthias Bussonnier
Do not import from IPython.core.display and warn users.
r25632 from IPython.display import display
Brian Granger
Continuing a massive refactor of everything.
r2205 from IPython.core.error import TryNext
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 from IPython.utils.data import chop
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 from IPython.utils.process import system
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 from IPython.utils.terminal import get_terminal_size
Michael Droettboom
Address comments in the PR.
r8966 from IPython.utils import py3compat
Brian Granger
Continuing a massive refactor of everything.
r2205
Min RK
add InteractiveShell.display_page config...
r19387 def display_page(strng, start=0, screen_lines=25):
"""Just display, no paging. screen_lines is ignored."""
if isinstance(strng, dict):
data = strng
else:
if start:
strng = u'\n'.join(strng.splitlines()[start:])
Sylvain Corlay
page_dumb mime bundle
r22463 data = { 'text/plain': strng }
Min RK
add InteractiveShell.display_page config...
r19387 display(data, raw=True)
def as_hook(page_func):
"""Wrap a pager func to strip the `self` arg
Matthias Bussonnier
some docstring reformatting and fixing
r27288
Min RK
add InteractiveShell.display_page config...
r19387 so it can be called as a hook.
"""
return lambda self, *args, **kwargs: page_func(*args, **kwargs)
Brian Granger
Continuing a massive refactor of everything.
r2205
esc_re = re.compile(r"(\x1b[^m]+m)")
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def page_dumb(strng, start=0, screen_lines=25):
Brian Granger
Continuing a massive refactor of everything.
r2205 """Very dumb 'pager' in Python, for when nothing else works.
Only moves forward, same interface as page(), except for pager_cmd and
Sylvain Corlay
page_dumb mime bundle
r22463 mode.
"""
if isinstance(strng, dict):
strng = strng.get('text/plain', '')
Brian Granger
Continuing a massive refactor of everything.
r2205 out_ln = strng.splitlines()[start:]
screens = chop(out_ln,screen_lines-1)
if len(screens) == 1:
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 print(os.linesep.join(screens[0]))
Brian Granger
Continuing a massive refactor of everything.
r2205 else:
last_escape = ""
for scr in screens[0:-1]:
hunk = os.linesep.join(scr)
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 print(last_escape + hunk)
Brian Granger
Continuing a massive refactor of everything.
r2205 if not page_more():
return
esc_list = esc_re.findall(hunk)
if len(esc_list) > 0:
last_escape = esc_list[-1]
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 print(last_escape + os.linesep.join(screens[-1]))
Brian Granger
Continuing a massive refactor of everything.
r2205
Thomas Kluyver
Don't load curses on startup
r9388 def _detect_screen_size(screen_lines_def):
Thomas Kluyver
Follow Fernando's suggestions.
r4920 """Attempt to work out the number of lines on the screen.
Michael Droettboom
Address comments in the PR.
r8966
Thomas Kluyver
Follow Fernando's suggestions.
r4920 This is called by page(). It can raise an error (e.g. when run in the
test suite), so it's separated out so it can easily be called in a try block.
"""
Thomas Kluyver
Fix for paging docstrings.
r4921 TERM = os.environ.get('TERM',None)
Thomas Kluyver
Don't load curses on startup
r9388 if not((TERM=='xterm' or TERM=='xterm-color') and sys.platform != 'sunos5'):
Thomas Kluyver
Make page function more robust.
r4897 # curses causes problems on many terminals other than xterm, and
# some termios calls lock up on Sun OS5.
Thomas Kluyver
Don't load curses on startup
r9388 return screen_lines_def
try:
Thomas Kluyver
Make page function more robust.
r4897 import termios
import curses
Thomas Kluyver
Don't load curses on startup
r9388 except ImportError:
return screen_lines_def
# There is a bug in curses, where *sometimes* it fails to properly
# initialize, and then after the endwin() call is made, the
# terminal is left in an unusable state. Rather than trying to
Min ho Kim
Fix typos
r25146 # check every time for this (by requesting and comparing termios
Thomas Kluyver
Don't load curses on startup
r9388 # flags each time), we just save the initial terminal state and
# unconditionally reset it every time. It's cheaper than making
# the checks.
Volker Braun
Catch termios.error in pager...
r21030 try:
term_flags = termios.tcgetattr(sys.stdout)
except termios.error as err:
Volker Braun
Added comments and an exception message
r21033 # can fail on Linux 2.6, pager_page will catch the TypeError
Ram Rachum
Fix exception causes all over the codebase
r25833 raise TypeError('termios error: {0}'.format(err)) from err
Thomas Kluyver
Don't load curses on startup
r9388
try:
Thomas Kluyver
Make page function more robust.
r4897 scr = curses.initscr()
Thomas Kluyver
Don't load curses on startup
r9388 except AttributeError:
# Curses on Solaris may not be complete, so we can't use it there
Thomas Kluyver
Make page function more robust.
r4897 return screen_lines_def
Thomas Kluyver
Don't load curses on startup
r9388
screen_lines_real,screen_cols = scr.getmaxyx()
curses.endwin()
# Restore terminal state in case endwin() didn't.
termios.tcsetattr(sys.stdout,termios.TCSANOW,term_flags)
# Now we have what we needed: the screen size in rows/columns
return screen_lines_real
#print '***Screen size:',screen_lines_real,'lines x',\
#screen_cols,'columns.' # dbg
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Min RK
add InteractiveShell.display_page config...
r19387 def pager_page(strng, start=0, screen_lines=0, pager_cmd=None):
MinRK
updates per review...
r16665 """Display a string, piping through a pager after a certain length.
Matthias Bussonnier
some docstring reformatting and fixing
r27288
MinRK
updates per review...
r16665 strng can be a mime-bundle dict, supplying multiple representations,
keyed by mime-type.
Brian Granger
Continuing a massive refactor of everything.
r2205
The screen_lines parameter specifies the number of *usable* lines of your
terminal screen (total lines minus lines you need to reserve to show other
information).
If you set screen_lines to a number <=0, page() will try to auto-determine
your screen size and will only use up to (screen_size+screen_lines) for
printing, paging after that. That is, if you want auto-detection but need
to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
auto-detection without any lines reserved simply use screen_lines = 0.
If a string won't fit in the allowed lines, it is sent through the
specified pager command. If none given, look for PAGER in the environment,
and ultimately default to less.
If no system pager works, the string is sent through a 'dumb pager'
written in python, very simplistic.
"""
MinRK
pager payload is a mime-bundle
r16586
# for compatibility with mime-bundle form:
if isinstance(strng, dict):
strng = strng['text/plain']
Brian Granger
Continuing a massive refactor of everything.
r2205
# Ugly kludge, but calling curses.initscr() flat out crashes in emacs
TERM = os.environ.get('TERM','dumb')
if TERM in ['dumb','emacs'] and os.name != 'nt':
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print(strng)
Brian Granger
Continuing a massive refactor of everything.
r2205 return
# chop off the topmost part of the string we don't want to see
MinRK
fix too-restrictive use of split(os.linesep) in %page...
r3739 str_lines = strng.splitlines()[start:]
Brian Granger
Continuing a massive refactor of everything.
r2205 str_toprint = os.linesep.join(str_lines)
num_newlines = len(str_lines)
len_str = len(str_toprint)
# Dumb heuristics to guesstimate number of on-screen lines the string
# takes. Very basic, but good enough for docstrings in reasonable
# terminals. If someone later feels like refining it, it's not hard.
numlines = max(num_newlines,int(len_str/80)+1)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 screen_lines_def = get_terminal_size()[1]
Brian Granger
Continuing a massive refactor of everything.
r2205
# auto-determine screen size
if screen_lines <= 0:
Thomas Kluyver
Make page function more robust.
r4897 try:
Thomas Kluyver
Don't load curses on startup
r9388 screen_lines += _detect_screen_size(screen_lines_def)
Thomas Kluyver
Add simple smoketest for page._detect_screen_size.
r4923 except (TypeError, UnsupportedOperation):
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 print(str_toprint)
Thomas Kluyver
Make page function more robust.
r4897 return
Brian Granger
Continuing a massive refactor of everything.
r2205
#print 'numlines',numlines,'screenlines',screen_lines # dbg
if numlines <= screen_lines :
#print '*** normal print' # dbg
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 print(str_toprint)
Brian Granger
Continuing a massive refactor of everything.
r2205 else:
# Try to open pager and default to internal one if that fails.
# All failure modes are tagged as 'retval=1', to match the return
# value of a failed system command. If any intermediate attempt
# sets retval to 1, at the end we resort to our own page_dumb() pager.
pager_cmd = get_pager_cmd(pager_cmd)
pager_cmd += ' ' + get_pager_start(pager_cmd,start)
if os.name == 'nt':
if pager_cmd.startswith('type'):
# The default WinXP 'type' command is failing on complex strings.
retval = 1
else:
Julian Taylor
remove mktemp usage...
r15372 fd, tmpname = tempfile.mkstemp('.txt')
Inception95
Use pathlib in page.py
r26024 tmppath = Path(tmpname)
Julian Taylor
remove mktemp usage...
r15372 try:
os.close(fd)
gousaiyang
Format code
r27495 with tmppath.open("wt", encoding="utf-8") as tmpfile:
Julian Taylor
remove mktemp usage...
r15372 tmpfile.write(strng)
Inception95
Use pathlib in page.py
r26024 cmd = "%s < %s" % (pager_cmd, tmppath)
Julian Taylor
remove mktemp usage...
r15372 # tmpfile needs to be closed for windows
if os.system(cmd):
retval = 1
else:
retval = None
finally:
Inception95
Use pathlib in page.py
r26024 Path.unlink(tmppath)
Brian Granger
Continuing a massive refactor of everything.
r2205 else:
try:
retval = None
Matthias Bussonnier
Use proper subprocess commands instead of modifying the command line....
r25239 # Emulate os.popen, but redirect stderr
gousaiyang
Format code
r27495 proc = subprocess.Popen(
pager_cmd,
shell=True,
stdin=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
pager = os._wrap_close(
io.TextIOWrapper(proc.stdin, encoding="utf-8"), proc
)
Michael Droettboom
Address comments in the PR.
r8966 try:
pager_encoding = pager.encoding or sys.stdout.encoding
Srinivas Reddy Thatiparthy
rm cast_bytes_py2
r23666 pager.write(strng)
Michael Droettboom
Address comments in the PR.
r8966 finally:
retval = pager.close()
Matthias BUSSONNIER
conform to pep 3110...
r7787 except IOError as msg: # broken pipe when user quits
Michael Droettboom
Address comments in the PR.
r8966 if msg.args == (32, 'Broken pipe'):
Brian Granger
Continuing a massive refactor of everything.
r2205 retval = None
else:
retval = 1
except OSError:
# Other strange problems, sometimes seen in Win2k/cygwin
retval = 1
if retval is not None:
page_dumb(strng,screen_lines=screen_lines)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Min RK
add InteractiveShell.display_page config...
r19387 def page(data, start=0, screen_lines=0, pager_cmd=None):
"""Display content in a pager, piping through a pager after a certain length.
Matthias Bussonnier
some docstring reformatting and fixing
r27288
Min RK
add InteractiveShell.display_page config...
r19387 data can be a mime-bundle dict, supplying multiple representations,
keyed by mime-type, or text.
Matthias Bussonnier
some docstring reformatting and fixing
r27288
Min RK
add InteractiveShell.display_page config...
r19387 Pager is dispatched via the `show_in_pager` IPython hook.
If no hook is registered, `pager_page` will be used.
"""
# Some routines may auto-compute start offsets incorrectly and pass a
# negative value. Offset to 0 for robustness.
start = max(0, start)
# first, try the hook
ip = get_ipython()
if ip:
try:
ip.hooks.show_in_pager(data, start=start, screen_lines=screen_lines)
return
except TryNext:
pass
# fallback on default pager
return pager_page(data, start, screen_lines, pager_cmd)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def page_file(fname, start=0, pager_cmd=None):
Brian Granger
Continuing a massive refactor of everything.
r2205 """Page a file, using an optional pager command and starting line.
"""
pager_cmd = get_pager_cmd(pager_cmd)
pager_cmd += ' ' + get_pager_start(pager_cmd,start)
try:
if os.environ['TERM'] in ['emacs','dumb']:
raise EnvironmentError
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 system(pager_cmd + ' ' + fname)
Brian Granger
Continuing a massive refactor of everything.
r2205 except:
try:
if start > 0:
start -= 1
gousaiyang
Format code
r27495 page(open(fname, encoding="utf-8").read(), start)
Brian Granger
Continuing a massive refactor of everything.
r2205 except:
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print('Unable to show file',repr(fname))
Brian Granger
Continuing a massive refactor of everything.
r2205
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def get_pager_cmd(pager_cmd=None):
"""Return a pager command.
Brian Granger
Continuing a massive refactor of everything.
r2205
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 Makes some attempts at finding an OS-correct one.
"""
Brian Granger
Continuing a massive refactor of everything.
r2205 if os.name == 'posix':
Pierre Gerold
Thomas fix
r22263 default_pager_cmd = 'less -R' # -R for color control sequences
Brian Granger
Continuing a massive refactor of everything.
r2205 elif os.name in ['nt','dos']:
default_pager_cmd = 'type'
if pager_cmd is None:
try:
pager_cmd = os.environ['PAGER']
except:
pager_cmd = default_pager_cmd
Dmitry Zotikov
Ensure pager interprets escape sequences...
r22100
Pierre Gerold
Check '-r' or '-R' and add '-R' if both absent
r22272 if pager_cmd == 'less' and '-r' not in os.environ.get('LESS', '').lower():
Pierre Gerold
Fix #9406
r22262 pager_cmd += ' -R'
Dmitry Zotikov
Ensure pager interprets escape sequences...
r22100
Brian Granger
Continuing a massive refactor of everything.
r2205 return pager_cmd
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def get_pager_start(pager, start):
Brian Granger
Continuing a massive refactor of everything.
r2205 """Return the string for paging files with an offset.
This is the '+N' argument which less and more (under Unix) accept.
"""
if pager in ['less','more']:
if start:
start_string = '+' + str(start)
else:
start_string = ''
else:
start_string = ''
return start_string
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
# (X)emacs on win32 doesn't like to be bypassed with msvcrt.getch()
Brian Granger
Continuing a massive refactor of everything.
r2205 if os.name == 'nt' and os.environ.get('TERM','dumb') != 'emacs':
import msvcrt
def page_more():
""" Smart pausing between pages
@return: True if need print more lines, False if quit
"""
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 sys.stdout.write('---Return to continue, q to quit--- ')
Thomas Kluyver
Use msvcrt.getwch() for Windows pager....
r12260 ans = msvcrt.getwch()
Brian Granger
Continuing a massive refactor of everything.
r2205 if ans in ("q", "Q"):
result = False
else:
result = True
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 sys.stdout.write("\b"*37 + " "*37 + "\b"*37)
Brian Granger
Continuing a massive refactor of everything.
r2205 return result
else:
def page_more():
Thomas Kluyver
Fix references to raw_input()
r13355 ans = py3compat.input('---Return to continue, q to quit--- ')
Brian Granger
Continuing a massive refactor of everything.
r2205 if ans.lower().startswith('q'):
return False
else:
return True