Simple rollback mechanism
Project description
This is a simple Pythonic mechanism for rolling back multiple operations in a predictable way, usable as a context manager or a standalone instance (see Example usage). By default, errors are re-raised, but an explicit mode or call must be supplied to trigger a rollback. Valid modes are:
- onError Boolean when True will roll back if an error is raised
- onSuccess Boolean when True will roll back if an error is not raised
Both modes can be set to True to always rollback. A rollback can also be triggered manually by calling doRollback. Note that multiple calls to doRollback will only call the rollback steps once.
Errors can be supressed by setting raiseError to False. Note that errors from rollback steps will not be surpressed, regardless of the raiseError setting.
If a rollback is triggered, each step is called in a last in, first out order (LIFO). That is, the most recently added step is called first, the first step is called last.
Compatibility
Rollback was tested with the following versions of Python:
- 3.7.4
- 3.6.9
- 3.5.7
- 3.4.10
- 3.3.7
- 2.7.16
- 2.6.9
Installation
Install from source:
python setup.py install
Install from PyPI:
pip install rollback
Example usage
from __future__ import print_function from rollback import Rollback # *always* rollback after exiting block, letting any error be re-raised with Rollback(onError=True, onSuccess=True) as rollback: print('do a1') rollback.addStep(print, 'undo a1') print('do a2') rollback.addStep(print, 'undo a2') # rollback *only* if *no* error is raised, letting any error be re-raised with Rollback(onSuccess=True) as rollback: print('do b1') rollback.addStep(print, 'undo b1') print('do b2') rollback.addStep(print, 'undo b2') # rollback manually with Rollback() as rollback: print('do c1') rollback.addStep(print, 'undo c1') print('do c2') rollback.addStep(print, 'undo c2') rollback.doRollback() # rollback *only* if an error is raised, suppressing the error with Rollback(onError=True, raiseError=False) as rollback: print('do d1') rollback.addStep(print, 'undo d1') print('do d2') rollback.addStep(print, 'undo d2') raise RuntimeError('this is not re-raised') # rollback *only* if an error is raised, letting the error be re-raised with Rollback(onError=True) as rollback: print('do e1') rollback.addStep(print, 'undo e1') print('do e2') rollback.addStep(print, 'undo e2') raise RuntimeError('this is re-raised')
Produces output:
do a1 do a2 undo a2 undo a1 do b1 do b2 undo b2 undo b1 do c1 do c2 undo c2 undo c1 do d1 do d2 undo d2 undo d1 do e1 do e2 undo e2 undo e1 Traceback (most recent call last): File "example.py", line 41, in <module> raise RuntimeError('this is re-raised') RuntimeError: this is re-raised
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size rollback-1.0.7-py2.py3-none-any.whl (4.3 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes View |
Filename, size rollback-1.0.7.tar.gz (3.5 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for rollback-1.0.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3942cb8e0a4a3348cca880908b13b578b566b1b0d52ed902a9cf31a636536eef |
|
MD5 | b632a92dac1ea5346205c67c03632b3d |
|
BLAKE2-256 | 326e62162c0886e50bc41c67b85413979b7489eafaca88531f21631548a9239d |