晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/hc_python/lib/python3.12/site-packages/sqlalchemy/event/ |
| Current File : //opt/hc_python/lib/python3.12/site-packages/sqlalchemy/event/legacy.py |
# event/legacy.py
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
# <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: https://www.opensource.org/licenses/mit-license.php
"""Routines to handle adaption of legacy call signatures,
generation of deprecation notes and docstrings.
"""
from __future__ import annotations
import typing
from typing import Any
from typing import Callable
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from .registry import _ET
from .registry import _ListenerFnType
from .. import util
from ..util.compat import FullArgSpec
if typing.TYPE_CHECKING:
from .attr import _ClsLevelDispatch
from .base import _HasEventsDispatch
_LegacySignatureType = Tuple[str, List[str], Optional[Callable[..., Any]]]
def _legacy_signature(
since: str,
argnames: List[str],
converter: Optional[Callable[..., Any]] = None,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
"""legacy sig decorator
:param since: string version for deprecation warning
:param argnames: list of strings, which is *all* arguments that the legacy
version accepted, including arguments that are still there
:param converter: lambda that will accept tuple of this full arg signature
and return tuple of new arg signature.
"""
def leg(fn: Callable[..., Any]) -> Callable[..., Any]:
if not hasattr(fn, "_legacy_signatures"):
fn._legacy_signatures = [] # type: ignore[attr-defined]
fn._legacy_signatures.append((since, argnames, converter)) # type: ignore[attr-defined] # noqa: E501
return fn
return leg
def _wrap_fn_for_legacy(
dispatch_collection: _ClsLevelDispatch[_ET],
fn: _ListenerFnType,
argspec: FullArgSpec,
) -> _ListenerFnType:
for since, argnames, conv in dispatch_collection.legacy_signatures:
if argnames[-1] == "**kw":
has_kw = True
argnames = argnames[0:-1]
else:
has_kw = False
if len(argnames) == len(argspec.args) and has_kw is bool(
argspec.varkw
):
formatted_def = "def %s(%s%s)" % (
dispatch_collection.name,
", ".join(dispatch_collection.arg_names),
", **kw" if has_kw else "",
)
warning_txt = (
'The argument signature for the "%s.%s" event listener '
"has changed as of version %s, and conversion for "
"the old argument signature will be removed in a "
'future release. The new signature is "%s"'
% (
dispatch_collection.clsname,
dispatch_collection.name,
since,
formatted_def,
)
)
if conv is not None:
assert not has_kw
def wrap_leg(*args: Any, **kw: Any) -> Any:
util.warn_deprecated(warning_txt, version=since)
assert conv is not None
return fn(*conv(*args))
else:
def wrap_leg(*args: Any, **kw: Any) -> Any:
util.warn_deprecated(warning_txt, version=since)
argdict = dict(zip(dispatch_collection.arg_names, args))
args_from_dict = [argdict[name] for name in argnames]
if has_kw:
return fn(*args_from_dict, **kw)
else:
return fn(*args_from_dict)
return wrap_leg
else:
return fn
def _indent(text: str, indent: str) -> str:
return "\n".join(indent + line for line in text.split("\n"))
def _standard_listen_example(
dispatch_collection: _ClsLevelDispatch[_ET],
sample_target: Any,
fn: _ListenerFnType,
) -> str:
example_kw_arg = _indent(
"\n".join(
"%(arg)s = kw['%(arg)s']" % {"arg": arg}
for arg in dispatch_collection.arg_names[0:2]
),
" ",
)
if dispatch_collection.legacy_signatures:
current_since = max(
since
for since, args, conv in dispatch_collection.legacy_signatures
)
else:
current_since = None
text = (
"from sqlalchemy import event\n\n\n"
"@event.listens_for(%(sample_target)s, '%(event_name)s')\n"
"def receive_%(event_name)s("
"%(named_event_arguments)s%(has_kw_arguments)s):\n"
" \"listen for the '%(event_name)s' event\"\n"
"\n # ... (event handling logic) ...\n"
)
text %= {
"current_since": (
" (arguments as of %s)" % current_since if current_since else ""
),
"event_name": fn.__name__,
"has_kw_arguments": ", **kw" if dispatch_collection.has_kw else "",
"named_event_arguments": ", ".join(dispatch_collection.arg_names),
"example_kw_arg": example_kw_arg,
"sample_target": sample_target,
}
return text
def _legacy_listen_examples(
dispatch_collection: _ClsLevelDispatch[_ET],
sample_target: str,
fn: _ListenerFnType,
) -> str:
text = ""
for since, args, conv in dispatch_collection.legacy_signatures:
text += (
"\n# DEPRECATED calling style (pre-%(since)s, "
"will be removed in a future release)\n"
"@event.listens_for(%(sample_target)s, '%(event_name)s')\n"
"def receive_%(event_name)s("
"%(named_event_arguments)s%(has_kw_arguments)s):\n"
" \"listen for the '%(event_name)s' event\"\n"
"\n # ... (event handling logic) ...\n"
% {
"since": since,
"event_name": fn.__name__,
"has_kw_arguments": (
" **kw" if dispatch_collection.has_kw else ""
),
"named_event_arguments": ", ".join(args),
"sample_target": sample_target,
}
)
return text
def _version_signature_changes(
parent_dispatch_cls: Type[_HasEventsDispatch[_ET]],
dispatch_collection: _ClsLevelDispatch[_ET],
) -> str:
since, args, conv = dispatch_collection.legacy_signatures[0]
return (
"\n.. versionchanged:: %(since)s\n"
" The :meth:`.%(clsname)s.%(event_name)s` event now accepts the \n"
" arguments %(named_event_arguments)s%(has_kw_arguments)s.\n"
" Support for listener functions which accept the previous \n"
' argument signature(s) listed above as "deprecated" will be \n'
" removed in a future release."
% {
"since": since,
"clsname": parent_dispatch_cls.__name__,
"event_name": dispatch_collection.name,
"named_event_arguments": ", ".join(
":paramref:`.%(clsname)s.%(event_name)s.%(param_name)s`"
% {
"clsname": parent_dispatch_cls.__name__,
"event_name": dispatch_collection.name,
"param_name": param_name,
}
for param_name in dispatch_collection.arg_names
),
"has_kw_arguments": ", **kw" if dispatch_collection.has_kw else "",
}
)
def _augment_fn_docs(
dispatch_collection: _ClsLevelDispatch[_ET],
parent_dispatch_cls: Type[_HasEventsDispatch[_ET]],
fn: _ListenerFnType,
) -> str:
header = (
".. container:: event_signatures\n\n"
" Example argument forms::\n"
"\n"
)
sample_target = getattr(parent_dispatch_cls, "_target_class_doc", "obj")
text = header + _indent(
_standard_listen_example(dispatch_collection, sample_target, fn),
" " * 8,
)
if dispatch_collection.legacy_signatures:
text += _indent(
_legacy_listen_examples(dispatch_collection, sample_target, fn),
" " * 8,
)
text += _version_signature_changes(
parent_dispatch_cls, dispatch_collection
)
return util.inject_docstring_text(fn.__doc__, text, 1)
|