Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add specs.Transform.

  • Loading branch information...
commit 7ac0a05b9ddb0605a207c994cd640004dc88ec69 1 parent f6b71f4
Samuel knipknap authored
7 README
... ... @@ -1,5 +1,5 @@
1 1 Spiff Workflow
2   ----------------
  2 +==============
3 3 Spiff Workflow is a library implementing a framework for workflows.
4 4 It is based on http://www.workflowpatterns.com and implemented in pure Python.
5 5
@@ -62,9 +62,10 @@ Hint: The examples are located in tests/data/spiff/.
62 62 10. Block Task to Sub-Workflow Decomposition [data/block_to_subworkflow.xml]
63 63 11. Sub-Workflow Decomposition to Block Task [data/subworkflow_to_block.xml]
64 64
65   - Other Patterns:
  65 + Specs that have no corresponding workflow pattern on workflowpatterns.com:
66 66
67   - 1. Execute - spawns a subprocess and waits for the results
  67 + Execute - spawns a subprocess and waits for the results
  68 + Transform - executes commands that can be used for data transforms
68 69
69 70
70 71 Contact
64 SpiffWorkflow/specs/Transform.py
... ... @@ -0,0 +1,64 @@
  1 +# Copyright (C) 2007 Samuel Abels
  2 +#
  3 +# This library is free software; you can redistribute it and/or
  4 +# modify it under the terms of the GNU Lesser General Public
  5 +# License as published by the Free Software Foundation; either
  6 +# version 2.1 of the License, or (at your option) any later version.
  7 +#
  8 +# This library is distributed in the hope that it will be useful,
  9 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  10 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11 +# Lesser General Public License for more details.
  12 +#
  13 +# You should have received a copy of the GNU Lesser General Public
  14 +# License along with this library; if not, write to the Free Software
  15 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16 +from SpiffWorkflow.Task import Task
  17 +from SpiffWorkflow.exceptions import WorkflowException
  18 +from SpiffWorkflow.specs.TaskSpec import TaskSpec
  19 +
  20 +class Transform(TaskSpec):
  21 + """
  22 + This class implements a task that transforms input/output data.
  23 + """
  24 +
  25 + def __init__(self, parent, name, transforms=None, **kwargs):
  26 + """
  27 + Constructor.
  28 +
  29 + @type parent: TaskSpec
  30 + @param parent: A reference to the parent task spec.
  31 + @type name: str
  32 + @param name: The name of the task spec.
  33 + @type transforms: list
  34 + @param transforms: The commands that this task will execute to
  35 + transform data. The commands will be executed using the
  36 + python 'exec' function. Accessing inputs and outputs is
  37 + achieved by referencing the my_task.* and self.*
  38 + variables'
  39 + @type kwargs: dict
  40 + @param kwargs: See L{SpiffWorkflow.specs.TaskSpec}.
  41 + """
  42 + assert parent is not None
  43 + assert name is not None
  44 + TaskSpec.__init__(self, parent, name, **kwargs)
  45 + self.transforms = transforms
  46 +
  47 +
  48 + def _on_complete_hook(self, my_task):
  49 + if self.transforms:
  50 + for transform in self.transforms:
  51 + exec(transform)
  52 + return TaskSpec._on_complete_hook(self, my_task)
  53 +
  54 + def serialize(self, serializer):
  55 + s_state = serializer._serialize_simple(self)
  56 + s_state['transforms'] = self.transforms
  57 + return s_state
  58 +
  59 + @classmethod
  60 + def deserialize(self, serializer, wf_spec, s_state):
  61 + spec = Transform(wf_spec, m_state['name'])
  62 + spec.transforms = s_state['transforms']
  63 + serializer._deserialize_simple(spec, s_state)
  64 + return spec
1  SpiffWorkflow/specs/__init__.py
@@ -14,6 +14,7 @@
14 14 from SubWorkflow import SubWorkflow
15 15 from ThreadMerge import ThreadMerge
16 16 from ThreadSplit import ThreadSplit
  17 +from Transform import Transform
17 18 from Trigger import Trigger
18 19 from WorkflowSpec import WorkflowSpec
19 20
45 tests/SpiffWorkflow/specs/TransformTest.py
... ... @@ -0,0 +1,45 @@
  1 +import sys, unittest, re, os
  2 +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
  3 +
  4 +from tests.SpiffWorkflow.util import run_workflow
  5 +from TaskSpecTest import TaskSpecTest
  6 +from SpiffWorkflow import Task
  7 +from SpiffWorkflow.specs import Transform, Simple
  8 +
  9 +class TransformTest(TaskSpecTest):
  10 + CORRELATE = Transform
  11 +
  12 + def create_instance(self):
  13 + return Transform(self.wf_spec,
  14 + 'testtask',
  15 + description = 'foo',
  16 + transforms = [''])
  17 +
  18 + def testPattern(self):
  19 + """
  20 + Tests that we can create a task that executes an shell command
  21 + and that the workflow can be called to complete such tasks.
  22 + """
  23 + task1 = Transform(self.wf_spec, 'First', transforms=[
  24 + "my_task.set_attribute(foo=1)"])
  25 + self.wf_spec.start.connect(task1)
  26 + task2 = Transform(self.wf_spec, 'Second', transforms=[
  27 + "my_task.set_attribute(foo=my_task.attributes['foo']+1)",
  28 + "my_task.set_attribute(copy=my_task.attributes['foo'])"
  29 + ])
  30 + task1.connect(task2)
  31 + task3 = Simple(self.wf_spec, 'Last')
  32 + task2.connect(task3)
  33 +
  34 + expected = 'Start\n First\n Second\n Last\n'
  35 + workflow = run_workflow(self, self.wf_spec, expected, '')
  36 + first = workflow.get_task(3)
  37 + last = workflow.get_task(5)
  38 + self.assertEqual(first.attributes.get('foo'), 1)
  39 + self.assertEqual(last.attributes.get('foo'), 2)
  40 + self.assertEqual(last.attributes.get('copy'), 2)
  41 +
  42 +def suite():
  43 + return unittest.TestLoader().loadTestsFromTestCase(TransformTest)
  44 +if __name__ == '__main__':
  45 + unittest.TextTestRunner(verbosity = 2).run(suite())

0 comments on commit 7ac0a05

Please sign in to comment.
Something went wrong with that request. Please try again.