diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-03-16 14:04:41 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-03-16 14:15:11 +0300 |
commit | d83e7ea91cbc5e5d4ea49195eeab84c364dba4bb (patch) | |
tree | 5ffdbed1c2a932a8d41089a6ac2766dd783ad15b /contrib/python/Twisted/py3/twisted/logger | |
parent | f7d1b63f8a2e18da4372628a2917fa0c508ba43a (diff) | |
download | ydb-d83e7ea91cbc5e5d4ea49195eeab84c364dba4bb.tar.gz |
Intermediate changes
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/logger')
6 files changed, 60 insertions, 12 deletions
diff --git a/contrib/python/Twisted/py3/twisted/logger/_filter.py b/contrib/python/Twisted/py3/twisted/logger/_filter.py index fa4220ea3e..07e443849a 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_filter.py +++ b/contrib/python/Twisted/py3/twisted/logger/_filter.py @@ -11,7 +11,7 @@ from typing import Dict, Iterable from zope.interface import Interface, implementer -from constantly import NamedConstant, Names # type: ignore[import] +from constantly import NamedConstant, Names from ._interfaces import ILogObserver, LogEvent from ._levels import InvalidLogLevelError, LogLevel diff --git a/contrib/python/Twisted/py3/twisted/logger/_format.py b/contrib/python/Twisted/py3/twisted/logger/_format.py index 4bc06ec40c..59b44c7f72 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_format.py +++ b/contrib/python/Twisted/py3/twisted/logger/_format.py @@ -6,10 +6,12 @@ Tools for formatting logging events. """ +from __future__ import annotations + from datetime import datetime as DateTime from typing import Any, Callable, Iterator, Mapping, Optional, Union, cast -from constantly import NamedConstant # type: ignore[import] +from constantly import NamedConstant from twisted.python._tzhelper import FixedOffsetTimeZone from twisted.python.failure import Failure @@ -164,6 +166,57 @@ def formatEventAsClassicLogText( return eventText + "\n" +def keycall(key: str, getter: Callable[[str], Any]) -> PotentialCallWrapper: + """ + Check to see if C{key} ends with parentheses ("C{()}"); if not, wrap up the + result of C{get} in a L{PotentialCallWrapper}. Otherwise, call the result + of C{get} first, before wrapping it up. + + @param key: The last dotted segment of a formatting key, as parsed by + L{Formatter.vformat}, which may end in C{()}. + + @param getter: A function which takes a string and returns some other + object, to be formatted and stringified for a log. + + @return: A L{PotentialCallWrapper} that will wrap up the result to allow + for subsequent usages of parens to defer execution to log-format time. + """ + callit = key.endswith("()") + realKey = key[:-2] if callit else key + value = getter(realKey) + if callit: + value = value() + return PotentialCallWrapper(value) + + +class PotentialCallWrapper(object): + """ + Object wrapper that wraps C{getattr()} so as to process call-parentheses + C{"()"} after a dotted attribute access. + """ + + def __init__(self, wrapped: object) -> None: + self._wrapped = wrapped + + def __getattr__(self, name: str) -> object: + return keycall(name, self._wrapped.__getattribute__) + + def __getitem__(self, name: str) -> object: + # The sub-object may not be indexable, but if it isn't, that's the + # caller's problem. + value = self._wrapped[name] # type:ignore[index] + return PotentialCallWrapper(value) + + def __format__(self, format_spec: str) -> str: + return self._wrapped.__format__(format_spec) + + def __repr__(self) -> str: + return self._wrapped.__repr__() + + def __str__(self) -> str: + return self._wrapped.__str__() + + class CallMapping(Mapping[str, Any]): """ Read-only mapping that turns a C{()}-suffix in key names into an invocation @@ -190,12 +243,7 @@ class CallMapping(Mapping[str, Any]): Look up an item in the submapping for this L{CallMapping}, calling it if C{key} ends with C{"()"}. """ - callit = key.endswith("()") - realKey = key[:-2] if callit else key - value = self._submapping[realKey] - if callit: - value = value() - return value + return keycall(key, self._submapping.__getitem__) def formatWithCall(formatString: str, mapping: Mapping[str, Any]) -> str: diff --git a/contrib/python/Twisted/py3/twisted/logger/_io.py b/contrib/python/Twisted/py3/twisted/logger/_io.py index 7a49718db7..9663421220 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_io.py +++ b/contrib/python/Twisted/py3/twisted/logger/_io.py @@ -9,7 +9,7 @@ File-like object that logs. import sys from typing import AnyStr, Iterable, Optional -from constantly import NamedConstant # type: ignore[import] +from constantly import NamedConstant from incremental import Version from twisted.python.deprecate import deprecatedProperty diff --git a/contrib/python/Twisted/py3/twisted/logger/_json.py b/contrib/python/Twisted/py3/twisted/logger/_json.py index 2ecdd43045..0cc05ce501 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_json.py +++ b/contrib/python/Twisted/py3/twisted/logger/_json.py @@ -10,7 +10,7 @@ from json import dumps, loads from typing import IO, Any, AnyStr, Dict, Iterable, Optional, Union, cast from uuid import UUID -from constantly import NamedConstant # type: ignore[import] +from constantly import NamedConstant from twisted.python.failure import Failure from ._file import FileLogObserver diff --git a/contrib/python/Twisted/py3/twisted/logger/_levels.py b/contrib/python/Twisted/py3/twisted/logger/_levels.py index 800a549f88..39fab342de 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_levels.py +++ b/contrib/python/Twisted/py3/twisted/logger/_levels.py @@ -6,7 +6,7 @@ Log levels. """ -from constantly import NamedConstant, Names # type: ignore[import] +from constantly import NamedConstant, Names class InvalidLogLevelError(Exception): diff --git a/contrib/python/Twisted/py3/twisted/logger/_stdlib.py b/contrib/python/Twisted/py3/twisted/logger/_stdlib.py index 030b643883..abc707e4a8 100644 --- a/contrib/python/Twisted/py3/twisted/logger/_stdlib.py +++ b/contrib/python/Twisted/py3/twisted/logger/_stdlib.py @@ -11,7 +11,7 @@ from typing import Mapping, Tuple from zope.interface import implementer -from constantly import NamedConstant # type: ignore[import] +from constantly import NamedConstant from twisted.python.compat import currentframe from ._format import formatEvent |