summaryrefslogtreecommitdiff
path: root/src/mailman/database
diff options
context:
space:
mode:
authorBarry Warsaw2012-04-22 17:33:33 -0400
committerBarry Warsaw2012-04-22 17:33:33 -0400
commit4488631dff02731ff03f2fef01ee27bbd944812b (patch)
treea4064e30019764ffac7f5d65893b49779cb6918b /src/mailman/database
parent7da6bb2bdba138877e53a70d224b253af14c65bc (diff)
parentcd3bab07fe2de591fee0dd27e9d47076a7e7db0f (diff)
downloadmailman-4488631dff02731ff03f2fef01ee27bbd944812b.tar.gz
mailman-4488631dff02731ff03f2fef01ee27bbd944812b.tar.zst
mailman-4488631dff02731ff03f2fef01ee27bbd944812b.zip
Several non-functional improvements to the code base.
Reduce the explicit use of the config.db global by introducing two new helpers: - A new transaction() context manager which will commit the transaction on successful exit, otherwise it will abort the transaction - A new dbconnection decorator which calls the decorated method with the Storm store object as the first argument (after self). This can be used instead of config.db.store. By reducing the explicit use of this global, we have a better chance of refactoring it away in the future. Still TODO: runner.py and lmtp.py. Be explicit about the `store` attribute on the IDatabase interface. More consistent use of __future__ imports. Remove an obsolete command line script.
Diffstat (limited to 'src/mailman/database')
-rw-r--r--src/mailman/database/transaction.py52
1 files changed, 39 insertions, 13 deletions
diff --git a/src/mailman/database/transaction.py b/src/mailman/database/transaction.py
index 7a6ba00af..295f3d567 100644
--- a/src/mailman/database/transaction.py
+++ b/src/mailman/database/transaction.py
@@ -21,15 +21,32 @@ from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
+ 'dbconnection',
+ 'transaction',
'transactional',
]
+from contextlib import contextmanager
+
from mailman.config import config
-class transactional:
+@contextmanager
+def transaction():
+ """Context manager for ensuring the transaction is complete."""
+ try:
+ yield
+ except:
+ config.db.abort()
+ raise
+ else:
+ config.db.commit()
+
+
+
+def transactional(function):
"""Decorator for transactional support.
When the function this decorator wraps exits cleanly, the current
@@ -38,16 +55,25 @@ class transactional:
Either way, the current transaction is completed.
"""
- def __init__(self, function):
- self._function = function
+ def wrapper(*args, **kws):
+ try:
+ rtn = function(*args, **kws)
+ config.db.commit()
+ return rtn
+ except:
+ config.db.abort()
+ raise
+ return wrapper
- def __get__(self, obj, type=None):
- def wrapper(*args, **kws):
- try:
- rtn = self._function(obj, *args, **kws)
- config.db.commit()
- return rtn
- except:
- config.db.abort()
- raise
- return wrapper
+
+
+def dbconnection(function):
+ """Decorator for getting at the database connection.
+
+ Use this to avoid having to access the global `config.db.store`
+ attribute. This calls the function with `store` as the first argument.
+ """
+ def wrapper(*args, **kws):
+ # args[0] is self.
+ return function(args[0], config.db.store, *args[1:], **kws)
+ return wrapper