1 # Copyright (C) 2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Generic workflow."""
29 from collections
import deque
30 from mailman
.interfaces
.workflow
import IWorkflowStateManager
31 from zope
.component
import getUtility
35 log
= logging
.getLogger('mailman.error')
40 """Generic workflow."""
48 self
.push(self
.INITIAL_STATE
)
54 return self
.__class
__.__name
__
60 self
._next
.append(step
)
63 name
= self
._next
.popleft()
64 step
= getattr(self
, '_step_{}'.format(name
))
67 print('[{:02d}] -> {}'.format(self
._count
, name
), file=sys
.stderr
)
72 name
, step
= self
._pop
()
77 log
.exception('deque: {}'.format(COMMASPACE
.join(self
._next
)))
80 def run_thru(self
, stop_after
):
81 """Run the state machine through and including the given step.
83 :param stop_after: Name of method, sans prefix to run the
84 state machine through. In other words, the state machine runs
85 until the named method completes.
90 name
, step
= self
._pop
()
91 except (StopIteration, IndexError):
94 results
.append(step())
95 if name
== stop_after
:
99 def run_until(self
, stop_before
):
100 """Trun the state machine until (not including) the given step.
102 :param stop_before: Name of method, sans prefix that the
103 state machine is run until the method is reached. Unlike
104 `run_thru()` the named method is not run.
109 name
, step
= self
._pop
()
110 except (StopIteration, IndexError):
113 if name
== stop_before
:
114 # Stop executing, but not before we push the last state back
115 # onto the deque. Otherwise, resuming the state machine would
117 self
._next
.appendleft(step
)
119 results
.append(step())
123 assert self
.token
, 'Workflow token must be set'
124 state_manager
= getUtility(IWorkflowStateManager
)
125 data
= {attr
: getattr(self
, attr
) for attr
in self
.SAVE_ATTRIBUTES
}
126 # Note: only the next step is saved, not the whole stack. This is not
127 # an issue in practice, since there's never more than a single step in
128 # the queue anyway. If we want to support more than a single step in
129 # the queue *and* want to support state saving/restoring, change this
130 # method and the restore() method.
131 if len(self
._next
) == 0:
133 elif len(self
._next
) == 1:
136 raise AssertionError(
137 "Can't save a workflow state with more than one step "
140 self
.__class
__.__name
__,
146 state_manager
= getUtility(IWorkflowStateManager
)
147 state
= state_manager
.restore(self
.__class
__.__name
__, self
.token
)
148 if state
is not None:
151 self
._next
.append(state
.step
)
152 if state
.data
is not None:
153 for attr
, value
in json
.loads(state
.data
).items():
154 setattr(self
, attr
, value
)