Files
deb-python-taskflow/taskflow/patterns/linear_flow.py
Joshua Harlow 45701e8fe3 Have the linear workflow verify the tasks inputs.
When a task is added ensure that the previous task, if
it exists creates the neccasary outputs for the task to
be added. If it does not then throw an exception.
2013-05-21 22:49:53 -07:00

57 lines
2.1 KiB
Python

# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from taskflow import exceptions as exc
from taskflow.patterns import ordered_flow
class Flow(ordered_flow.Flow):
"""A linear chain of tasks that can be applied as one unit or
rolled back as one unit. Each task in the chain may have requirements
which are satisfied by the previous task in the chain."""
def __init__(self, name, tolerant=False, parents=None):
super(Flow, self).__init__(name, tolerant, parents)
self._tasks = []
def _validate_provides(self, task):
last_provides = set()
last_provider = None
if self._tasks:
last_provider = self._tasks[-1]
last_provides = last_provider.provides()
# Ensure that the last task provides all the needed input for this
# task to run correctly.
req_diff = task.requires().difference(last_provides)
if req_diff:
if last_provider is None:
msg = ("There is no previous task providing the outputs %s"
" for %s to correctly execute.") % (req_diff, task)
else:
msg = ("%s does not provide the needed outputs %s for %s to"
" correctly execute.")
msg = msg % (last_provider, req_diff, task)
raise exc.InvalidStateException(msg)
def add(self, task):
self._validate_provides(task)
self._tasks.append(task)
def order(self):
return list(self._tasks)