Singleton Pattern For AfterCommitAction. The idea of doing this is to keep a single object at all time for the AfterCommitActions, that way we can save multiple function calls with arguments for each session commit().
| 53 | |
| 54 | |
| 55 | class AfterCommitAction: |
| 56 | """ |
| 57 | Singleton Pattern For AfterCommitAction. |
| 58 | The idea of doing this is to keep a single object at all time for the |
| 59 | AfterCommitActions, that way we can save multiple function calls with arguments |
| 60 | for each session commit(). |
| 61 | """ |
| 62 | instance = None |
| 63 | |
| 64 | class __AfterCommitAction: |
| 65 | |
| 66 | def after_commit(self, session): |
| 67 | for i in range(0, len(self.callback_args_map[session])): |
| 68 | self.callbacks_map[session][i](**self.callback_args_map[session][i]) |
| 69 | |
| 70 | self.callbacks_map[session] = {} |
| 71 | self.callback_args_map[session] = {} |
| 72 | |
| 73 | def __init__(self, |
| 74 | session, |
| 75 | callback, |
| 76 | callback_args): |
| 77 | """ |
| 78 | |
| 79 | :param session: a sql alchemy session object |
| 80 | :param operation: one of insert, update, delete |
| 81 | """ |
| 82 | self.callbacks_map = {} |
| 83 | self.callback_args_map = {} |
| 84 | |
| 85 | self.callbacks_map[session] = [callback] |
| 86 | self.callback_args_map[session] = [callback_args] |
| 87 | |
| 88 | check_for_listener_and_attach(session, self.after_commit) |
| 89 | |
| 90 | def __str__(self): |
| 91 | return repr(self) |
| 92 | |
| 93 | def __init__(self, session, callback, callback_args): |
| 94 | if not AfterCommitAction.instance: |
| 95 | AfterCommitAction.instance = AfterCommitAction.__AfterCommitAction(session, callback, callback_args) |
| 96 | else: |
| 97 | check_for_listener_and_attach(session, AfterCommitAction.instance.after_commit) |
| 98 | if AfterCommitAction.instance.callbacks_map.get(session): |
| 99 | AfterCommitAction.instance.callbacks_map.get(session).append(callback) |
| 100 | AfterCommitAction.instance.callback_args_map.get(session).append(callback_args) |
| 101 | else: |
| 102 | AfterCommitAction.instance.callbacks_map[session] = [callback] |
| 103 | AfterCommitAction.instance.callback_args_map[session] = [callback_args] |
| 104 | |
| 105 | def __getattr__(self, name): |
| 106 | return getattr(self.instance, name) |
| 107 | |
| 108 | |
| 109 | @contextmanager |
no outgoing calls
no test coverage detected