;ς
ΞΡAHc           @   s   d  Z  d k l Z d e i d Z e i Z d k Z d k Z d k	 l
 Z
 d e i f d     YZ d f  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 $(   s   Releases   %s <%s>s   FernandoN(   s   StringIOs   OutputTrapErrorc           B   s   t  Z d  Z e d  Z RS(   s   Exception for OutputTrap class.c         C   s   t  i i |   | |  _ d  S(   N(   s
   exceptionss	   Exceptions   __init__s   selfs   args(   s   selfs   args(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   __init__   s    (   s   __name__s
   __module__s   __doc__s   Nones   __init__(    (    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   OutputTrapError   s    s
   OutputTrapc        
   B   sΆ   t  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   |  _ t |  _	 t |  _
 | |  _ | |  _ |	 |  _ | o |  i   n | o |  i   n d  S(   N(   s   names   selfs   out_heads   err_heads   sum_seps   StringIOs   outs   errs   Nones   out_saves   err_saves   debugs	   quiet_outs	   quiet_errs   trap_outs   trap_err(
   s   selfs   names   out_heads   err_heads   sum_seps   debugs   trap_outs   trap_errs	   quiet_outs	   quiet_err(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   __init__@   s    									c         C   sK   t  i |  i j o t d  n |  i o t  i |  _ |  i t  _ n d S(   s   Trap and log stdout.s    You are already trapping stdout.N(   s   syss   stdouts   selfs   outs   OutputTrapErrors   debugs	   _out_save(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   trap_outT   s     c         C   sI   |  i o: t i |  i j o t d  n |  i t _ t |  _ n d S(   s   Release stdout.s   You are not trapping stdout.N(	   s   selfs   debugs   syss   stdouts   outs   OutputTrapErrors	   _out_saves   Nones   out_save(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   release_out\   s     c         C   sN   |  i i   } | o0 |  i o | SqJ |  i d |  i d | Sn d Sd S(   s'   Return as a string the log from stdout.s   Log by s   :
s    N(   s   selfs   outs   getvalues	   quiet_outs   out_heads   name(   s   selfs   out(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   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(   s   selfs   outs   closes   StringIO(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys	   flush_outo   s     c         C   sK   t  i |  i j o t d  n |  i o t  i |  _ |  i t  _ n d S(   s   Trap and log stderr.s    You are already trapping stderr.N(   s   syss   stderrs   selfs   errs   OutputTrapErrors   debugs	   _err_save(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   trap_erru   s     c         C   sI   |  i o: t i |  i j o t d  n |  i t _ t |  _ n d S(   s   Release stderr.s   You are not trapping stderr.N(	   s   selfs   debugs   syss   stderrs   errs   OutputTrapErrors	   _err_saves   Nones   err_save(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   release_err}   s     c         C   sN   |  i i   } | o0 |  i o | SqJ |  i d |  i d | Sn d Sd S(   s'   Return as a string the log from stderr.s   Log by s   :
s    N(   s   selfs   errs   getvalues	   quiet_errs   err_heads   name(   s   selfs   err(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   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(   s   selfs   errs   closes   StringIO(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys	   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(   s   selfs   trap_outs   OutputTrapErrors   trap_err(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   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(   s   selfs   release_outs   OutputTrapErrors   release_err(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   release_all£   s     c         C   sb   d } |  i   } | o | |  i | 7} n |  i   } | o | d |  i | 7} n | Sd S(   s{   Return as a string the log from stdout and stderr, prepending a separator
        to each (defined in __init__ as sum_sep).s    s   
N(   s   sums   selfs   summary_outs   souts   sum_seps   summary_errs   serr(   s   selfs   serrs   sums   sout(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   summary_all°   s     c         C   s   |  i   |  i   d S(   s   Flush stdout and stderrN(   s   selfs	   flush_outs	   flush_err(   s   self(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys	   flush_allΌ   s     
(   s   __name__s
   __module__s   __doc__s   __init__s   trap_outs   release_outs   summary_outs	   flush_outs   trap_errs   release_errs   summary_errs	   flush_errs   trap_alls   release_alls   summary_alls	   flush_alls   traps   releases   summarys   flush(    (    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys
   OutputTrap   s$   " $												c          C   s   d   } d   }  |    d S(   s]   Module testing functions, activated when the module is called as a
    script (not imported).c          C   s0   d }  d d d |  GHd |  d d d GHd  S(   Ns    s   #i2   s   
Running test for s   Finished test for s   
(   s   name(   s   name(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   _test_Υ   s    c          C   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(   Ns   names	   Test Traps   sum_seps   .i2   s   
s   out_heads   SOut. s   err_heads   SErr. s   OutputTrap classs   #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 (   s
   OutputTraps   traps   names   trap_outs   syss   stdouts   stderrs   release_outs   summary_outs   trap_errs   release_errs   summary_errs   trap_alls   release_alls   summary_alls   flush(   s   traps   name(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   _test_OutputTrapΫ   s<    








N(   s   _test_s   _test_OutputTrap(   s   _test_OutputTraps   _test_(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys	   _test_allΜ   s     		%s   __main__(   s   __doc__s   IPythons   Releases   authorss
   __author__s   licenses   __license__s
   exceptionss   syss	   cStringIOs   StringIOs	   Exceptions   OutputTrapErrors
   OutputTraps	   _test_alls   __name__(	   s   __license__s   StringIOs   OutputTrapErrors
   __author__s   syss
   exceptionss
   OutputTraps   Releases	   _test_all(    (    sB   /nyx/web/d/b/dbachman/work/src/ipython-0.8.4/IPython/OutputTrap.pys   ?   s   				°	7