from pypy.translator.tool.make_dot import DotGen from dotviewer.graphclient import display_page class Graph(DotGen): def highlight(self, word, text, linked_to=None): if not hasattr(self, '_links'): self._links = {} self._links_to = {} self._links[word] = text if linked_to: self._links_to[word] = linked_to def display(self): "Display a graph page locally." display_page(_Page(self)) class NoGraph(Exception): pass class _Page: def __init__(self, graph_builder): if callable(graph_builder): graph = graph_builder() else: graph = graph_builder if graph is None: raise NoGraph self.graph_builder = graph_builder def content(self): return _PageContent(self.graph_builder) class _PageContent: def __init__(self, graph_builder): if callable(graph_builder): graph = graph_builder() else: graph = graph_builder assert graph is not None self.graph_builder = graph_builder self.graph = graph self.links = getattr(graph, '_links', {}) if not hasattr(graph, '_source'): graph._source = graph.generate(target=None) self.source = graph._source def followlink(self, link): try: return _Page(self.graph._links_to[link]) except NoGraph: return _Page(self.graph_builder) # ____________________________________________________________ def test_graph(): g1 = Graph('test') g2 = Graph('test2') g1.emit_node('a', shape="box", label="a") g1.emit_node('b', shape="box", label="b") g1.emit_node('c', shape="box", label="c") g1.emit_edge('a', 'b', 'ablink') g1.emit_edge('a', 'c', 'aclink') g1.highlight('b', 'This is b.', g2) g2.emit_node('mini', shape="box", label="mini") g1.display() if __name__ == '__main__': test_graph()