2ddlmZddlmZddlmZmZmZmZm Z m Z m Z ddl Z ddlZddl Z ddlmZddlmZmZddlmZGd d ejZGd d eZd efdZdS)) defaultdict) LogRecord)ListUnionTupleSequenceCallableAnyOptionalN)pformat)SequenceComparisoncompare)wrapceZdZUdZeeed<eeed<e Z dZ dZ e jZ d&d eeeefd ed ed ed eeeeegeffdedefdZedZdZdZdZdZdZd'd efdZde jfdZ dZ!dZ"edZ#dZ$defdZ%dZ&d Z'dd!d"efd#Z(d$Z)d%Z*dS)( LogCapturea These are used to capture entries logged to the Python logging framework and make assertions about what was logged. :param names: A string (or tuple of strings) containing the dotted name(s) of loggers to capture. By default, the root logger is captured. :param install: If `True`, the :class:`LogCapture` will be installed as part of its instantiation. :param propagate: If specified, any captured loggers will have their `propagate` attribute set to the supplied value. This can be used to prevent propagation from a child logger to a parent logger that has configured handlers. :param attributes: The sequence of attribute names to return for each record or a callable that extracts a row from a record. If a sequence of attribute names, those attributes will be taken from the :class:`~logging.LogRecord`. If an attribute is callable, the value used will be the result of calling it. If an attribute is missing, ``None`` will be used in its place. If a callable, it will be called with the :class:`~logging.LogRecord` and the value returned will be used as the row. :param recursive_check: If ``True``, log messages will be compared recursively by :meth:`LogCapture.check`. :param ensure_checks_above: The log level above which checks must be made for logged events. See :meth:`ensure_checked`. recordsensure_checks_aboveFNTr name levelname getMessagenamesinstalllevel propagate attributesrecursive_checkcptj|t|ts|f}||_||_||_||_||_ tt|_ | |j |_n||_||r|dSdSN)loggingHandler__init__ isinstancetuplerrrrrrdictolddefault_ensure_checks_aboverclearr)selfrrrrrrrs /srv/buildsys-work-dir/castor/build_node/builder-2/WGSG1/unpkd_srcs/cloudlinux-venv-1.0.6/venv/lib/python3.11/site-packages/testfixtures/logcapture.pyr#zLogCapture.__init__As   &&&%'' IE  "$.t$$  &'+'GD $ $':D $   LLNNNNN  c|jr;tjddd|jDzdSdS)NzFLogCapture instances not uninstalled by shutdown, loggers captured: %s c3>K|]}t|jVdSr )strr).0is r+ z$LogCapture.atexit..ds*"G"GA3qw<<"G"G"G"G"G"Gr,) instanceswarningswarnjoin)clss r+atexitzLogCapture.atexit^s_ =  M "G"G"G"G"GHHJ       r,c*t|jSr )lenrr*s r+__len__zLogCapture.__len__gs4<   r,cB||j|Sr ) _actual_rowr)r*indexs r+ __getitem__zLogCapture.__getitem__js U 3444r,cft|D] \}}||krd|j|_dS!dS)NT) enumeraterchecked)r*whatr2items r+ __contains__zLogCapture.__contains__msH   GAtt||*. Q'tt  r,cg|_dS)z*Clear any entries that have been captured.N)rr<s r+r)zLogCapture.clearss  r,c(|jD] }d|_ dS)z Mark all captured events as checked. This should be called if you have made assertions about logging other than through :class:`LogCapture` methods. TN)rrDr*records r+mark_all_checkedzLogCapture.mark_all_checkedws( l " "F!FNN " "r,c ||j}|tjkrdSg}|jD]<}|j|kr/|js(|||=|rtdt|zdS)z Ensure every entry logged above the specified `level` has been checked. Raises an :class:`AssertionError` if this is not the case. :param level: the logging level, defaults to :attr:`ensure_checks_above`. NzNot asserted ERROR log(s): %s) rr!NOTSETrlevelnorDappendr?AssertionErrorr )r*r un_checkedrKs r+ensure_checkedzLogCapture.ensure_checkeds =,E GN " " F l <   4   ~* / ODK ( ( (*.DN ' ' ' / /r,c||jvr|jD]}tj|}||jd||jd||_|jd||_|jd||_|jd||_ |j |dSdS)a& Un-install this :class:`LogCapture` from the Python logging framework for the named loggers. This will re-instate any existing handlers for those loggers that were removed during installation and restore their level that prior to installation. rWrXrYrZrN) r4rr!r[r\r'rXrYrZrremoveras r+ uninstallzLogCapture.uninstalls 4> ! !  ? ? *400 24 8999!%)!4T!:"&(:"6t"<"&(:"6t"<#'8K#8#>  N ! !$ ' ' ' ' ' " !r,c\t|jD]}|dS)z=This will uninstall all existing :class:`LogCapture` objects.N)r%r4re)r8r2s r+ uninstall_allzLogCapture.uninstall_alls6s}%%  A KKMMMM  r,c>t|jr||Sg}|jD]A}t||d}t|r |}||Bt |dkr|dSt |S)Nr r)callablergetattrrPr;r%)r*rKvaluesavalues r+r?zLogCapture._actual_rows DO $ $ %??6** *F_ % %400E??$!EGGE e$$$$6{{aay V}}$r,returncng}|jD]*}|||+|S)a The sequence of actual records logged, having had their attributes extracted as specified by the ``attributes`` parameter to the :class:`LogCapture` constructor. This can be useful for making more complex assertions about logged records. The actual records logged can also be inspected by using the :attr:`records` attribute. )rrPr?)r*actualrs r+rpzLogCapture.actualsB / /A MM$**1-- . . . . r,cv|jsdSdd|DS)NzNo logging capturedr.cg|]}d|zS)z %s %s %s)r1rqs r+ z&LogCapture.__str__..sCCC-!+CCCr,)rr7rpr<s r+__str__zLogCapture.__str__s;| )((yyCCT[[]]CCCDDDr,ct|||j|dS)a7 This will compare the captured entries with the expected entries provided and raise an :class:`AssertionError` if they do not match. :param expected: A sequence of entries of the structure specified by the ``attributes`` passed to the constructor. )rp recursiveN)rrprrL)r*expecteds r+checkzLogCapture.checksI  ;;==*     r,) order_mattersr{c|}t||d|jd}||krt|j|jD]}d|j|_dS)as This will check if the captured entries contain all of the expected entries provided and raise an :class:`AssertionError` if not. This will ignore entries that have been captured but that do not match those in ``expected``. :param expected: A sequence of entries of the structure specified by the ``attributes`` passed to the constructor. :param order_matters: A keyword-only parameter that controls whether the order of the captured entries is required to match those of the expected entries. Defaults to ``True``. T)orderedpartialrxN)rprrrQfailedchecked_indicesrrD)r*r{ryrpr@s r+ check_presentzLogCapture.check_presents|$% }ddFZ    v   11 1- / /E*.DL  ' ' / /r,c|Sr rtr<s r+ __enter__zLogCapture.__enter__"s r,cV||dSr )rerS)r*typerm tracebacks r+__exit__zLogCapture.__exit__%s*  r,)NTr NrFNr )+__name__ __module__ __qualname____doc__rr__annotations__r intsetr4r_ installedr!rNr(rr0rboolrr r r# classmethodr9r=rArGr)rLrSrUrrergr?rprvrzrrrrtr,r+rr sq''T)_!#&&&ILI").-1 "L%*'+eCj)    hsmXyk36F-GGH ""%:[!!!555 """++C++++($7,$$$$///6(((&[ %%%$     EEE    $>B///d////6r,rceZdZdZdS)LogCaptureForDecoratorcbt|||Sr )rrr)r<s r+rzLogCaptureForDecorator.install,s(4     r,N)rrrrrtr,r+rr*s#r,rrcXt|pdfddi|}t|j|jS)a A decorator for making a :class:`LogCapture` installed and available for the duration of a test function. :param names: An optional sequence of names specifying the loggers to be captured. If not specified, the root logger will be captured. Keyword parameters other than ``install`` may also be supplied and will be passed on to the :class:`LogCapture` constructor. NrF)rrrre)rkwls r+ log_capturer2s6 u}BBeBrBBA  1; ' ''r,) collectionsrr!rtypingrrrrr r r r9r5pprintr comparisonrrutilsrr"rrr0rrtr,r+rs:######HHHHHHHHHHHHHHHHHH 33333333ZZZZZZZZzZ ( ( ( ( ( ( (r,