web/views/dotgraphview.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 26 Aug 2010 11:35:02 +0200
branchstable
changeset 6160 12038ca95f0f
parent 6095 09a404123f0f
child 6405 6989f0e02542
permissions -rw-r--r--
Added tag cubicweb-version-3.9.5 for changeset 0a1fce8ddc67
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
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    34
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    35
    def cell_call(self, row, col):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    36
        entity = self.cw_rset.get_entity(row, col)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    37
        visitor = self.build_visitor(entity)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    38
        prophdlr = self.build_dotpropshandler()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    39
        graphname = 'dotgraph%s' % str(entity.eid)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    40
        generator = GraphGenerator(DotBackend(graphname, None,
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    41
                                              ratio='compress', size='30,10'))
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    42
        # map file
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    43
        pmap, mapfile = tempfile.mkstemp(".map", graphname)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    44
        os.close(pmap)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    45
        # image file
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    46
        fd, tmpfile = tempfile.mkstemp('.png')
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    47
        os.close(fd)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    48
        generator.generate(visitor, prophdlr, tmpfile, mapfile)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    49
        filekeyid = make_uid()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    50
        self._cw.session.data[filekeyid] = tmpfile
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    51
        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
    52
            xml_escape(entity.absolute_url(vid='tmppng', tmpfile=filekeyid)),
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    53
            xml_escape(self._cw._('Data connection graph for %s') % entity.name),
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    54
            graphname))
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    55
        stream = open(mapfile, 'r').read()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    56
        stream = stream.decode(self._cw.encoding)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    57
        self.w(stream)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    58
        os.unlink(mapfile)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    59
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    60
    def build_visitor(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    61
        raise NotImplementedError
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    62
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    63
    def build_dotpropshandler(self):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    64
        return DotGraphPropsHandler(self._cw)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    65
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
class DotPropsHandler(object):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    68
    def __init__(self, req):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    69
        self._ = req._
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    70
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    71
    def node_properties(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    72
        """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
    73
        return {'label': entity.dc_title(),
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    74
                'href': entity.absolute_url(),
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    75
                'fontname': 'Courier', 'fontsize': 10, 'shape':'box',
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    76
                 }
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
    def edge_properties(self, transition, fromstate, tostate):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    79
        return {'label': '', 'dir': 'forward',
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    80
                'color': 'black', 'style': 'filled'}