
J9Qc           @   s   d  Z  d d l Z d d l m Z d d l Z d d l m Z d d l m Z m Z m	 Z	 y d d l
 m Z Wn! e k
 r d d l m Z n Xe j e  Z d e f d     YZ d	 e f d
     YZ d e f d     YZ d S(   s  
This plugin captures logging statements issued during test execution. When an
error or failure occurs, the captured log messages are attached to the running
test in the test.capturedLogging attribute, and displayed with the error failure
output. It is enabled by default but can be turned off with the option
``--nologcapture``.

You can filter captured logging statements with the ``--logging-filter`` option. 
If set, it specifies which logger(s) will be captured; loggers that do not match
will be passed. Example: specifying ``--logging-filter=sqlalchemy,myapp``
will ensure that only statements logged via sqlalchemy.engine, myapp
or myapp.foo.bar logger will be logged.

You can remove other installed logging handlers with the
``--logging-clear-handlers`` option.
iN(   t   Handler(   t   Plugin(   t   anypt   lnt   safe_str(   t   StringIOt	   FilterSetc           B   sV   e  Z d    Z d   Z e e  Z d   Z d   Z e e  Z d   Z d   Z RS(   c         C   s   |  j  |  \ |  _ |  _ d  S(   N(   t
   _partitiont	   inclusivet	   exclusive(   t   selft   filter_components(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   __init__!   s    c         C   sX   g  g  } } x> |  D]6 } | j  d  r= | j | d  q | j |  q W| | f S(   Nt   -i   (   t
   startswitht   append(   t
   componentsR   R	   t	   component(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR   %   s    c         C   s'   |  s
 t  S|  j |  o& |  j |  S(   s-   returns whether this record should be printed(   t   Truet   _allowt   _deny(   R
   t   record(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   allow/   s    c            s%     f d   } t  t t | |    S(   sN   return the bool of whether `record` starts with
        any item in `matchers`c            s     |  k p   j  |  d  S(   Nt   .(   R   (   t   key(   R   (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   record_matches_key:   s    (   R   t   boolt   map(   t   matchersR   R   (    (   R   s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt
   _any_match7   s    c         C   s    |  j  s t S|  j |  j  |  S(   N(   R   R   R   (   R
   R   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR   ?   s    	c         C   s    |  j  s t S|  j |  j  |  S(   N(   R	   t   FalseR   (   R
   R   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR   D   s    	(	   t   __name__t
   __module__R   R   t   staticmethodR   R   R   R   (    (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR       s   					t   MyMemoryHandlerc           B   sG   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   sH   t  j |   t j | |  } |  j |  t |  |  _ g  |  _ d  S(   N(   R    R   t   loggingt	   Formattert   setFormatterR   t	   filtersett   buffer(   R
   t	   logformatt
   logdatefmtt   filterst   fmt(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR   K   s
    c         C   s   |  j  j |  j |   d  S(   N(   R'   R   t   format(   R
   R   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   emitQ   s    c         C   s   d  S(   N(    (   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   flushS   s    c         C   s   g  |  _  d  S(   N(   R'   (   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   truncateU   s    c         C   s   |  j  j | j  S(   N(   R&   R   t   name(   R
   R   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   filterW   s    c         C   s   |  j  j   } | d =| S(   Nt   lock(   t   __dict__t   copy(   R
   t   state(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   __getstate__Y   s    c         C   s#   |  j  j |  t j   |  _ d  S(   N(   R3   t   updatet	   threadingt   RLockR2   (   R
   R5   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   __setstate__]   s    (	   R   R    R   R-   R.   R/   R1   R6   R:   (    (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR"   J   s   						t
   LogCapturec           B   s   e  Z d  Z e Z d Z d Z d Z d Z d Z
 e Z d g 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 RS(   s  
    Log capture plugin. Enabled by default. Disable with --nologcapture.
    This plugin captures logging statements issued during test execution,
    appending any output captured to the error or failure output,
    should the test fail or raise an error.
    t   NOSE_NOLOGCAPTUREt
   logcapturei  s$   %(name)s: %(levelname)s: %(message)ss   -nosec         C   s.  | j  d d d d | j |  j  d d d d | j  d	 d d
 d d d | j d  p_ |  j d d d d | j  d d d
 d d d | j d  p |  j d d d d | j  d d d
 d d d | j d  d d d d | j  d d d d t d d d d | j  d d d
 d d d d d d  d! S("   s&   Register commandline options.
        s   --nologcapturet   actiont   store_falset   defaultt   destR=   t   helps]   Disable logging capture plugin. Logging configurtion will be left intact. [NOSE_NOLOGCAPTURE]s   --logging-formatt   storet   logcapture_formatt   NOSE_LOGFORMATt   metavart   FORMATsv   Specify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT]s   --logging-datefmtt   logcapture_datefmtt   NOSE_LOGDATEFMTs   Specify custom date/time format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]s   --logging-filtert   logcapture_filterst   NOSE_LOGFILTERt   FILTERs  Specify which statements to filter in/out. By default, everything is captured. If the output is too verbose,
use this option to filter out needless output.
Example: filter=foo will capture statements issued ONLY to
 foo or foo.what.ever.sub but not foobar or other logger.
Specify multiple loggers with comma: filter=foo,bar,baz.
If any logger name is prefixed with a minus, eg filter=-foo,
it will be excluded rather than included. Default: exclude logging messages from nose itself (-nose). [NOSE_LOGFILTER]
s   --logging-clear-handlerst
   store_truet   logcapture_clears    Clear all other logging handlerss   --logging-levelt   NOTSETt   logcapture_levels   Set the log level to captureN(   t
   add_optiont   gett   env_optR(   R)   R   (   R
   t   parsert   env(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   optionsr   s6    
c         C   s}   | |  _  | j s | j r( t |  _ n  | j |  _ | j |  _ | j	 |  _
 | j |  _ | j ry | j j d  |  _ n  d S(   s   Configure plugin.
        t   ,N(   t   confR=   t   loggingConfigR   t   enabledRD   R(   RH   R)   RN   t   clearRP   t   loglevelRJ   t   splitR*   (   R
   RV   RX   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt	   configure   s    		c         C   s  t  j   } |  j r t | d  rH x! | j D] } | j |  q. Wn  xS t  j j j j	   D]9 } t | d  r^ x! | j D] } | j |  q} Wq^ q^ Wn  x4 | j D]( } t
 | t  r | j j |  q q W| j |  j  t |  d d  } | j t t  |   d  S(   Nt   handlersR\   RO   (   R#   t	   getLoggerR[   t   hasattrR_   t   removeHandlert   Loggert   managert
   loggerDictt   valuest
   isinstanceR"   t   removet
   addHandlert   handlert   getattrt   setLevel(   R
   t   root_loggerRj   t   loggerR\   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   setupLoghandler   s    	c         C   s   |  j    d S(   s7   Set up logging handler before test run begins.
        N(   t   start(   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   begin   s    c         C   s,   t  |  j |  j |  j  |  _ |  j   d  S(   N(   R"   R(   R)   R*   Rj   Ro   (   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyRp      s    c         C   s   d  S(   N(    (   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   end   s    c         C   s   |  j    d S(   s0   Clear buffers and handlers before test.
        N(   Ro   (   R
   t   test(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt
   beforeTest   s    c         C   s   |  j  j   d S(   s"   Clear buffers after test.
        N(   Rj   R/   (   R
   Rs   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt	   afterTest   s    c         C   s   |  j  | |  S(   s5   Add captured log messages to failure output.
        (   t   formatError(   R
   Rs   t   err(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   formatFailure   s    c         C   sE   |  j    | _ } | s | S| \ } } } | |  j | |  | f S(   s3   Add captured log messages to error output.
        (   t   formatLogRecordst   capturedLoggingt   addCaptureToErr(   R
   Rs   Rw   t   recordst   ect   evt   tb(    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyRv      s
    c         C   s   t  t |  j j  S(   N(   R   R   Rj   R'   (   R
   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyRy      s    c         C   s0   d j  t |  t d  g | t d  g  S(   Ns   
s   >> begin captured logging <<s   >> end captured logging <<(   t   joinR   R   (   R
   R~   R|   (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR{      s    N(   R   R    t   __doc__R   RZ   RS   R0   t   scoreR(   t   NoneR)   R   R[   R*   RV   R^   Ro   Rq   Rp   Rr   Rt   Ru   Rx   Rv   Ry   R{   (    (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyR;   b   s*   		.									
	(   R   R#   R    R8   t   nose.plugins.baseR   t	   nose.utilR   R   R   t	   cStringIOR   t   ImportErrorR`   R   t   logt   objectR   R"   R;   (    (    (    s;   /sys/lib/python2.7/site-packages/nose/plugins/logcapture.pyt   <module>   s   *