B t `$@sdZddlZddlmZddlZddlmZddlmZmZm Z yddl m Z Wn e k rpddl m Z YnXe eZGdddeZGd d d eZGd d d eZdS) 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)anyplnsafe_str)StringIOc@sLeZdZddZddZeeZddZddZeeZd d Zd d Z d S) FilterSetcCs||\|_|_dS)N) _partition inclusive exclusive)selfZfilter_componentsr ;/tmp/pip-unpacked-wheel-cjhnoqsi/nose/plugins/logcapture.py__init__!szFilterSet.__init__cCsHgg}}x4|D],}|dr2||ddq||qW||fS)N-) startswithappend) componentsr r componentr r rr %s    zFilterSet._partitioncCs|sdS||o|| S)z-returns whether this record should be printedT)_allow_deny)r recordr r rallow/szFilterSet.allowcs fdd}tttt||S)zNreturn the bool of whether `record` starts with any item in `matchers`cs|kp|dS)N.)r)key)rr rrecord_matches_key:sz0FilterSet._any_match..record_matches_key)rboollistmap)Zmatchersrrr )rr _any_match7s zFilterSet._any_matchcCs|js dS||j|S)NT)r r )r rr r rr?szFilterSet._allowcCs|js dS||j|S)NF)r r )r rr r rrDszFilterSet._denyN) __name__ __module__ __qualname__rr staticmethodrr rrr r r rr src@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)MyMemoryHandlercCs4t|t||}||t||_g|_dS)N)rrlogging Formatter setFormatterr filtersetbuffer)r logformat logdatefmtfiltersfmtr r rrKs     zMyMemoryHandler.__init__cCs|j||dS)N)r*rformat)r rr r remitQszMyMemoryHandler.emitcCsdS)Nr )r r r rflushSszMyMemoryHandler.flushcCs g|_dS)N)r*)r r r rtruncateUszMyMemoryHandler.truncatecCs|j|jrt||SdS)N)r)rnamerfilter)r rr r rr4WszMyMemoryHandler.filtercCs|j}|d=|S)Nlock)__dict__copy)r stater r r __getstate__Zs zMyMemoryHandler.__getstate__cCs|j|t|_dS)N)r6update threadingRLockr5)r r8r r r __setstate__^s zMyMemoryHandler.__setstate__N) r!r"r#rr0r1r2r4r9r=r r r rr%Jsr%c@seZdZdZdZdZdZdZdZdZ dZ d gZ d d Z d d Z ddZddZddZddZddZddZddZddZddZd d!ZdS)" 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. TZNOSE_NOLOGCAPTURE logcaptureiz$%(name)s: %(levelname)s: %(message)sNFz-nosecCs|jdd||j ddd|jddd|d p6|jd d d |jd dd|dpX|jd dd |jddd|dddd |jdddddd|jdddddddS) z&Register commandline options. z--nologcapture store_falser?z^Disable logging capture plugin. Logging configuration will be left intact. [NOSE_NOLOGCAPTURE])actiondefaultdesthelpz--logging-formatstorelogcapture_formatZNOSE_LOGFORMATZFORMATzvSpecify custom format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGFORMAT])rArCrBmetavarrDz--logging-datefmtlogcapture_datefmtZNOSE_LOGDATEFMTzSpecify custom date/time format to print statements. Uses the same format as used by standard logging handlers. [NOSE_LOGDATEFMT]z--logging-filterlogcapture_filtersZNOSE_LOGFILTERZFILTERaSpecify 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_trueFlogcapture_clearz Clear all other logging handlersz--logging-levelNOTSETlogcapture_levelzSet the log level to captureN) add_optiongetenv_optr+r,)r parserenvr r roptionsss6 zLogCapture.optionscCsP||_|jr|jrd|_|j|_|j|_|j|_ |j |_ |j rL|j d|_dS)zConfigure plugin. F,N)confr?Z loggingConfigenabledrFr+rHr,rKclearrMloglevelrIsplitr-)r rSrUr r r configures zLogCapture.configurecCst}|jrrt|dr2x|jD]}||q Wx>ttjjj D](}t|drFx|jD]}||q\WqFWx,|jddD]}t |t r|j |qW||jt|dd}|tt|dS)NhandlersrXrL)r& getLoggerrWhasattrr[ removeHandlerrLoggermanager loggerDictvalues isinstancer%remove addHandlerhandlergetattrsetLevel)r Z root_loggerrfloggerrXr r rsetupLoghandlers       zLogCapture.setupLoghandlercCs |dS)z7Set up logging handler before test run begins. N)start)r r r rbeginszLogCapture.begincCs t|j|j|j|_|dS)N)r%r+r,r-rfrj)r r r rrks  zLogCapture.startcCsdS)Nr )r r r rendszLogCapture.endcCs |dS)z0Clear buffers and handlers before test. N)rj)r testr r r beforeTestszLogCapture.beforeTestcCs|jdS)z"Clear buffers after test. N)rfr2)r rnr r r afterTestszLogCapture.afterTestcCs |||S)z5Add captured log messages to failure output. ) formatError)r rnerrr r r formatFailureszLogCapture.formatFailurecCs2||_}|s|S|\}}}|||||fS)z3Add captured log messages to error output. )formatLogRecordsZcapturedLoggingaddCaptureToErr)r rnrrrecordsZecevtbr r rrqs  zLogCapture.formatErrorcCsttt|jjS)N)rrrrfr*)r r r rrtszLogCapture.formatLogRecordscCs$dt|tdg|tdgS)N z>> begin captured logging <> end captured logging <<)joinrr)r rwrvr r rruszLogCapture.addCaptureToErr)r!r"r#__doc__rVrPr3Zscorer+r,rWr-rSrZrjrlrkrmrorprsrqrtrur r r rr>cs*. r>)r{r&rr;Znose.plugins.baserZ nose.utilrrrior ImportErrorr\r!logobjectrr%r>r r r rs   *