Ñò
ÎÑAHc           @   s¢   d  Z  d d k l Z d e i d Z e i Z d d k Z d d k Z d d k	 l
 Z
 d e i f d „  ƒ  YZ d	 d d
 „  ƒ  YZ d „  Z e d j o n d S(   sn   Class to trap stdout and stderr and log them separately.

$Id: OutputTrap.py 958 2005-12-27 23:17:51Z fperez $iÿÿÿÿ(   t   Releases   %s <%s>t   FernandoN(   t   StringIOt   OutputTrapErrorc           B   s   e  Z d  Z d d „ Z RS(   s   Exception for OutputTrap class.c         C   s   t  i i |  ƒ | |  _ d  S(   N(   t
   exceptionst	   Exceptiont   __init__t   args(   t   selfR   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR      s    N(   t   __name__t
   __module__t   __doc__t   NoneR   (    (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR      s   t
   OutputTrapc        
   B   s¶   e  Z d  Z d d d d d d d d d d „	 Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z e Z e Z e Z e Z RS(   sR  Class to trap standard output and standard error. They get logged in
    StringIO objects which are available as <instance>.out and
    <instance>.err. The class also offers summary methods which format this
    data a bit.

    A word of caution: because it blocks messages, using this class can make
    debugging very tricky. If you are having bizarre problems silently, try
    turning your output traps off for a while. You can call the constructor
    with the parameter debug=1 for these cases. This turns actual trapping
    off, but you can keep the rest of your code unchanged (this has already
    been a life saver).

    Example:

    # config: trapper with a line of dots as log separator (final '\n' needed)
    config = OutputTrap('Config','Out ','Err ','.'*80+'\n')

    # start trapping output
    config.trap_all()

    # now all output is logged ...
    # do stuff...

    # output back to normal:
    config.release_all()

    # print all that got logged:
    print config.summary()

    # print individual raw data:
    print config.out.getvalue()
    print config.err.getvalue()
    s   Generic Output Traps   Standard Output. s   Standard Error. s   
i    c
   
      C   s—   | |  _  | |  _ | |  _ | |  _ t ƒ  |  _ t ƒ  |  _ d  |  _ d  |  _	 | |  _
 | |  _ |	 |  _ | o |  i ƒ  n | o |  i ƒ  n d  S(   N(   t   namet   out_headt   err_headt   sum_sepR   t   outt   errR   t   out_savet   err_savet   debugt	   quiet_outt	   quiet_errt   trap_outt   trap_err(
   R   R   R   R   R   R   R   R   R   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR   @   s    									c         C   sJ   t  i |  i j o t d ‚ n |  i p t  i |  _ |  i t  _ n d S(   s   Trap and log stdout.s    You are already trapping stdout.N(   t   syst   stdoutR   R   R   t	   _out_save(   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR   T   s
    
c         C   sG   |  i  p9 t i |  i j	 o t d ‚ n |  i t _ d |  _ n d S(   s   Release stdout.s   You are not trapping stdout.N(   R   R   R   R   R   R   R   R   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   release_out\   s
    
c         C   sK   |  i  i ƒ  } | o- |  i o | S|  i d |  i d | Sn d Sd S(   s'   Return as a string the log from stdout.s   Log by s   :
t    N(   R   t   getvalueR   R   R   (   R   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   summary_outd   s    
c         C   s   |  i  i ƒ  t ƒ  |  _  d S(   s7   Flush the stdout log. All data held in the log is lost.N(   R   t   closeR   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt	   flush_outo   s    c         C   sJ   t  i |  i j o t d ‚ n |  i p t  i |  _ |  i t  _ n d S(   s   Trap and log stderr.s    You are already trapping stderr.N(   R   t   stderrR   R   R   t	   _err_save(   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR   u   s
    
c         C   sG   |  i  p9 t i |  i j	 o t d ‚ n |  i t _ d |  _ n d S(   s   Release stderr.s   You are not trapping stderr.N(   R   R   R$   R   R   R%   R   R   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   release_err}   s
    
c         C   sK   |  i  i ƒ  } | o- |  i o | S|  i d |  i d | Sn d Sd S(   s'   Return as a string the log from stderr.s   Log by s   :
R   N(   R   R    R   R   R   (   R   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   summary_err…   s    
c         C   s   |  i  i ƒ  t ƒ  |  _  d S(   s7   Flush the stdout log. All data held in the log is lost.N(   R   R"   R   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt	   flush_err   s    c         C   sL   y |  i  ƒ  Wn t j
 o n Xy |  i ƒ  Wn t j
 o n Xd S(   se   Trap and log both stdout and stderr.

        Cacthes and discards OutputTrapError exceptions raised.N(   R   R   R   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   trap_all–   s    c         C   sL   y |  i  ƒ  Wn t j
 o n Xy |  i ƒ  Wn t j
 o n Xd S(   s`   Release both stdout and stderr.

        Cacthes and discards OutputTrapError exceptions raised.N(   R   R   R&   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   release_all£   s    c         C   s^   d } |  i  ƒ  } | o | |  i | 7} n |  i ƒ  } | o | d |  i | 7} n | S(   s{   Return as a string the log from stdout and stderr, prepending a separator
        to each (defined in __init__ as sum_sep).R   s   
(   R!   R   R'   (   R   t   sumt   soutt   serr(    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   summary_all°   s    c         C   s   |  i  ƒ  |  i ƒ  d S(   s   Flush stdout and stderrN(   R#   R(   (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt	   flush_all¼   s    
(   R	   R
   R   R   R   R   R!   R#   R   R&   R'   R(   R)   R*   R.   R/   t   trapt   releaset   summaryt   flush(    (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyR      s*   "												c          C   s   d „  }  d „  } | ƒ  d S(   s]   Module testing functions, activated when the module is called as a
    script (not imported).c          S   s0   d }  d d d |  GHd |  d d d GHd  S(   NR   t   #i2   s   
Running test for s   Finished test for s   
(    (   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   _test_Õ   s    c       	   S   s[  t  d d d d d d d d d	 d
 ƒ }  d } d d d | GHd GH|  i ƒ  t i d IJt i d IJ|  i ƒ  |  i ƒ  GHd GH|  i ƒ  t i d IJt i d IJ|  i ƒ  |  i	 ƒ  GHd GH|  i
 ƒ  t i d IJt i d IJ|  i ƒ  |  i ƒ  GHd GH|  i ƒ  |  i
 ƒ  t i d IJt i d IJ|  i ƒ  |  i ƒ  GHd | d d d GHd  S(   NR   s	   Test TrapR   t   .i2   s   
R   s   SOut. R   s   SErr. s   OutputTrap classR4   s   
Running test for s   Trapping outs   >>stdout. stdout is trapped.s   >>stderr. stdout is trapped.s   Trapping errs   >>stdout. stderr is trapped.s   >>stderr. stderr is trapped.s   Trapping all (no flushing)s    >>stdout. stdout/err is trapped.s    >>stderr. stdout/err is trapped.s   Trapping all (flushing first)s   Finished test for (   R   R   R   R   R$   R   R!   R   R&   R'   R)   R*   R.   R3   (   R0   R   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   _test_OutputTrapÛ   s<    








N(    (   R5   R7   (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt	   _test_allÌ   s    			%t   __main__(    (   R   t   IPythonR    t   authorst
   __author__t   licenset   __license__R   R   t	   cStringIOR   R   R   R   R8   R	   (    (    (    sJ   /u/d/b/dbachman/web/work/lib/python2.3/site-packages/IPython/OutputTrap.pyt   <module>   s   		°	7