Added DevState
This commit is contained in:
parent
d814ffbc6b
commit
9a16377f30
1 changed files with 47 additions and 0 deletions
47
state/devstate.py
Normal file
47
state/devstate.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from .pystate import PyState
|
||||
|
||||
|
||||
class DevState(PyState):
|
||||
"""
|
||||
Version of PyState that allows dumping to .dot files
|
||||
+ node id's are generated sequentially to make writing tests easier
|
||||
"""
|
||||
|
||||
free_id = 0
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
@staticmethod
|
||||
def new_id() -> str:
|
||||
DevState.free_id += 1
|
||||
return str(DevState.free_id - 1)
|
||||
|
||||
def dump(self, path: str, png_path: str = None):
|
||||
"""Dumps the whole MV graph to a graphviz .dot-file
|
||||
|
||||
Args:
|
||||
path (str): path for .dot-file
|
||||
png_path (str, optional): path for .png image generated from the .dot-file. Defaults to None.
|
||||
"""
|
||||
with open(path, "w") as f:
|
||||
f.write("digraph main {\n")
|
||||
for n in sorted(self.nodes):
|
||||
if n in self.values:
|
||||
f.write("\"a_%s\" [label=\"%s\"];\n" % (
|
||||
n, str(self.values[n]).replace('"', '\\"')))
|
||||
else:
|
||||
f.write("\"a_%s\" [label=\"\"];\n" % n)
|
||||
for i, e in sorted(list(self.edges.items())):
|
||||
f.write("\"a_%s\" [label=\"e_%s\" shape=point];\n" % (i, i))
|
||||
f.write("\"a_%s\" -> \"a_%s\" [arrowhead=none];\n" % (e[0], i))
|
||||
f.write("\"a_%s\" -> \"a_%s\";\n" % (i, e[1]))
|
||||
f.write("}")
|
||||
|
||||
if png_path is not None:
|
||||
# generate png from dot-file
|
||||
bashCommand = f"dot -Tpng {path} -o {png_path}"
|
||||
import subprocess
|
||||
process = subprocess.Popen(
|
||||
bashCommand.split(), stdout=subprocess.PIPE)
|
||||
output, error = process.communicate()
|
||||
Loading…
Add table
Add a link
Reference in a new issue