|
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 from cubicweb import _ |
|
22 |
|
23 import tempfile |
|
24 import os |
|
25 import codecs |
|
26 |
|
27 from logilab.mtconverter import xml_escape |
|
28 from logilab.common.graph import GraphGenerator, DotBackend |
|
29 |
|
30 from cubicweb.view import EntityView |
|
31 from cubicweb.utils import make_uid |
|
32 |
|
33 class DotGraphView(EntityView): |
|
34 __abstract__ = True |
|
35 backend_class = DotBackend |
|
36 backend_kwargs = {'ratio': 'compress', 'size': '30,10'} |
|
37 |
|
38 def cell_call(self, row, col): |
|
39 if 'MSIE 8' in self._cw.useragent(): |
|
40 return |
|
41 entity = self.cw_rset.get_entity(row, col) |
|
42 visitor = self.build_visitor(entity) |
|
43 prophdlr = self.build_dotpropshandler() |
|
44 graphname = 'dotgraph%s' % str(entity.eid) |
|
45 generator = GraphGenerator(self.backend_class(graphname, None, |
|
46 **self.backend_kwargs)) |
|
47 # image file |
|
48 fd, tmpfile = tempfile.mkstemp('.svg') |
|
49 os.close(fd) |
|
50 generator.generate(visitor, prophdlr, tmpfile) |
|
51 with codecs.open(tmpfile, 'rb', encoding='utf-8') as svgfile: |
|
52 self.w(svgfile.read()) |
|
53 |
|
54 def build_visitor(self, entity): |
|
55 raise NotImplementedError |
|
56 |
|
57 def build_dotpropshandler(self): |
|
58 return DotPropsHandler(self._cw) |
|
59 |
|
60 |
|
61 class DotPropsHandler(object): |
|
62 def __init__(self, req): |
|
63 self._ = req._ |
|
64 |
|
65 def node_properties(self, entity): |
|
66 """return default DOT drawing options for a state or transition""" |
|
67 return {'label': entity.dc_long_title(), |
|
68 'href': entity.absolute_url(), |
|
69 'fontname': 'Courier', 'fontsize': 10, 'shape':'box', |
|
70 } |
|
71 |
|
72 def edge_properties(self, transition, fromstate, tostate): |
|
73 return {'label': '', 'dir': 'forward', |
|
74 'color': 'black', 'style': 'filled'} |