B
    ؐ<[$                 @   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 W n  ek
rp   ddl
mZ Y nX eeZG dd deZG d	d
 d
eZG dd deZdS )a  
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.
    N)Handler)Plugin)anyplnsafe_str)StringIOc               @   sL   e Zd Zdd Zdd ZeeZdd Zdd ZeeZd	d
 Zdd Z	dS )	FilterSetc             C   s   |  |\| _| _d S )N)
_partition	inclusive	exclusive)selfZfilter_components r   6lib/python3.7/site-packages/nose/plugins/logcapture.py__init__!   s    zFilterSet.__init__c             C   sH   g g  }}x4| D ],}| dr2||dd   q|| qW ||fS )N-   )
startswithappend)Z
componentsr
   r   Z	componentr   r   r   r	   %   s    


zFilterSet._partitionc             C   s   | sdS |  |o| | S )z-returns whether this record should be printedT)_allow_deny)r   recordr   r   r   allow/   s    zFilterSet.allowc                s     fdd}t ttt|| S )zNreturn the bool of whether `record` starts with
        any item in `matchers`c                s    | kp  | d S )N.)r   )key)r   r   r   record_matches_key:   s    z0FilterSet._any_match.<locals>.record_matches_key)r   boollistmap)Zmatchersr   r   r   )r   r   
_any_match7   s    zFilterSet._any_matchc             C   s   | j s
dS | | j |S )NT)r
   r   )r   r   r   r   r   r   ?   s    zFilterSet._allowc             C   s   | j s
dS | | j |S )NF)r   r   )r   r   r   r   r   r   D   s    zFilterSet._denyN)
__name__
__module____qualname__r   r	   staticmethodr   r   r   r   r   r   r   r   r       s   r   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )MyMemoryHandlerc             C   s4   t |  t||}| | t|| _g | _d S )N)r   r   loggingZ	FormatterZsetFormatterr   	filtersetbuffer)r   	logformat
logdatefmtfiltersZfmtr   r   r   r   K   s
    


zMyMemoryHandler.__init__c             C   s   | j | | d S )N)r&   r   format)r   r   r   r   r   emitQ   s    zMyMemoryHandler.emitc             C   s   d S )Nr   )r   r   r   r   flushS   s    zMyMemoryHandler.flushc             C   s
   g | _ d S )N)r&   )r   r   r   r   truncateU   s    zMyMemoryHandler.truncatec             C   s   | j |jrt| |S d S )N)r%   r   namer   filter)r   r   r   r   r   r/   W   s    zMyMemoryHandler.filterc             C   s   | j  }|d= |S )Nlock)__dict__copy)r   stater   r   r   __getstate__Z   s    
zMyMemoryHandler.__getstate__c             C   s   | j | t | _d S )N)r1   update	threadingRLockr0   )r   r3   r   r   r   __setstate__^   s    zMyMemoryHandler.__setstate__N)
r   r    r!   r   r+   r,   r-   r/   r4   r8   r   r   r   r   r#   J   s   r#   c               @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	gZd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )"
LogCapturea  
    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.
    TZNOSE_NOLOGCAPTURE
logcapturei  z$%(name)s: %(levelname)s: %(message)sNFz-nosec             C   s   |j dd|| j ddd |j ddd|d	p6| jd
dd |j ddd|dpX| jd
dd |j ddd|dddd |j dddddd |j dddddd dS ) z&Register commandline options.
        z--nologcaptureZstore_falser:   z^Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE])actiondefaultdesthelpz--logging-formatZstorelogcapture_formatZNOSE_LOGFORMATZFORMATzvSpecify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT])r;   r=   r<   metavarr>   z--logging-datefmtlogcapture_datefmtZNOSE_LOGDATEFMTzSpecify custom date/time format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]z--logging-filterlogcapture_filtersZNOSE_LOGFILTERZFILTERa  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]
z--logging-clear-handlers
store_trueFlogcapture_clearz Clear all other logging handlersz--logging-levelNOTSETlogcapture_levelzSet the log level to captureN)Z
add_optiongetenv_optr'   r(   )r   parserenvr   r   r   optionss   s6    
zLogCapture.optionsc             C   sP   || _ |jr|jrd| _|j| _|j| _|j| _	|j
| _|jrL|jd| _dS )zConfigure plugin.
        F,N)confr:   ZloggingConfigenabledr?   r'   rA   r(   rD   clearrF   loglevelrB   splitr)   )r   rK   rM   r   r   r   	configure   s    zLogCapture.configurec             C   s   t  }| jrrt|dr2x|jD ]}|| q W x>tt jjj	
 D ](}t|drFx|jD ]}|| q\W qFW x,|jd d  D ]}t|tr|j| qW || j t| dd}|tt | d S )NhandlersrP   rE   )r$   	getLoggerrO   hasattrrS   ZremoveHandlerr   ZLoggerZmanagerZ
loggerDictvalues
isinstancer#   removeZ
addHandlerhandlergetattrZsetLevel)r   Zroot_loggerrY   ZloggerrP   r   r   r   setupLoghandler   s    


zLogCapture.setupLoghandlerc             C   s   |    dS )z7Set up logging handler before test run begins.
        N)start)r   r   r   r   begin   s    zLogCapture.beginc             C   s    t | j| j| j| _|   d S )N)r#   r'   r(   r)   rY   r[   )r   r   r   r   r\      s    

zLogCapture.startc             C   s   d S )Nr   )r   r   r   r   end   s    zLogCapture.endc             C   s   |    dS )z0Clear buffers and handlers before test.
        N)r[   )r   testr   r   r   
beforeTest   s    zLogCapture.beforeTestc             C   s   | j   dS )z"Clear buffers after test.
        N)rY   r-   )r   r_   r   r   r   	afterTest   s    zLogCapture.afterTestc             C   s   |  ||S )z5Add captured log messages to failure output.
        )formatError)r   r_   errr   r   r   formatFailure   s    zLogCapture.formatFailurec             C   s2   |    |_}|s|S |\}}}|| |||fS )z3Add captured log messages to error output.
        )formatLogRecordsZcapturedLoggingaddCaptureToErr)r   r_   rc   recordsZecevtbr   r   r   rb      s
    
zLogCapture.formatErrorc             C   s   t tt| jjS )N)r   r   r   rY   r&   )r   r   r   r   re      s    zLogCapture.formatLogRecordsc             C   s$   d t|tdg| tdg S )N
z>> begin captured logging <<z>> end captured logging <<)joinr   r   )r   rh   rg   r   r   r   rf      s    zLogCapture.addCaptureToErr)r   r    r!   __doc__rN   rH   r.   Zscorer'   r(   rO   r)   rK   rR   r[   r]   r\   r^   r`   ra   rd   rb   re   rf   r   r   r   r   r9   c   s*   .
r9   )rl   r$   r   r6   Znose.plugins.baser   Z	nose.utilr   r   r   ior   ImportErrorrT   r   logobjectr   r#   r9   r   r   r   r   <module>   s   
*