ó
æNXc           @  s¾  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d d d „ Z d „  Z e j d ƒ Z d d d „ Z d „  Z d d d d „ Z d d d d „ Z d d d „ Z  d d „ Z! d „  Z" e j# d k rŸe j$ j% d d ƒ d k rŸd d l& Z& d „  Z' n	 d „  Z' d d d  d! „ Z( d S("   s»   
Paging capabilities for IPython.core

Notes
-----

For now this uses IPython hooks, so it can't be in IPython.utils.  If we can get
rid of that dependency, we could move it there.
-----
iÿÿÿÿ(   t   print_functionN(   t   UnsupportedOperation(   t   get_ipython(   t   display(   t   TryNext(   t   chop(   t   io(   t   system(   t   get_terminal_size(   t	   py3compati    i   c         C  s[   t  |  t ƒ r |  } n/ | r: d j |  j ƒ  | ƒ }  n  i |  d 6} t | d t ƒd S(   s1   Just display, no paging. screen_lines is ignored.u   
s
   text/plaint   rawN(   t
   isinstancet   dictt   joint
   splitlinesR   t   True(   t   strngt   startt   screen_linest   data(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   display_page#   s    	c           s   ‡  f d †  S(   sV   Wrap a pager func to strip the `self` arg
    
    so it can be called as a hook.
    c           s   ˆ  | | Ž  S(   N(    (   t   selft   argst   kwargs(   t	   page_func(    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   <lambda>3   s    (    (   R   (    (   R   sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   as_hook.   s    s   (\x1b[^m]+m)c   	      C  s  |  j  ƒ  | } t | | d ƒ } t | ƒ d k r[ t t j j | d ƒ d t j ƒn© d } xy | d d !D]j } t j j | ƒ } t | | d t j ƒt	 ƒ  s« d St
 j | ƒ } t | ƒ d k ro | d } qo qo Wt | t j j | d ƒ d t j ƒd S(   s   Very dumb 'pager' in Python, for when nothing else works.

    Only moves forward, same interface as page(), except for pager_cmd and
    mode.i   i    t   filet    iÿÿÿÿN(   R   R   t   lent   printt   ost   linesepR   R   t   stdoutt	   page_moret   esc_ret   findall(	   R   R   R   t   out_lnt   screenst   last_escapet   scrt   hunkt   esc_list(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt	   page_dumb8   s    &	c   
      C  s[  t  j j d d ƒ } | d k s- | d k o9 t j d k s@ |  Sy d d l } d d l } Wn t k
 rp |  SXy | j	 t j
 ƒ } Wn+ | j k
 r´ } t d j | ƒ ƒ ‚ n Xt  j j d d ƒ } d	 t  j d <y | j ƒ  } Wn t k
 rû |  SX| j ƒ  \ } }	 | j ƒ  | d k r1t  j d =n | t  j d <| j t j
 | j | ƒ | S(
   sØ   Attempt to work out the number of lines on the screen.

    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.
    t   TERMt   xterms   xterm-colort   sunos5iÿÿÿÿNs   termios error: {0}t   NCURSES_NO_SETBUFR   (   R   t   environt   gett   Nonet   syst   platformt   termiost   cursest   ImportErrort	   tcgetattrR!   t   errort	   TypeErrort   formatt   initscrt   AttributeErrort   getmaxyxt   endwint	   tcsetattrt   TCSANOW(
   t   screen_lines_defR,   R5   R6   t
   term_flagst   errR/   R(   t   screen_lines_realt   screen_cols(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   _detect_screen_sizeN   s2    '	

c         C  sÖ  t  |  t ƒ r |  d }  n  t j j d d ƒ } | d k rZ t j d k rZ t |  ƒ d S|  j ƒ  | } t j j	 | ƒ } t
 | ƒ } t
 | ƒ } t | t | d ƒ d ƒ }	 t ƒ  d }
 | d	 k ry | t |
 ƒ 7} Wqt t f k
 rt | d
 t j ƒd SXn  |	 | k r1t | d
 t j ƒn¡t | ƒ } | d t | | ƒ 7} t j d k r| j d ƒ r{d } q³t j d ƒ \ } } zd t j | ƒ t | d ƒ $ } | j |  ƒ d | | f } Wd QXt j | ƒ ríd } n d } Wd t j | ƒ Xn« ya d } t j | d ƒ } z5 | j p8t  j j } | j t! j" |  d | ƒƒ Wd | j ƒ  } XWnG t# k
 rœ} | j$ d k r“d } q³d } n t% k
 r²d } n X| d k	 rÒt& |  d | ƒn  d S(   s  Display a string, piping through a pager after a certain length.
    
    strng can be a mime-bundle dict, supplying multiple representations,
    keyed by mime-type.

    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.
    s
   text/plainR,   t   dumbt   emacst   ntNiP   i   i    R   t    t   types   .txtt   wts   %s < %st   wt   encodingi    s   Broken pipeR   (   RH   RI   (   i    s   Broken pipe('   R   R   R   R0   R1   t   nameR   R   R    R   R   t   maxt   intR   RG   R:   R   R   R!   t   get_pager_cmdt   get_pager_startt
   startswitht   tempfilet   mkstempt   closet   opent   writeR   R2   t   removet   popenRO   R3   R	   t   cast_bytes_py2t   IOErrorR   t   OSErrorR+   (   R   R   R   t	   pager_cmdR,   t	   str_linest   str_toprintt   num_newlinest   len_strt   numlinesRB   t   retvalt   fdt   tmpnamet   tmpfilet   cmdt   pagert   pager_encodingt   msg(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt
   pager_page   sf    
		
		
c         C  sl   t  d | ƒ } t ƒ  } | rY y$ | j j |  d | d | ƒd SWqY t k
 rU qY Xn  t |  | | | ƒ S(   s9  Display content in a pager, piping through a pager after a certain length.
    
    data can be a mime-bundle dict, supplying multiple representations,
    keyed by mime-type, or text.
    
    Pager is dispatched via the `show_in_pager` IPython hook.
    If no hook is registered, `pager_page` will be used.
    i    R   R   N(   RQ   R   t   hookst   show_in_pagerR   Rn   (   R   R   R   R`   t   ip(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   pageú   s    	c         C  s¶   t  | ƒ } | d t | | ƒ 7} y2 t j d d	 k rB t ‚ n  t | d |  ƒ WnZ y6 | d k rw | d 8} n  t t |  ƒ j ƒ  | ƒ Wq² t	 d t
 |  ƒ ƒ q² Xn Xd S(
   sD   Page a file, using an optional pager command and starting line.
    RK   R,   RI   RH   i    i   s   Unable to show fileN(   s   emacss   dumb(   RS   RT   R   R0   t   EnvironmentErrorR   Rr   RY   t   readR   t   repr(   t   fnameR   R`   (    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt	   page_file  s    	c         C  sd   t  j d k r d } n t  j d k r0 d } n  |  d k r` y t  j d }  Wq` | }  q` Xn  |  S(	   sS   Return a pager command.

    Makes some attempts at finding an OS-correct one.
    t   posixs   less -rRJ   t   dosRL   t   PAGER(   s   nts   dosN(   R   RP   R2   R0   (   R`   t   default_pager_cmd(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyRS   (  s    		c         C  s8   |  d k r. | r% d t  | ƒ } q4 d } n d } | S(   s   Return the string for paging files with an offset.

    This is the '+N' argument which less and more (under Unix) accept.
    t   lesst   moret   +R   (   R|   s   more(   t   str(   Rk   R   t   start_string(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyRT   :  s    	RJ   R,   RH   RI   c          C  s_   t  j j d ƒ t j ƒ  }  |  d k r1 t } n t } t  j j d d d d d d ƒ | S(   sg    Smart pausing between pages

        @return:    True if need print more lines, False if quit
        s$   ---Return to continue, q to quit--- t   qt   Qs   i%   RK   (   R   R‚   (   R   R!   RZ   t   msvcrtt   getwcht   FalseR   (   t   anst   result(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyR"   M  s    	$c          C  s0   t  j d ƒ }  |  j ƒ  j d ƒ r( t St Sd  S(   Ns$   ---Return to continue, q to quit--- R   (   R	   t   inputt   lowerRU   R…   R   (   R†   (    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyR"   [  s    iK   R   c         C  sÍ   | d k r t  | |  ƒ d St | d d ƒt |  ƒ | k  rS t |  ƒ d } n5 t | d d ƒ } t |  |  d |  | ƒ d } | rÉ | d k rÉ t j | d ƒ j ƒ  d	 k rÉ t  |  ƒ qÉ n  | S(
   s$  Print a string snipping the midsection to fit in width.

    print_full: mode control:
    
      - 0: only snip long strings
      - 1: send to page() directly.
      - 2: snip long strings and ask for full length viewing with page()
    
    Return 1 if snipping was necessary, 0 otherwise.i   i    t   endRK   i   i   s    <...> s    Snipped. View (y/n)? [N]t   y(   Rr   R   R   RR   R	   Rˆ   R‰   (   R   t   widtht
   print_fullt   headert   snipt   whalf(    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt
   snip_printc  s    
	()   t   __doc__t
   __future__R    R   t   reR3   RV   R   R   t   IPythonR   t   IPython.core.displayR   t   IPython.core.errorR   t   IPython.utils.dataR   t   IPython.utilst   IPython.utils.processR   t   IPython.utils.terminalR   R	   R   R   t   compileR#   R+   RG   R2   Rn   Rr   Rw   RS   RT   RP   R0   R1   Rƒ   R"   R‘   (    (    (    sI   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/page.pyt   <module>   s:   		Ak	*	