Add a start of a few simple examples.

Change-Id: I6abd1798de6f2a778f874b8fa928772b94e7b588
This commit is contained in:
Joshua Harlow
2013-06-26 23:23:38 -07:00
committed by Joshua Harlow
parent a7482eaa18
commit 6c40c56c5b
4 changed files with 134 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
Documentation
=============
The start of documentation and examples about taskflow.
*WIP*
+52
View File
@@ -0,0 +1,52 @@
import logging
import os
import sys
logging.basicConfig(level=logging.ERROR)
if not os.path.isfile(os.path.join(os.getcwd(), "taskflow", '__init__.py')):
sys.path.insert(0, os.path.join(os.path.abspath(os.getcwd()), os.pardir))
else:
sys.path.insert(0, os.path.abspath(os.getcwd()))
from taskflow import decorators
from taskflow.patterns import linear_flow as lf
def undo_call(context, result, cause):
print("Calling %s and apologizing." % result)
@decorators.task(revert_with=undo_call)
def call_jim(context):
print("Calling jim.")
print("Context = %s" % (context))
return context['jim_number']
@decorators.task(revert_with=undo_call)
def call_joe(context):
print("Calling joe.")
print("Context = %s" % (context))
return context['joe_number']
def call_suzzie(context):
raise IOError("Suzzie not home right now.")
flow = lf.Flow("call-them")
flow.add(call_jim)
flow.add(call_joe)
flow.add(call_suzzie)
context = {
"joe_number": 444,
"jim_number": 555,
}
try:
flow.run(context)
except Exception as e:
print("Flow failed: %s" % (e))
+33
View File
@@ -0,0 +1,33 @@
import logging
import os
import sys
logging.basicConfig(level=logging.ERROR)
if not os.path.isfile(os.path.join(os.getcwd(), "taskflow", '__init__.py')):
sys.path.insert(0, os.path.join(os.path.abspath(os.getcwd()), os.pardir))
else:
sys.path.insert(0, os.path.abspath(os.getcwd()))
from taskflow.patterns import linear_flow as lf
def call_jim(context):
print("Calling jim.")
print("Context = %s" % (context))
def call_joe(context):
print("Calling joe.")
print("Context = %s" % (context))
flow = lf.Flow("call-them")
flow.add(call_jim)
flow.add(call_joe)
context = {
"joe_number": 444,
"jim_number": 555,
}
flow.run(context)
+43
View File
@@ -0,0 +1,43 @@
import logging
import os
import sys
logging.basicConfig(level=logging.ERROR)
if not os.path.isfile(os.path.join(os.getcwd(), "taskflow", '__init__.py')):
sys.path.insert(0, os.path.join(os.path.abspath(os.getcwd()), os.pardir))
else:
sys.path.insert(0, os.path.abspath(os.getcwd()))
from taskflow.patterns import linear_flow as lf
def call_jim(context):
print("Calling jim.")
print("Context = %s" % (context))
def call_joe(context):
print("Calling joe.")
print("Context = %s" % (context))
def flow_watch(context, flow, old_state):
print("%s moved from %s => %s" % (flow, old_state, flow.state))
def task_watch(context, state, flow, task, result=None):
print("%s of %s moved into state %s" % (task, flow, state))
flow = lf.Flow("call-them")
flow.add(call_jim)
flow.add(call_joe)
flow.task_listeners.append(task_watch)
flow.listeners.append(flow_watch)
context = {
"joe_number": 444,
"jim_number": 555,
}
flow.run(context)