Tweak functor used to find flatteners/storage routines

Make both of these finding functions use similar routines
that the utility module now provides since the logic that
both use can be shared.

Change-Id: Ib941b99945d42f5c0d791e9b2a0696d0e62a2388
This commit is contained in:
Joshua Harlow
2015-03-02 18:49:17 -08:00
parent d4f02f6fea
commit b598897d15
5 changed files with 34 additions and 36 deletions

View File

@@ -20,7 +20,6 @@ import threading
from taskflow import exceptions as exc
from taskflow import flow
from taskflow import logging
from taskflow import retry
from taskflow import task
from taskflow.types import graph as gr
from taskflow.types import tree as tr
@@ -281,34 +280,22 @@ class PatternCompiler(object):
self._freeze = freeze
self._lock = threading.Lock()
self._compilation = None
self._flatten_matchers = [
((flow.Flow,), self._flatten_flow),
((task.BaseTask,), self._flatten_task),
]
def _flatten(self, item, parent):
"""Flattens a item (pattern, task) into a graph + tree node."""
functor = self._find_flattener(item, parent)
functor = misc.match_type_handler(item, self._flatten_matchers)
if not functor:
raise TypeError("Unknown item '%s' (%s) requested to flatten"
% (item, type(item)))
self._pre_item_flatten(item)
graph, node = functor(item, parent)
self._post_item_flatten(item, graph, node)
return graph, node
def _find_flattener(self, item, parent):
"""Locates the flattening function to use to flatten the given item."""
if isinstance(item, flow.Flow):
return self._flatten_flow
elif isinstance(item, task.BaseTask):
return self._flatten_task
elif isinstance(item, retry.Retry):
if parent is None:
raise TypeError("Retry controller '%s' (%s) must only be used"
" as a flow constructor parameter and not as a"
" root component" % (item, type(item)))
else:
raise TypeError("Retry controller '%s' (%s) must only be used"
" as a flow constructor parameter and not as a"
" flow added component" % (item, type(item)))
else:
raise TypeError("Unknown item '%s' (%s) requested to flatten"
% (item, type(item)))
def _connect_retry(self, retry, graph):
graph.add_node(retry)