Skip to content

Commit

Permalink
Create cache of graph to avoid generating it more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Oct 15, 2021
1 parent 304cbc9 commit 188772f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def connect(self, from_name: str, from_pin: (int, str), via_name: str, via_wire:
if to_name in self.connectors:
self.connectors[to_name].activate_pin(to_pin)


def create_graph(self) -> Graph:
dot = Graph()
dot.body.append(f'// Graph generated by {APP_NAME} {__version__}')
Expand Down Expand Up @@ -457,10 +458,20 @@ def typecheck(name: str, value: Any, expect: type) -> None:

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
def png(self):
from io import BytesIO
graph = self.create_graph()
graph = self.graph
data = BytesIO()
data.write(graph.pipe(format='png'))
data.seek(0)
Expand All @@ -469,15 +480,15 @@ def png(self):
@property
def svg(self):
from io import BytesIO
graph = self.create_graph()
graph = self.graph
data = BytesIO()
data.write(graph.pipe(format='svg'))
data.seek(0)
return data.read()

def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('html','png','svg','tsv')) -> None:
# 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
# graphical output
for f in fmt:
Expand Down

0 comments on commit 188772f

Please sign in to comment.