Skip to content

Commit

Permalink
MNT: directly write DOT and call dot
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyf committed Apr 21, 2024
1 parent dae05bb commit 44711ad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
28 changes: 26 additions & 2 deletions promela/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import ctypes
import pprint
import subprocess as _sbp

import networkx as nx
from networkx.utils import misc
Expand Down Expand Up @@ -1042,5 +1043,28 @@ def dump_graph(g, fname='a.pdf', node_label='label',
label = d.get(edge_label, ' ')
label = f'"{label}"'
h.add_edge(u, v, label=label)
pd = nx.drawing.nx_pydot.to_pydot(h)
pd.write_pdf(fname)
# format as DOT
nodes = list()
for u, d in h.nodes(data=True):
label = d['label']
nodes.append(
f'{u} [label={label}];')
nodes_dot = '\n'.join(nodes)
edges = list()
for u, v, d in h.edges(data=True):
label = d['label']
edges.append(
f'{u} -> {v} [label={label}];')
edges_dot = '\n'.join(edges)
dot_text = f'digraph {{ {nodes_dot}\n{edges_dot} }}'
# write as PDF
dot = [
'dot',
'-Tpdf',
f'-o{fname}']
_sbp.run(
dot,
encoding='utf8',
input=dot_text,
capture_output=True,
check=True)
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
PYTHON_REQUIRES = '>=3.9'
INSTALL_REQUIRES = [
'networkx >= 2.0',
'ply >= 3.4, <= 3.10',
'pydot >= 1.1.0']
'ply >= 3.4, <= 3.10']
CLASSIFIERS = [
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Science/Research',
Expand Down

0 comments on commit 44711ad

Please sign in to comment.