Use constants for link metadata keys

Instead of using strings (which can be easy to
mistype and get wrong), provide a set of constants
that can be used to attach and use these keys in
flows and at compilation.

This also helps make it more clear what the keys
do and where they are used.

Change-Id: I5283b27617961136a4582bbcfce4617f05e8dd1d
This commit is contained in:
Joshua Harlow 2014-07-19 16:52:13 -07:00
parent 8d1b936239
commit 3a8a78ee64
4 changed files with 27 additions and 14 deletions

View File

@ -28,6 +28,11 @@ from taskflow.utils import misc
LOG = logging.getLogger(__name__)
_RETRY_EDGE_DATA = {
flow.LINK_RETRY: True,
}
_EDGE_INVARIANTS = (flow.LINK_INVARIANT, flow.LINK_MANUAL, flow.LINK_RETRY)
class Compilation(object):
"""The result of a compilers compile() is this *immutable* object."""
@ -45,11 +50,6 @@ class Compilation(object):
return self._hierarchy
_RETRY_EDGE_DATA = {
'retry': True,
}
class PatternCompiler(object):
"""Compiles a pattern (or task) into a compilation unit."""
@ -110,8 +110,8 @@ class PatternCompiler(object):
# Add association for each node of graph that has no existing retry.
for n in graph.nodes_iter():
if n is not retry and 'retry' not in graph.node[n]:
graph.node[n]['retry'] = retry
if n is not retry and flow.LINK_RETRY not in graph.node[n]:
graph.node[n][flow.LINK_RETRY] = retry
def _flatten_task(self, task, parent):
"""Flattens a individual task."""
@ -143,7 +143,7 @@ class PatternCompiler(object):
for (u, v, attrs) in flow.iter_links():
u_g = subgraphs[u]
v_g = subgraphs[v]
if any(attrs.get(k) for k in ('invariant', 'manual', 'retry')):
if any(attrs.get(k) for k in _EDGE_INVARIANTS):
# Connect nodes with no predecessors in v to nodes with
# no successors in u (thus maintaining the edge dependency).
self._add_new_edges(graph,
@ -151,7 +151,7 @@ class PatternCompiler(object):
v_g.no_predecessors_iter(),
edge_attrs=attrs)
else:
# This is dependency-only edge, connect corresponding
# This is symbol dependency edge, connect corresponding
# providers and consumers.
for provider in u_g:
for consumer in v_g:

View File

@ -20,6 +20,19 @@ import six
from taskflow.utils import reflection
# Link metadata keys that have inherent/special meaning.
#
# This key denotes the link is an invariant that ensures the order is
# correctly preserved.
LINK_INVARIANT = 'invariant'
# This key denotes the link is a manually/user-specified.
LINK_MANUAL = 'manual'
# This key denotes the link was created when resolving/compiling retries.
LINK_RETRY = 'retry'
# This key denotes the link was created due to symbol constraints and the
# value will be a set of names that the constraint ensures are satisfied.
LINK_REASONS = 'reasons'
@six.add_metaclass(abc.ABCMeta)
class Flow(object):

View File

@ -75,11 +75,11 @@ class Flow(flow.Flow):
if not attrs:
attrs = {}
if manual:
attrs['manual'] = True
attrs[flow.LINK_MANUAL] = True
if reason is not None:
if 'reasons' not in attrs:
attrs['reasons'] = set()
attrs['reasons'].add(reason)
if flow.LINK_REASONS not in attrs:
attrs[flow.LINK_REASONS] = set()
attrs[flow.LINK_REASONS].add(reason)
if not mutable_graph:
graph = gr.DiGraph(graph)
graph.add_edge(u, v, **attrs)

View File

@ -17,7 +17,7 @@
from taskflow import flow
_LINK_METADATA = {'invariant': True}
_LINK_METADATA = {flow.LINK_INVARIANT: True}
class Flow(flow.Flow):