Create cache of graph to avoid generating it more than once

This commit is contained in:
Daniel Rojas 2021-10-15 16:37:09 +02:00
parent fb2aae5515
commit 65b8e36fe5

View File

@ -97,6 +97,7 @@ class Harness:
if to_name in self.connectors: if to_name in self.connectors:
self.connectors[to_name].activate_pin(to_pin, Side.LEFT) self.connectors[to_name].activate_pin(to_pin, Side.LEFT)
def create_graph(self) -> Graph: def create_graph(self) -> Graph:
dot = Graph() dot = Graph()
dot.body.append(f'// Graph generated by {APP_NAME} {__version__}') dot.body.append(f'// Graph generated by {APP_NAME} {__version__}')
@ -446,10 +447,20 @@ class Harness:
return dot return dot
# cache for the GraphViz Graph object
# do not access directly, use self.graph instead
_graph = None
@property
def graph(self):
if not self._graph: # no cached graph exists, generate one
self._graph = self.create_graph()
return self._graph # return cached graph
@property @property
def png(self): def png(self):
from io import BytesIO from io import BytesIO
graph = self.create_graph() graph = self.graph
data = BytesIO() data = BytesIO()
data.write(graph.pipe(format='png')) data.write(graph.pipe(format='png'))
data.seek(0) data.seek(0)
@ -458,7 +469,7 @@ class Harness:
@property @property
def svg(self): def svg(self):
from io import BytesIO from io import BytesIO
graph = self.create_graph() graph = self.graph
data = BytesIO() data = BytesIO()
data.write(graph.pipe(format='svg')) data.write(graph.pipe(format='svg'))
data.seek(0) data.seek(0)
@ -466,7 +477,7 @@ class Harness:
def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('html','png','svg','tsv')) -> None: def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('html','png','svg','tsv')) -> None:
# graphical output # graphical output
graph = self.create_graph() graph = self.graph
svg_already_exists = Path(f'{filename}.svg').exists() # if SVG already exists, do not delete later svg_already_exists = Path(f'{filename}.svg').exists() # if SVG already exists, do not delete later
# graphical output # graphical output
for f in fmt: for f in fmt: