Skip to content

Commit

Permalink
Throw an exception on error
Browse files Browse the repository at this point in the history
  • Loading branch information
fsimonis committed Sep 24, 2024
1 parent 30f838c commit e12baff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
8 changes: 6 additions & 2 deletions preciceconfigvisualizer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pydot
import os

from preciceconfigvisualizer.common import configFileToDotCode
from preciceconfigvisualizer.common import configFileToDotCode, VisualizerException


SUPPORTED_FORMATS = [
Expand Down Expand Up @@ -79,7 +79,11 @@ def parse_args():

def main() -> None:
args = parse_args()
dot: str = configFileToDotCode(args.infile, **vars(args))
try:
dot: str = configFileToDotCode(args.infile, **vars(args))
except VisualizerException as e:
print(f"Failed to visualize config: {e.args[0]}", file=sys.stderr)
sys.exit(1)

ext: str = os.path.splitext(args.outfile.name)[1].lower().lstrip(".")
data: bytes
Expand Down
52 changes: 37 additions & 15 deletions preciceconfigvisualizer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
" with python {}.{}".format(sys.version_info.major.sys.version_info.minor)
)


class VisualizerException(Exception):
pass


VISIBILITY_TYPE = Literal["full", "merged", "hide"]


Expand Down Expand Up @@ -159,13 +164,30 @@ def configToGraph(
meshes = {}
meshDims = {}
participantClusterName = {}

def lookupParticipantClusterFor(name):
if name not in participantClusterName:
raise VisualizerException(
f"The participant {name} wasn't found. Known participants are: "
+ ", ".join(participantClusterName.keys())
)
return participantClusterName[name]

m2nCluster = pydot.Cluster("m2n", label=quote("Communicators"))
g.add_subgraph(m2nCluster)
cplCluster = pydot.Cluster("cpl", label=quote("Coupling Schemes"))
g.add_subgraph(cplCluster)

participantColor = getParticipantColor(root, colors)

def lookupParticipantColor(name):
if name not in participantColor:
raise VisualizerException(
f"The participant {name} wasn't found. Known participants are: "
+ ", ".join(participantColor.keys())
)
return participantColor[name]

for elem in findAllWithPrefix(root, "data"):
kind = elem.tag[elem.tag.find(":") + 1 :]
name = elem.attrib["name"]
Expand All @@ -180,7 +202,7 @@ def configToGraph(
name = elem.attrib["name"]
participant = pydot.Cluster(name, label=quote(name), style="bold")
participantClusterName[name] = participant.get_name()
color = participantColor[name]
color = lookupParticipantColor(name)
addNode(participant, name, color=color, shape="doubleoctagon")
# use-mesh
for use in elem.findall("use-mesh"):
Expand Down Expand Up @@ -208,7 +230,7 @@ def configToGraph(
meshname,
shape="cylinder",
label=label,
color=participantColor[pfrom],
color=lookupParticipantColor(pfrom),
style="dashed",
)
# provide-mesh
Expand Down Expand Up @@ -237,7 +259,7 @@ def configToGraph(
meshname,
shape="cylinder",
label=quote(f"{mesh}\nfrom {pfrom}"),
color=participantColor[pfrom],
color=lookupParticipantColor(pfrom),
style="dashed",
)
# read-data
Expand Down Expand Up @@ -331,25 +353,25 @@ def configToGraph(
g,
name,
pto,
lhead=participantClusterName[pto],
lhead=lookupParticipantClusterFor(pto),
dir="both",
color=participantColor[pto],
color=lookupParticipantColor(pto),
)
addEdge(
g,
name,
pfrom,
lhead=participantClusterName[pfrom],
lhead=lookupParticipantClusterFor(pfrom),
dir="both",
color=participantColor[pfrom],
color=lookupParticipantColor(pfrom),
)
if communicators == "merged":
addEdge(
g,
pfrom,
pto,
lhead=participantClusterName[pto],
ltail=participantClusterName[pfrom],
lhead=lookupParticipantClusterFor(pto),
ltail=lookupParticipantClusterFor(pfrom),
label=quote(kind),
dir="both",
)
Expand All @@ -370,8 +392,8 @@ def configToGraph(
g,
name,
thisName,
lhead=participantClusterName[thisName],
color=participantColor[thisName],
lhead=lookupParticipantClusterFor(thisName),
color=lookupParticipantColor(thisName),
)
if other.get("control"):
e.set_taillabel("Controller")
Expand All @@ -398,15 +420,15 @@ def configToGraph(
first,
lhead=participantClusterName[first],
taillabel=quote("first"),
color=participantColor[first],
color=lookupParticipantColor(first),
)
addEdge(
g,
name,
second,
lhead=participantClusterName[second],
taillabel=quote("second"),
color=participantColor[second],
color=lookupParticipantColor(second),
)
elif cplschemes == "merged":
addUniqueEdge(
Expand All @@ -429,7 +451,7 @@ def configToGraph(
init = isTrue(exchange.get("initialize", "no"))
withSubsteps = isTrue(exchange.get("substeps", "no"))
if data_exchange == "full":
pcolor = participantColor[pfrom]
pcolor = lookupParticipantColor(pfrom)
style = "bold" if init else ""
tooltip = dataType[data] + (" initialized" if init else "")
color = f"{pcolor}:invis:{pcolor}" if withSubsteps else pcolor
Expand All @@ -447,7 +469,7 @@ def configToGraph(
g,
f"{pfrom}-{mesh}",
f"{pto}-{mesh}",
color=participantColor[pfrom],
color=lookupParticipantColor(pfrom),
)

return g
Expand Down

0 comments on commit e12baff

Please sign in to comment.