Skip to content

Commit

Permalink
Documentation updates to ensure examples run properly in Python 2.7.x…
Browse files Browse the repository at this point in the history
…, 3.3.x
  • Loading branch information
corbinbs committed Jan 25, 2014
1 parent e304e3e commit 7257a21
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
32 changes: 22 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Example

It allows to easilly define a workflow, attach it to a class, and use its transitions::

import xworkflows

class MyWorkflow(xworkflows.Workflow):
# A list of state names
states = (
('foo', _(u"Foo")),
('bar', _(u"Bar")),
('baz', _(u"Baz")),
('foo', "Foo"),
('bar', "Bar"),
('baz', "Baz"),
)
# A list of transition definitions; items are (name, source states, target).
transitions = (
Expand All @@ -36,30 +38,40 @@ It allows to easilly define a workflow, attach it to a class, and use its transi
class MyObject(xworkflows.WorkflowEnabled):
state = MyWorkflow()

@transition()
@xworkflows.transition()
def foobar(self):
return 42

# It is possible to use another method for a given transition.
@transition('gobaz')
@xworkflows.transition('gobaz')
def blah(self):
return 13

>>> o = MyObject()
>>> o.state
State('foo')
<StateWrapper: <State: 'foo'>>
>>> o.state.is_foo
True

>>> o.state.name
'foo'
>>> o.state.title
'Foo'
>>> o.foobar()
42
>>> o.state
State('bar')

<StateWrapper: <State: 'bar'>>
>>> o.state.name
'bar'
>>> o.state.title
'Bar'
>>> o.blah()
13
>>> o.state
State('baz')
<StateWrapper: <State: 'baz'>>
>>> o.state.name
'baz'
>>> o.state.title
'Baz'

Hooks
-----
Expand Down
44 changes: 30 additions & 14 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ Declaring workflows

You can now define a :class:`~xworkflows.Workflow`::

import xworkflows

class MyWorkflow(xworkflows.Workflow):
states = (
('init', _(u"Initial state")),
('ready', _(u"Ready")),
('active', _(u"Active")),
('done', _(u"Done")),
('cancelled', _(u"Cancelled")),
('init', "Initial state"),
('ready', "Ready"),
('active', "Active"),
('done', "Done"),
('cancelled', "Cancelled"),
)

transitions = (
Expand Down Expand Up @@ -74,10 +76,19 @@ There is now one method per transition defined in the workflow:

>>> obj = MyObject()
>>> obj.state
State('init')
<StateWrapper: <State: 'init'>>
>>> obj.state.name
'init'
>>> obj.state.title
'Initial state'
>>> obj.prepare()
>>> obj.state
State('ready')
<StateWrapper: <State: 'ready'>>
>>> obj.state.name
'ready'
>>> obj.state.title
'Ready'


As seen in the example above, calling a transition automatically updates the state
of the workflow.
Expand All @@ -87,11 +98,11 @@ Only transitions compatible with the current state may be called:
.. sourcecode:: pycon

>>> obj.state
State('ready')
<StateWrapper: <State: 'ready'>>
>>> obj.complete()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
InvalidTransitionError: Transition "complete" isn't available from state "ready"
InvalidTransitionError: Transition 'complete' isn't available from state 'ready'.


Custom transition code
Expand All @@ -100,23 +111,28 @@ Custom transition code
It is possible to define explicit code for a transition::

class MyObject(xworkflows.WorkflowEnabled):
state = MyWorkflow
state = MyWorkflow()

@xworkflows.transition()
def activate(self, user):
self.activated_by = user
print "State is %s" % self.state.name
print("State is %s" % self.state.name)

obj = MyObject()

When calling the transition, the custom code is called before updating the state:

.. sourcecode:: pycon

>>> obj.state
State('ready')
<StateWrapper: <State: 'init'>>
>>> obj.prepare()
>>> obj.state
<StateWrapper: <State: 'ready'>>
>>> obj.activate('blah')
State is ready
>>> obj.state
State('active')
<StateWrapper: <State: 'active'>>
>>> obj.activated_by
'blah'

Expand All @@ -128,7 +144,7 @@ Other functions can be hooked onto transitions, through the :func:`~xworkflows.b
:func:`~xworkflows.on_enter_state` and :func:`~xworkflows.on_leave_state` decorators::

class MyObject(xworkflows.WorkflowEnabled):
state = MyWorkflow
state = MyWorkflow()

@xworkflows.before_transition('foobar', 'gobaz')
def hook(self, *args, **kwargs):
Expand Down

0 comments on commit 7257a21

Please sign in to comment.