diff options
| author | J08nY | 2017-06-29 23:51:47 +0200 |
|---|---|---|
| committer | J08nY | 2017-08-30 13:18:10 +0200 |
| commit | f2cf2d0c96c0cf47e6dfa80137bb705ec4e8321b (patch) | |
| tree | 05afab72170dca745dad0d9976b28ce5878956d0 /src/mailman/workflows/base.py | |
| parent | c1060c9dfec4776ab6d714bea6e678a7d708396e (diff) | |
| download | mailman-f2cf2d0c96c0cf47e6dfa80137bb705ec4e8321b.tar.gz mailman-f2cf2d0c96c0cf47e6dfa80137bb705ec4e8321b.tar.zst mailman-f2cf2d0c96c0cf47e6dfa80137bb705ec4e8321b.zip | |
Create mailman.workflows package. Move base Workflow there.
- Also introduce IWorkflow, ISubscriptionWorkflow,
IUnsubscriptionWorkflow.
Diffstat (limited to '')
| -rw-r--r-- | src/mailman/workflows/base.py (renamed from src/mailman/app/workflow.py) | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/src/mailman/app/workflow.py b/src/mailman/workflows/base.py index 92b78c184..5988dfa43 100644 --- a/src/mailman/app/workflow.py +++ b/src/mailman/workflows/base.py @@ -22,7 +22,8 @@ import json import logging from collections import deque -from mailman.interfaces.workflow import IWorkflowStateManager + +from mailman.interfaces.workflows import IWorkflowStateManager from public import public from zope.component import getUtility @@ -35,24 +36,33 @@ log = logging.getLogger('mailman.error') class Workflow: """Generic workflow.""" - SAVE_ATTRIBUTES = () - INITIAL_STATE = None + initial_state = None + save_attributes = () def __init__(self): self.token = None self._next = deque() - self.push(self.INITIAL_STATE) + self.push(self.initial_state) self.debug = False self._count = 0 - @property - def name(self): - return self.__class__.__name__ - def __iter__(self): + """See `IWorkflow`.""" return self + def __next__(self): + """See `IWorkflow`.""" + try: + name, step = self._pop() + return step() + except IndexError: + raise StopIteration + except: + log.exception('deque: {}'.format(COMMASPACE.join(self._next))) + raise + def push(self, step): + """See `IWorkflow`.""" self._next.append(step) def _pop(self): @@ -63,23 +73,8 @@ class Workflow: print('[{:02d}] -> {}'.format(self._count, name), file=sys.stderr) return name, step - def __next__(self): - try: - name, step = self._pop() - return step() - except IndexError: - raise StopIteration - except: - log.exception('deque: {}'.format(COMMASPACE.join(self._next))) - raise - def run_thru(self, stop_after): - """Run the state machine through and including the given step. - - :param stop_after: Name of method, sans prefix to run the - state machine through. In other words, the state machine runs - until the named method completes. - """ + """See `IWorkflow`.""" results = [] while True: try: @@ -93,12 +88,7 @@ class Workflow: return results def run_until(self, stop_before): - """Trun the state machine until (not including) the given step. - - :param stop_before: Name of method, sans prefix that the - state machine is run until the method is reached. Unlike - `run_thru()` the named method is not run. - """ + """See `IWorkflow`.""" results = [] while True: try: @@ -116,9 +106,10 @@ class Workflow: return results def save(self): + """See `IWorkflow`.""" assert self.token, 'Workflow token must be set' state_manager = getUtility(IWorkflowStateManager) - data = {attr: getattr(self, attr) for attr in self.SAVE_ATTRIBUTES} + data = {attr: getattr(self, attr) for attr in self.save_attributes} # Note: only the next step is saved, not the whole stack. This is not # an issue in practice, since there's never more than a single step in # the queue anyway. If we want to support more than a single step in @@ -135,6 +126,7 @@ class Workflow: state_manager.save(self.token, step, json.dumps(data)) def restore(self): + """See `IWorkflow`.""" state_manager = getUtility(IWorkflowStateManager) state = state_manager.restore(self.token) if state is None: @@ -144,7 +136,7 @@ class Workflow: if state.step: self._next.append(state.step) data = json.loads(state.data) - for attr in self.SAVE_ATTRIBUTES: + for attr in self.save_attributes: try: setattr(self, attr, data[attr]) except KeyError: |
