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()
|
||||
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
|
||||
@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 = signals.detailed_connection_graph()
|
||||
g = signals.detailed_connection_graph(start_with=start_with,
|
||||
end_with=end_with)
|
||||
|
||||
nx.write_dot(g, 'graph.dot')
|
||||
subprocess.call(['dot', '-Tpng', 'graph.dot', '-o', 'graph.png'])
|
||||
|
@ -225,38 +225,47 @@ def assign_connections(receiver, connections):
|
||||
def connection_graph():
|
||||
resource_dependencies = {}
|
||||
|
||||
for source, destination_values in CLIENTS.items():
|
||||
resource_dependencies.setdefault(source, set())
|
||||
for src, destinations in destination_values.items():
|
||||
resource_dependencies[source].update([
|
||||
destination[0] for destination in destinations
|
||||
])
|
||||
for emitter_name, destination_values in CLIENTS.items():
|
||||
resource_dependencies.setdefault(emitter_name, set())
|
||||
for emitter_input, receivers in destination_values.items():
|
||||
resource_dependencies[emitter_name].update(
|
||||
receiver[0] for receiver in receivers
|
||||
)
|
||||
|
||||
g = nx.DiGraph()
|
||||
|
||||
# TODO: tags as graph node attributes
|
||||
for source, destinations in resource_dependencies.items():
|
||||
g.add_node(source)
|
||||
g.add_nodes_from(destinations)
|
||||
for emitter_name, receivers in resource_dependencies.items():
|
||||
g.add_node(emitter_name)
|
||||
g.add_nodes_from(receivers)
|
||||
g.add_edges_from(
|
||||
itertools.izip(
|
||||
itertools.repeat(source),
|
||||
destinations
|
||||
itertools.repeat(emitter_name),
|
||||
receivers
|
||||
)
|
||||
)
|
||||
|
||||
return g
|
||||
|
||||
|
||||
def detailed_connection_graph():
|
||||
def detailed_connection_graph(start_with=None, end_with=None):
|
||||
g = nx.MultiDiGraph()
|
||||
|
||||
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:
|
||||
label = emitter_input
|
||||
if emitter_input != receiver_input:
|
||||
label = '{}:{}'.format(emitter_input, receiver_input)
|
||||
label = '{}:{}'.format(emitter_input, receiver_input)
|
||||
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