|
1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """some basic stuff to build dot generated graph images""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 _ = unicode |
|
22 |
|
23 import tempfile |
|
24 import os |
|
25 |
|
26 from logilab.mtconverter import xml_escape |
|
27 from logilab.common.graph import GraphGenerator, DotBackend |
|
28 |
|
29 from cubicweb.view import EntityView |
|
30 from cubicweb.utils import make_uid |
|
31 |
|
32 class DotGraphView(EntityView): |
|
33 __abstract__ = True |
|
34 |
|
35 def cell_call(self, row, col): |
|
36 entity = self.cw_rset.get_entity(row, col) |
|
37 visitor = self.build_visitor(entity) |
|
38 prophdlr = self.build_dotpropshandler() |
|
39 graphname = 'dotgraph%s' % str(entity.eid) |
|
40 generator = GraphGenerator(DotBackend(graphname, None, |
|
41 ratio='compress', size='30,10')) |
|
42 # map file |
|
43 pmap, mapfile = tempfile.mkstemp(".map", graphname) |
|
44 os.close(pmap) |
|
45 # image file |
|
46 fd, tmpfile = tempfile.mkstemp('.png') |
|
47 os.close(fd) |
|
48 generator.generate(visitor, prophdlr, tmpfile, mapfile) |
|
49 filekeyid = make_uid() |
|
50 self._cw.session.data[filekeyid] = tmpfile |
|
51 self.w(u'<img src="%s" alt="%s" usemap="#%s" />' % ( |
|
52 xml_escape(entity.absolute_url(vid='tmppng', tmpfile=filekeyid)), |
|
53 xml_escape(self._cw._('Data connection graph for %s') % entity.name), |
|
54 graphname)) |
|
55 stream = open(mapfile, 'r').read() |
|
56 stream = stream.decode(self._cw.encoding) |
|
57 self.w(stream) |
|
58 os.unlink(mapfile) |
|
59 |
|
60 def build_visitor(self, entity): |
|
61 raise NotImplementedError |
|
62 |
|
63 def build_dotpropshandler(self): |
|
64 return DotGraphPropsHandler(self._cw) |
|
65 |
|
66 |
|
67 class DotPropsHandler(object): |
|
68 def __init__(self, req): |
|
69 self._ = req._ |
|
70 |
|
71 def node_properties(self, entity): |
|
72 """return default DOT drawing options for a state or transition""" |
|
73 return {'label': entity.printable_value('name'), |
|
74 'href': entity.absolute_url(), |
|
75 'fontname': 'Courier', 'fontsize': 10, 'shape':'box', |
|
76 } |
|
77 |
|
78 def edge_properties(self, transition, fromstate, tostate): |
|
79 return {'label': '', 'dir': 'forward', |
|
80 'color': 'black', 'style': 'filled'} |