| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
class GraphPage(object): |
| 4 |
"""Base class for the client-side content of one of the 'pages' |
| 5 |
(one graph) sent over to and displayed by the external process. |
| 6 |
""" |
| 7 |
save_tmp_file = None |
| 8 |
|
| 9 |
def __init__(self, *args, **kwds): |
| 10 |
self.args = args |
| 11 |
self.kwds = kwds |
| 12 |
|
| 13 |
def content(self): |
| 14 |
"""Compute the content of the page. |
| 15 |
This doesn't modify the page in place; it returns a new GraphPage. |
| 16 |
""" |
| 17 |
if hasattr(self, 'source'): |
| 18 |
return self |
| 19 |
else: |
| 20 |
new = self.__class__() |
| 21 |
new.source = '' |
| 22 |
new.links = {} |
| 23 |
new.compute(*self.args, **self.kwds) |
| 24 |
return new |
| 25 |
|
| 26 |
def followlink(self, word): |
| 27 |
raise KeyError |
| 28 |
|
| 29 |
def display(self): |
| 30 |
"Display a graph page." |
| 31 |
from dotviewer import graphclient, msgstruct |
| 32 |
try: |
| 33 |
graphclient.display_page(self, save_tmp_file=self.save_tmp_file) |
| 34 |
except msgstruct.RemoteError as e: |
| 35 |
import sys |
| 36 |
print >> sys.stderr, "Exception in the graph viewer:", str(e) |
| 37 |
|
| 38 |
def display_background(self): |
| 39 |
"Display a graph page in a background thread." |
| 40 |
try: |
| 41 |
import threading |
| 42 |
t = threading.Thread(target=self.display) |
| 43 |
t.start() |
| 44 |
except ImportError: |
| 45 |
self.display() |
| 46 |
|
| 47 |
class DotFileGraphPage(GraphPage): |
| 48 |
def compute(self, dotfile): |
| 49 |
import codecs |
| 50 |
from dotviewer.strunicode import RAW_ENCODING |
| 51 |
f = codecs.open(dotfile, 'r', RAW_ENCODING) |
| 52 |
self.source = f.read() |
| 53 |
f.close() |