$32 GRAYBYTE WORDPRESS FILE MANAGER $47

SERVER : vnpttt-amd7f72-h1.vietnix.vn #1 SMP Fri May 24 12:42:50 UTC 2024
SERVER IP : 103.200.23.149 | ADMIN IP 216.73.216.22
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/cloudlinux/venv/lib/python3.11/site-packages/testfixtures/

HOME
Current File : /opt/cloudlinux/venv/lib/python3.11/site-packages/testfixtures//twisted.py
"""
Tools for helping to test Twisted applications.
"""
from pprint import pformat
from typing import Union, Sequence, Callable
from unittest import TestCase

from constantly import NamedConstant

from . import compare
from twisted.logger import globalLogPublisher, formatEvent, LogLevel


class LogCapture(object):
    """
    A helper for capturing stuff logged using Twisted's loggers.

    :param fields:
      A sequence of field names that :meth:`~LogCapture.check` will use to build
      "actual" events to compare against the expected events passed in.
      If items are strings, they will be treated as keys info the Twisted logging event.
      If they are callable, they will be called with the event as their only parameter.
      If only one field is specified, "actual" events will just be that one field;
      otherwise they will be a tuple of the specified fields.
    """

    def __init__(self, fields: Sequence[Union[str, Callable]] = ('log_level', formatEvent,)):
        #: The list of events captured.
        self.events = []
        self.fields = fields

    def __call__(self, event):
        self.events.append(event)

    def install(self):
        "Start capturing."
        self.original_observers = globalLogPublisher._observers
        globalLogPublisher._observers = [self]

    def uninstall(self):
        "Stop capturing."
        globalLogPublisher._observers = self.original_observers

    def check(self, *expected, order_matters: bool = True):
        """
        Check captured events against those supplied. Please see the ``fields`` parameter
        to the constructor to see how "actual" events are built.

        :param order_matters:
          This defaults to ``True``. If ``False``, the order of expected logging versus
          actual logging will be ignored.
        """
        actual = []
        for event in self.events:
            actual_event = tuple(field(event) if callable(field) else event.get(field)
                            for field in self.fields)
            if len(actual_event) == 1:
                actual_event = actual_event[0]
            actual.append(actual_event)
        if order_matters:
            compare(expected=expected, actual=actual)
        else:
            expected = list(expected)
            matched = []
            unmatched = []
            for entry in actual:
                try:
                    index = expected.index(entry)
                except ValueError:
                    unmatched.append(entry)
                else:
                    matched.append(expected.pop(index))
            if expected:
                raise AssertionError((
                    'entries not as expected:\n\n'
                    'expected and found:\n%s\n\n'
                    'expected but not found:\n%s\n\n'
                    'other entries:\n%s'
                ) % (pformat(matched), pformat(expected), pformat(unmatched)))

    def check_failure_text(self, expected: str, index: int = -1, attribute: str = 'value'):
        """
        Check the string representation of an attribute of a logged ``Failure`` is as expected.

        :param expected: The expected string representation.
        :param index: The index into :attr:`events` where the failure should have been logged.
        :param attribute: The attribute of the failure of which to find the string representation.
        """
        compare(expected, actual=str(getattr(self.events[index]['log_failure'], attribute)))

    def raise_logged_failure(self, start_index: int = 0):
        """
        A debugging tool that raises the first failure encountered in captured logging.

        :param start_index: The index into :attr:`events` from where to start looking for failures.
        """
        for event in self.events[start_index:]:
            failure = event.get('log_failure')
            if failure:
                raise failure

    @classmethod
    def make(cls, testcase: TestCase, **kw):
        """
        Instantiate, install and add a cleanup for a :class:`LogCapture`.

        :param testcase: This must be an instance of :class:`twisted.trial.unittest.TestCase`.
        :param kw: Any other parameters are passed directly to the :class:`LogCapture` constructor.
        :return: The :class:`LogCapture` instantiated by this method.
        """
        capture = cls(**kw)
        capture.install()
        testcase.addCleanup(capture.uninstall)
        return capture


#: Short reference to Twisted's ``LogLevel.debug``
DEBUG: NamedConstant = LogLevel.debug
#: Short reference to Twisted's ``LogLevel.info``
INFO: NamedConstant = LogLevel.info
#: Short reference to Twisted's ``LogLevel.warn``
WARN: NamedConstant = LogLevel.warn
#: Short reference to Twisted's ``LogLevel.error``
ERROR: NamedConstant = LogLevel.error
#: Short reference to Twisted's ``LogLevel.critical``
CRITICAL: NamedConstant = LogLevel.critical

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
17 Dec 2025 3.08 AM
root / root
0755
__pycache__
--
14 Aug 2025 9.24 PM
root / root
0755
tests
--
14 Aug 2025 9.24 PM
root / root
0755
__init__.py
1.194 KB
17 Apr 2025 8.10 PM
root / root
0644
comparison.py
39.142 KB
17 Apr 2025 8.10 PM
root / root
0644
compat.py
0.219 KB
17 Apr 2025 8.10 PM
root / root
0644
components.py
1.306 KB
17 Apr 2025 8.10 PM
root / root
0644
datetime.py
24.423 KB
17 Apr 2025 8.10 PM
root / root
0644
django.py
2.885 KB
17 Apr 2025 8.10 PM
root / root
0644
logcapture.py
10.755 KB
17 Apr 2025 8.10 PM
root / root
0644
mock.py
1.205 KB
17 Apr 2025 8.10 PM
root / root
0644
outputcapture.py
4.688 KB
17 Apr 2025 8.10 PM
root / root
0644
popen.py
9.887 KB
17 Apr 2025 8.10 PM
root / root
0644
replace.py
12.149 KB
17 Apr 2025 8.10 PM
root / root
0644
resolve.py
2.054 KB
17 Apr 2025 8.10 PM
root / root
0644
rmtree.py
2.523 KB
17 Apr 2025 8.10 PM
root / root
0644
shouldraise.py
3.578 KB
17 Apr 2025 8.10 PM
root / root
0644
shouldwarn.py
2.213 KB
17 Apr 2025 8.10 PM
root / root
0644
sybil.py
2.279 KB
17 Apr 2025 8.10 PM
root / root
0644
tempdirectory.py
12.887 KB
17 Apr 2025 8.10 PM
root / root
0644
twisted.py
4.8 KB
17 Apr 2025 8.10 PM
root / root
0644
utils.py
2.737 KB
17 Apr 2025 8.10 PM
root / root
0644
version.txt
0.006 KB
17 Apr 2025 8.10 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF