web/views/dotgraphview.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 10 Mar 2011 11:56:35 +0100
branchstable
changeset 7067 f239da8e7c39
parent 6405 6989f0e02542
child 8212 a045d3821990
permissions -rw-r--r--
[tabs] forward url parameters to lazy tabs views If http://www.cubicweb.org/project/foo?x=12&y=13 dislays a LazyTabView, this patch forwards {x: 12, y: 13} to subsequent lazy tabs (i.e. called through ajax queries).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     3
#
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     5
#
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
     9
# any later version.
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    10
#
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    14
# details.
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    15
#
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    18
"""some basic stuff to build dot generated graph images"""
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    19
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    21
_ = unicode
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    22
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    23
import tempfile
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    24
import os
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    25
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    26
from logilab.mtconverter import xml_escape
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    27
from logilab.common.graph import GraphGenerator, DotBackend
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    28
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    29
from cubicweb.view import EntityView
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    30
from cubicweb.utils import make_uid
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    31
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    32
class DotGraphView(EntityView):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    33
    __abstract__ = True
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    34
    backend_class = DotBackend
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    35
    backend_kwargs = {'ratio': 'compress', 'size': '30,10'}
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    36
    def cell_call(self, row, col):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    37
        entity = self.cw_rset.get_entity(row, col)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    38
        visitor = self.build_visitor(entity)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    39
        prophdlr = self.build_dotpropshandler()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    40
        graphname = 'dotgraph%s' % str(entity.eid)
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    41
        generator = GraphGenerator(self.backend_class(graphname, None,
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    42
                                                      **self.backend_kwargs))
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    43
        # map file
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    44
        pmap, mapfile = tempfile.mkstemp(".map", graphname)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    45
        os.close(pmap)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    46
        # image file
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    47
        fd, tmpfile = tempfile.mkstemp('.png')
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    48
        os.close(fd)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    49
        generator.generate(visitor, prophdlr, tmpfile, mapfile)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    50
        filekeyid = make_uid()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    51
        self._cw.session.data[filekeyid] = tmpfile
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    52
        self.w(u'<img src="%s" alt="%s" usemap="#%s" />' % (
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    53
            xml_escape(entity.absolute_url(vid='tmppng', tmpfile=filekeyid)),
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    54
            xml_escape(self._cw._('Data connection graph for %s') % entity.dc_title()),
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    55
            graphname))
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    56
        stream = open(mapfile, 'r').read()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    57
        stream = stream.decode(self._cw.encoding)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    58
        self.w(stream)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    59
        os.unlink(mapfile)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    60
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    61
    def build_visitor(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    62
        raise NotImplementedError
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    63
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    64
    def build_dotpropshandler(self):
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    65
        return DotPropsHandler(self._cw)
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    66
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    67
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    68
class DotPropsHandler(object):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    69
    def __init__(self, req):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    70
        self._ = req._
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    71
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    72
    def node_properties(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    73
        """return default DOT drawing options for a state or transition"""
6095
09a404123f0f [dot] oops, should use dc_title here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6093
diff changeset
    74
        return {'label': entity.dc_title(),
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    75
                'href': entity.absolute_url(),
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    76
                'fontname': 'Courier', 'fontsize': 10, 'shape':'box',
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    77
                 }
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    78
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    79
    def edge_properties(self, transition, fromstate, tostate):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    80
        return {'label': '', 'dir': 'forward',
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    81
                'color': 'black', 'style': 'filled'}