-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from .engine import Engine, Session, Transaction, ensure_session | ||
import functools | ||
|
||
|
||
def connect_via_engine(engine, signal, func=None): | ||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(sender, **kw): | ||
matches = False | ||
if isinstance(sender, Engine): | ||
matches = sender is engine | ||
elif isinstance(sender, Session): | ||
matches = sender.engine is engine | ||
elif isinstance(sender, Transaction): | ||
matches = sender.session.engine is engine | ||
if matches: | ||
return func(sender, **kw) | ||
signal.connect(wrapper, weak=False) | ||
return wrapper | ||
return decorator(func) if func else decorator | ||
|
||
|
||
def after_commit(func, *args, **kwargs): | ||
with ensure_session() as session: | ||
def rollback_listener(session): | ||
Session.after_commit.disconnect(commit_listener) | ||
Session.after_rollback.disconnect(rollback_listener) | ||
def commit_listener(session): | ||
func(*args, **kwargs) | ||
rollback_listener(session) | ||
Session.after_commit.connect(commit_listener, session, weak=False) | ||
Session.after_rollback.connect(rollback_listener, session, weak=False) |