Merge pull request #93 from xarses/connections-graph-improvements
cli.py: connections graph start_with option
This commit is contained in:
commit
2d161ce0aa
@ -191,13 +191,42 @@ def init_cli_connections():
|
|||||||
|
|
||||||
@connections.command()
|
@connections.command()
|
||||||
def show():
|
def show():
|
||||||
print json.dumps(signals.CLIENTS, indent=2)
|
def format_resource_input(resource_name, resource_input_name):
|
||||||
|
return '{}::{}'.format(
|
||||||
|
#click.style(resource_name, fg='white', bold=True),
|
||||||
|
resource_name,
|
||||||
|
click.style(resource_input_name, fg='yellow')
|
||||||
|
)
|
||||||
|
|
||||||
|
def show_emitter_connections(emitter_name, destinations):
|
||||||
|
inputs = sorted(destinations)
|
||||||
|
|
||||||
|
for emitter_input in inputs:
|
||||||
|
click.echo(
|
||||||
|
'{} -> {}'.format(
|
||||||
|
format_resource_input(emitter_name, emitter_input),
|
||||||
|
'[{}]'.format(
|
||||||
|
', '.join(
|
||||||
|
format_resource_input(*r)
|
||||||
|
for r in destinations[emitter_input]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
keys = sorted(signals.CLIENTS)
|
||||||
|
for emitter_name in keys:
|
||||||
|
show_emitter_connections(emitter_name,
|
||||||
|
signals.CLIENTS[emitter_name])
|
||||||
|
|
||||||
# TODO: this requires graphing libraries
|
# TODO: this requires graphing libraries
|
||||||
@connections.command()
|
@connections.command()
|
||||||
def graph():
|
@click.option('--start-with', default=None)
|
||||||
|
@click.option('--end-with', default=None)
|
||||||
|
def graph(end_with, start_with):
|
||||||
#g = xs.connection_graph()
|
#g = xs.connection_graph()
|
||||||
g = signals.detailed_connection_graph()
|
g = signals.detailed_connection_graph(start_with=start_with,
|
||||||
|
end_with=end_with)
|
||||||
|
|
||||||
nx.write_dot(g, 'graph.dot')
|
nx.write_dot(g, 'graph.dot')
|
||||||
subprocess.call(['dot', '-Tpng', 'graph.dot', '-o', 'graph.png'])
|
subprocess.call(['dot', '-Tpng', 'graph.dot', '-o', 'graph.png'])
|
||||||
|
@ -225,38 +225,47 @@ def assign_connections(receiver, connections):
|
|||||||
def connection_graph():
|
def connection_graph():
|
||||||
resource_dependencies = {}
|
resource_dependencies = {}
|
||||||
|
|
||||||
for source, destination_values in CLIENTS.items():
|
for emitter_name, destination_values in CLIENTS.items():
|
||||||
resource_dependencies.setdefault(source, set())
|
resource_dependencies.setdefault(emitter_name, set())
|
||||||
for src, destinations in destination_values.items():
|
for emitter_input, receivers in destination_values.items():
|
||||||
resource_dependencies[source].update([
|
resource_dependencies[emitter_name].update(
|
||||||
destination[0] for destination in destinations
|
receiver[0] for receiver in receivers
|
||||||
])
|
)
|
||||||
|
|
||||||
g = nx.DiGraph()
|
g = nx.DiGraph()
|
||||||
|
|
||||||
# TODO: tags as graph node attributes
|
# TODO: tags as graph node attributes
|
||||||
for source, destinations in resource_dependencies.items():
|
for emitter_name, receivers in resource_dependencies.items():
|
||||||
g.add_node(source)
|
g.add_node(emitter_name)
|
||||||
g.add_nodes_from(destinations)
|
g.add_nodes_from(receivers)
|
||||||
g.add_edges_from(
|
g.add_edges_from(
|
||||||
itertools.izip(
|
itertools.izip(
|
||||||
itertools.repeat(source),
|
itertools.repeat(emitter_name),
|
||||||
destinations
|
receivers
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return g
|
return g
|
||||||
|
|
||||||
|
|
||||||
def detailed_connection_graph():
|
def detailed_connection_graph(start_with=None, end_with=None):
|
||||||
g = nx.MultiDiGraph()
|
g = nx.MultiDiGraph()
|
||||||
|
|
||||||
for emitter_name, destination_values in CLIENTS.items():
|
for emitter_name, destination_values in CLIENTS.items():
|
||||||
for emitter_input, receivers in CLIENTS[emitter_name].items():
|
for emitter_input, receivers in destination_values.items():
|
||||||
for receiver_name, receiver_input in receivers:
|
for receiver_name, receiver_input in receivers:
|
||||||
label = emitter_input
|
label = '{}:{}'.format(emitter_input, receiver_input)
|
||||||
if emitter_input != receiver_input:
|
|
||||||
label = '{}:{}'.format(emitter_input, receiver_input)
|
|
||||||
g.add_edge(emitter_name, receiver_name, label=label)
|
g.add_edge(emitter_name, receiver_name, label=label)
|
||||||
|
|
||||||
return g
|
ret = g
|
||||||
|
|
||||||
|
if start_with is not None:
|
||||||
|
ret = g.subgraph(
|
||||||
|
nx.dfs_postorder_nodes(ret, start_with)
|
||||||
|
)
|
||||||
|
if end_with is not None:
|
||||||
|
ret = g.subgraph(
|
||||||
|
nx.dfs_postorder_nodes(ret.reverse(), end_with)
|
||||||
|
)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
Loading…
x
Reference in New Issue
Block a user