summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/event/legacy.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/event/legacy.py')
-rw-r--r--lib/sqlalchemy/event/legacy.py72
1 files changed, 38 insertions, 34 deletions
diff --git a/lib/sqlalchemy/event/legacy.py b/lib/sqlalchemy/event/legacy.py
index 1883070f4..c30b922fd 100644
--- a/lib/sqlalchemy/event/legacy.py
+++ b/lib/sqlalchemy/event/legacy.py
@@ -15,10 +15,11 @@ from .. import util
def _legacy_signature(since, argnames, converter=None):
def leg(fn):
- if not hasattr(fn, '_legacy_signatures'):
+ if not hasattr(fn, "_legacy_signatures"):
fn._legacy_signatures = []
fn._legacy_signatures.append((since, argnames, converter))
return fn
+
return leg
@@ -30,15 +31,18 @@ def _wrap_fn_for_legacy(dispatch_collection, fn, argspec):
else:
has_kw = False
- if len(argnames) == len(argspec.args) \
- and has_kw is bool(argspec.keywords):
+ if len(argnames) == len(argspec.args) and has_kw is bool(
+ argspec.keywords
+ ):
if conv:
assert not has_kw
def wrap_leg(*args):
return fn(*conv(*args))
+
else:
+
def wrap_leg(*args, **kw):
argdict = dict(zip(dispatch_collection.arg_names, args))
args = [argdict[name] for name in argnames]
@@ -46,16 +50,14 @@ def _wrap_fn_for_legacy(dispatch_collection, fn, argspec):
return fn(*args, **kw)
else:
return fn(*args)
+
return wrap_leg
else:
return fn
def _indent(text, indent):
- return "\n".join(
- indent + line
- for line in text.split("\n")
- )
+ return "\n".join(indent + line for line in text.split("\n"))
def _standard_listen_example(dispatch_collection, sample_target, fn):
@@ -64,10 +66,13 @@ def _standard_listen_example(dispatch_collection, sample_target, fn):
"%(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)
+ current_since = max(
+ since
+ for since, args, conv in dispatch_collection.legacy_signatures
+ )
else:
current_since = None
text = (
@@ -82,7 +87,6 @@ def _standard_listen_example(dispatch_collection, sample_target, fn):
if len(dispatch_collection.arg_names) > 3:
text += (
-
"\n# named argument style (new in 0.9)\n"
"@event.listens_for("
"%(sample_target)s, '%(event_name)s', named=True)\n"
@@ -93,13 +97,14 @@ def _standard_listen_example(dispatch_collection, sample_target, fn):
)
text %= {
- "current_since": " (arguments as of %s)" %
- current_since if current_since else "",
+ "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
+ "sample_target": sample_target,
}
return text
@@ -113,13 +118,15 @@ def _legacy_listen_examples(dispatch_collection, sample_target, fn):
"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" % {
+ "\n # ... (event handling logic) ...\n"
+ % {
"since": since,
"event_name": fn.__name__,
"has_kw_arguments": " **kw"
- if dispatch_collection.has_kw else "",
+ if dispatch_collection.has_kw
+ else "",
"named_event_arguments": ", ".join(args),
- "sample_target": sample_target
+ "sample_target": sample_target,
}
)
return text
@@ -133,37 +140,34 @@ def _version_signature_changes(dispatch_collection):
" arguments ``%(named_event_arguments)s%(has_kw_arguments)s``.\n"
" Listener functions which accept the previous argument \n"
" signature(s) listed above will be automatically \n"
- " adapted to the new signature." % {
+ " adapted to the new signature."
+ % {
"since": since,
"event_name": dispatch_collection.name,
"named_event_arguments": ", ".join(dispatch_collection.arg_names),
- "has_kw_arguments": ", **kw" if dispatch_collection.has_kw else ""
+ "has_kw_arguments": ", **kw" if dispatch_collection.has_kw else "",
}
)
def _augment_fn_docs(dispatch_collection, parent_dispatch_cls, fn):
- header = ".. container:: event_signatures\n\n"\
- " Example argument forms::\n"\
+ 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)
+ 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)
+ _legacy_listen_examples(dispatch_collection, sample_target, fn),
+ " " * 8,
+ )
text += _version_signature_changes(dispatch_collection)
- return util.inject_docstring_text(fn.__doc__,
- text,
- 1
- )
+ return util.inject_docstring_text(fn.__doc__, text, 1)