a
    ñ»j¤%  ã                   @   s†   d 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m	Z	m
Z
 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_strc                   @   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   ú=E:\soccer\ml_env\Lib\site-packages\nose/plugins/logcapture.pyÚ__init__   s    zFilterSet.__init__c                 C   sD   g g  }}| D ],}|  d¡r0| |dd … ¡ q| |¡ q||fS )Nú-é   )Ú
startswithÚappend)Ú
componentsr
   r   Ú	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_key1   s    z0FilterSet._any_match.<locals>.record_matches_key)r   ÚboolÚlistÚmap)Zmatchersr   r   r   r   r   Ú
_any_match.   s    zFilterSet._any_matchc                 C   s   | j s
dS |  | j |¡S )NT)r
   r"   r   r   r   r   r   6   s    zFilterSet._allowc                 C   s   | j s
dS |  | j |¡S )NF)r   r"   r   r   r   r   r   ;   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 r   )r   r   ÚloggingÚ	FormatterÚsetFormatterr   Ú	filtersetÚbuffer)r   Ú	logformatÚ
logdatefmtÚfiltersÚfmtr   r   r   r   B   s
    


zMyMemoryHandler.__init__c                 C   s   | j  |  |¡¡ d S r   )r,   r   Úformatr   r   r   r   ÚemitI   s    zMyMemoryHandler.emitc                 C   s   d S r   r   ©r   r   r   r   ÚflushL   s    zMyMemoryHandler.flushc                 C   s
   g | _ d S r   )r,   r3   r   r   r   ÚtruncateO   s    zMyMemoryHandler.truncatec                 C   s   | j  |j¡rt | |¡S d S r   )r+   r   Únamer   Úfilterr   r   r   r   r7   R   s    zMyMemoryHandler.filterc                 C   s   | j  ¡ }|d= |S )NÚlock)Ú__dict__Úcopy©r   Ústater   r   r   Ú__getstate__W   s    
zMyMemoryHandler.__getstate__c                 C   s   | j  |¡ t ¡ | _d S r   )r9   ÚupdateÚ	threadingÚRLockr8   r;   r   r   r   Ú__setstate__\   s    zMyMemoryHandler.__setstate__N)
r#   r$   r%   r   r2   r4   r5   r7   r=   rA   r   r   r   r   r'   A   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 )"Ú
LogCapturezü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dddd� |j ddd	dd
dd� |j dd	dddd� |j ddd| d¡pV| 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--nologcaptureÚstore_falser   rC   z^Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE])ÚactionÚdefaultÚdestÚhelpz--capture-logsz--capture_logsÚ
store_trueÚcapture_logsz\Enable logging capture plugin. Logging configuration will be left intact. [NOSE_CAPTURELOGS]z--logging-initÚlogging_initzZInitialize standard logging configuration with:
logging.basicConfig(level)
 [NOSE_LOGINIT]z--logging-formatÚstoreÚlogcapture_formatZNOSE_LOGFORMATZFORMATzvSpecify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT])rE   rG   rF   ÚmetavarrH   z--logging-datefmtÚlogcapture_datefmtZNOSE_LOGDATEFMTz�Specify 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-handlersFÚlogcapture_clearz Clear all other logging handlersz--logging-levelÚWARNINGÚlogcapture_levelzSet the log level to captureN)Ú
add_optionÚgetr-   r.   )r   ÚparserÚenvr   r   r   Úoptionso   sT    ýýýüü
ýýýzLogCapture.optionsc                 C   s    || _ |j| _|j| _|j| _|j| _|j	r2|j
r8d| _dtjv rPtjd dksV|jr\d| _dtjv rttjd dksz|jrˆtj| jd� |jrœ|j d¡| _dS )	zConfigure plugin.FZNOSE_CAPTURELOGSÚ1TZNOSE_LOGINIT)Úlevelú,N)ÚconfrM   r-   rO   r.   rQ   ÚclearrS   ÚloglevelrC   ZloggingConfigÚenabledÚosÚenvironrJ   rK   r(   ÚbasicConfigrP   Úsplitr/   )r   rX   r\   r   r   r   Ú	configure¨   s.    ÿþýÿþýzLogCapture.configurec                 C   s¼   t  ¡ }| jrft|dƒr.|jD ]}| |¡ qtt jjj	 
¡ ƒD ]$}t|dƒr@|jD ]}| |¡ qTq@|jd d … D ]}t|tƒrt|j |¡ qt| | j¡ t| ddƒ}| tt |ƒ¡ d S )NÚhandlersr^   ÚNOTSET)r(   Ú	getLoggerr]   Úhasattrre   ÚremoveHandlerr    ÚLoggerÚmanagerÚ
loggerDictÚvaluesÚ
isinstancer'   ÚremoveÚ
addHandlerÚhandlerÚgetattrÚsetLevel)r   Zroot_loggerrq   Úloggerr^   r   r   r   ÚsetupLoghandler¾   s    




zLogCapture.setupLoghandlerc                 C   s   |   ¡  dS )z.Set up logging handler before test run begins.N)Ústartr3   r   r   r   ÚbeginÑ   s    zLogCapture.beginc                 C   s    t | j| j| jƒ| _|  ¡  d S r   )r'   r-   r.   r/   rq   ru   r3   r   r   r   rv   Õ   s    ÿzLogCapture.startc                 C   s   d S r   r   r3   r   r   r   ÚendÛ   s    zLogCapture.endc                 C   s   |   ¡  dS )z'Clear buffers and handlers before test.N)ru   ©r   Útestr   r   r   Ú
beforeTestÞ   s    zLogCapture.beforeTestc                 C   s   | j  ¡  dS )zClear buffers after test.N)rq   r5   ry   r   r   r   Ú	afterTestâ   s    zLogCapture.afterTestc                 C   s   |   ||¡S )z,Add captured log messages to failure output.)ÚformatError)r   rz   Úerrr   r   r   ÚformatFailureæ   s    zLogCapture.formatFailurec                 C   s2   |   ¡  |_}|s|S |\}}}||  ||¡|fS )z*Add captured log messages to error output.)ÚformatLogRecordsZcapturedLoggingÚaddCaptureToErr)r   rz   r~   ÚrecordsÚecÚevÚtbr   r   r   r}   ê   s
    
zLogCapture.formatErrorc                 C   s   t tt| jjƒƒS r   )r    r!   r   rq   r,   r3   r   r   r   r€   ó   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   r„   r‚   r   r   r   r�   ö   s    ÿþÿzLogCapture.addCaptureToErr)r#   r$   r%   Ú__doc__r_   Zenv_optr6   Zscorer-   r.   r]   r/   rX   rd   ru   rw   rv   rx   r{   r|   r   r}   r€   r�   r   r   r   r   rB   a   s*   9	rB   )rˆ   r(   r`   r?   r   Znose.plugins.baser   Z	nose.utilr   r   r   rg   r#   ÚlogÚobjectr   r'   rB   r   r   r   r   Ú<module>   s   
) 