U e$@sdZddlZddlmZddlZddlmZddlmZmZm Z zddl 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||\|_|_dSN) _partition inclusive exclusive)selfZfilter_componentsrE/opt/hc_python/lib/python3.8/site-packages/nose/plugins/logcapture.py__init__!szFilterSet.__init__cCsDgg}}|D],}|dr0||ddq||q||fS)N-) startswithappend) componentsr r componentrrrr %s    zFilterSet._partitioncCs|sdS||o|| S)z-returns whether this record should be printedT)_allow_denyr recordrrrallow/szFilterSet.allowcs fdd}tttt||S)zNreturn the bool of whether `record` starts with any item in `matchers`cs|kp|dS)N.)r)keyrrrrecord_matches_key:sz0FilterSet._any_match..record_matches_key)rboollistmap)Zmatchersrrrrr _any_match7s zFilterSet._any_matchcCs|js dS||j|S)NT)r r#rrrrr?szFilterSet._allowcCs|js dS||j|S)NF)r r#rrrrrDszFilterSet._denyN) __name__ __module__ __qualname__rr staticmethodrr#rrrrrrr src@sDeZdZddZddZddZddZd d Zd d Zd dZ dS)MyMemoryHandlercCs4t|t||}||t||_g|_dSr )rrlogging Formatter setFormatterr filtersetbuffer)r logformat logdatefmtfiltersfmtrrrrKs     zMyMemoryHandler.__init__cCs|j||dSr )r-rformatrrrremitQszMyMemoryHandler.emitcCsdSr rr rrrflushSszMyMemoryHandler.flushcCs g|_dSr )r-r4rrrtruncateUszMyMemoryHandler.truncatecCs|j|jrt||SdSr )r,rnamerfilterrrrrr8WszMyMemoryHandler.filtercCs|j}|d=|S)Nlock)__dict__copyr staterrr __getstate__Zs zMyMemoryHandler.__getstate__cCs|j|t|_dSr )r:update threadingRLockr9r<rrr __setstate__^s zMyMemoryHandler.__setstate__N) r$r%r&rr3r5r6r8r>rBrrrrr(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_falserDz^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])rFrHrGmetavarrIz--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 parserenvrrroptionsssZ zLogCapture.optionscCsP||_|jr|jrd|_|j|_|j|_|j|_ |j |_ |j rL|j d|_dS)zConfigure plugin. F,N)confrDZ loggingConfigenabledrKr.rMr/rPclearrRloglevelrNsplitr0)r rXrZrrr configures zLogCapture.configurecCst}|jrft|dr.|jD]}||qttjjj D]$}t|dr@|jD]}||qTq@|jddD]}t |t rt|j |qt||jt|dd}|tt|dS)Nhandlersr]rQ)r) getLoggerr\hasattrr` removeHandlerr!Loggermanager loggerDictvalues isinstancer(remove addHandlerhandlergetattrsetLevel)r Z root_loggerrkloggerr]rrrsetupLoghandlers        zLogCapture.setupLoghandlercCs |dS)z7Set up logging handler before test run begins. N)startr4rrrbeginszLogCapture.begincCs t|j|j|j|_|dSr )r(r.r/r0rkror4rrrrps zLogCapture.startcCsdSr rr4rrrendszLogCapture.endcCs |dS)z0Clear buffers and handlers before test. N)ror testrrr beforeTestszLogCapture.beforeTestcCs|jdS)z"Clear buffers after test. N)rkr6rsrrr afterTestszLogCapture.afterTestcCs |||S)z5Add captured log messages to failure output. ) formatError)r rterrrrr formatFailureszLogCapture.formatFailurecCs2||_}|s|S|\}}}|||||fS)z3Add captured log messages to error output. )formatLogRecordsZcapturedLoggingaddCaptureToErr)r rtrxrecordsecevtbrrrrws  zLogCapture.formatErrorcCsttt|jjSr )r!r"rrkr-r4rrrrzszLogCapture.formatLogRecordscCs$dt|tdg|tdgS)N z>> begin captured logging <> end captured logging <<)joinrr)r r~r|rrrr{s zLogCapture.addCaptureToErr)r$r%r&__doc__r[rUr7Zscorer.r/r\r0rXr_rorqrprrrurvryrwrzr{rrrrrCcs*. rC)rr)rr@Znose.plugins.baserZ nose.utilrrrior ImportErrorrar$logobjectrr(rCrrrrs   *