Style cleanup, use f-strings

This commit is contained in:
LeoVerto 2018-11-27 03:22:10 +01:00
parent 25eb4942b0
commit b84d30ee18

View file

@ -7,8 +7,8 @@ from docker import Client
from graphviz import Graph from graphviz import Graph
# colorlover.scales["12"]["qual"]["Paired"] converted to hex strings # colorlover.scales["12"]["qual"]["Paired"] converted to hex strings
COLORS = ['#1f78b4', '#33a02c', '#e31a1c', '#ff7f00', '#6a3d9a', '#b15928', '#a6cee3', '#b2df8a', '#fdbf6f', COLORS = ["#1f78b4", "#33a02c", "#e31a1c", "#ff7f00", "#6a3d9a", "#b15928", "#a6cee3", "#b2df8a", "#fdbf6f",
'#cab2d6', '#ffff99'] "#cab2d6", "#ffff99"]
i = 0 i = 0
@ -19,12 +19,14 @@ def get_unique_color():
c = COLORS[i] c = COLORS[i]
i += 1 i += 1
else: else:
# Generate random color if we've already used the 12 preset ones
c = "#%06x".format(random.randint(0, 0xFFFFFF)) c = "#%06x".format(random.randint(0, 0xFFFFFF))
return c return c
def generate_graph(verbose: bool, file: str): def generate_graph(verbose: bool, file: str):
g = Graph(comment='Docker Network Graph', engine="sfdp", format='png', g = Graph(comment="Docker Network Graph", engine="sfdp", format="png",
graph_attr=dict(splines="true")) graph_attr=dict(splines="true"))
docker_client = Client(os.environ.get("DOCKER_HOST", "unix:///var/run/docker.sock")) docker_client = Client(os.environ.get("DOCKER_HOST", "unix:///var/run/docker.sock"))
@ -33,41 +35,42 @@ def generate_graph(verbose: bool, file: str):
print(json.dumps(obj, indent=4)) print(json.dumps(obj, indent=4))
for c in docker_client.containers(): for c in docker_client.containers():
name = c['Names'][0][1:] name = c["Names"][0][1:]
container_id = c['Id'] container_id = c["Id"]
node_id = 'container_%s' % container_id node_id = f"container_{container_id}"
iface_labels = [] iface_labels = []
for net_name, net_info in c['NetworkSettings']['Networks'].items(): for net_name, net_info in c["NetworkSettings"]["Networks"].items():
label_iface = "<%s> %s" % (net_info['EndpointID'], net_info['IPAddress']) label_iface = f"<{net_info['EndpointID']}> {net_info['IPAddress']}"
iface_labels.append(label_iface) iface_labels.append(label_iface)
labels = "|".join(iface_labels)
if verbose: if verbose:
print('|'.join(iface_labels)) print(labels)
g.node(node_id, g.node(node_id,
shape='record', shape="record",
label="{ %s | { %s } }" % (name, '|'.join(iface_labels)), label=f"{{ {name} | { {labels} } }}",
fillcolor='#ff9999', fillcolor="#ff9999",
style='filled' style="filled"
) )
for net in sorted(docker_client.networks(), key=lambda k: k["Name"]): for net in sorted(docker_client.networks(), key=lambda k: k["Name"]):
net_name = net['Name'] net_name = net["Name"]
color = get_unique_color() color = get_unique_color()
try: try:
gateway = net['IPAM']['Config'][0]['Gateway'] gateway = net["IPAM"]["Config"][0]["Gateway"]
except (KeyError, IndexError): except (KeyError, IndexError):
# This network doesn't seem to be used, skip it # This network doesn't seem to be used, skip it
continue continue
internal = "" internal = ""
try: try:
if net['Internal']: if net["Internal"]:
internal = "| Internal" internal = "| Internal"
except KeyError: except KeyError:
pass pass
@ -80,30 +83,30 @@ def generate_graph(verbose: bool, file: str):
pass pass
if verbose: if verbose:
print("Network: %s %s gw:%s" % (net_name, internal, gateway)) print(f"Network: {net_name} {internal} gw:{gateway}")
net_node_id = "net_%s" % (net_name,) net_node_id = f"net_{net_name}"
label = "{<gw_iface> %s | %s %s%s}" % (gateway, net_name, internal, isolated) label = f"{{<gw_iface> {gateway} | {net_name} {internal} {isolated}}}"
g.node(net_node_id, g.node(net_node_id,
shape='record', shape="record",
label=label, label=label,
fillcolor=color, fillcolor=color,
style='filled' style="filled"
) )
if net['Containers']: if net["Containers"]:
for container_id, container in sorted(net['Containers'].items()): for container_id, container in sorted(net["Containers"].items()):
if verbose: if verbose:
dump_json(container) dump_json(container)
print(" * ", container['Name'], container['IPv4Address'], container['IPv6Address']) print(" * ", container["Name"], container["IPv4Address"], container["IPv6Address"])
container_node_id = 'container_%s' % container_id container_node_id = f"container_{container_id}"
container_iface_ref = "%s:%s" % (container_node_id, container['EndpointID']) container_iface_ref = f"{container_node_id}:{container['EndpointID']}"
g.edge(container_iface_ref, net_node_id+":gw_iface", color=color) g.edge(container_iface_ref, f"{net_node_id}:gw_iface", color=color)
print(g.source) print(g.source)
if file: if file: