web/views/dotgraphview.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 10 Jun 2014 09:49:45 +0200
changeset 9770 112c884b2d8d
parent 9383 7b3fa5a7a5a3
child 10666 7f6b5f023884
permissions -rw-r--r--
merge 3.18.5 into 3.19 branch
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
9383
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    25
import codecs
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    26
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    27
from logilab.mtconverter import xml_escape
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    28
from logilab.common.graph import GraphGenerator, DotBackend
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    29
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    30
from cubicweb.view import EntityView
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    31
from cubicweb.utils import make_uid
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    32
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    33
class DotGraphView(EntityView):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    34
    __abstract__ = True
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    35
    backend_class = DotBackend
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    36
    backend_kwargs = {'ratio': 'compress', 'size': '30,10'}
9383
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    37
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    38
    def cell_call(self, row, col):
9383
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    39
        if 'MSIE 8' in self._cw.useragent():
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    40
            return
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    41
        entity = self.cw_rset.get_entity(row, col)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    42
        visitor = self.build_visitor(entity)
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    43
        prophdlr = self.build_dotpropshandler()
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    44
        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
    45
        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
    46
                                                      **self.backend_kwargs))
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    47
        # image file
9383
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    48
        fd, tmpfile = tempfile.mkstemp('.svg')
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    49
        os.close(fd)
9383
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    50
        generator.generate(visitor, prophdlr, tmpfile)
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    51
        with codecs.open(tmpfile, 'rb', encoding='utf-8') as svgfile:
7b3fa5a7a5a3 [views/dot] use an inlined svg for schema and workflow graphs
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8212
diff changeset
    52
            self.w(svgfile.read())
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    53
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    54
    def build_visitor(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    55
        raise NotImplementedError
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    56
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    57
    def build_dotpropshandler(self):
6405
6989f0e02542 fix some issues in DotGraphView, and enhance reusability
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6095
diff changeset
    58
        return DotPropsHandler(self._cw)
6093
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
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    61
class DotPropsHandler(object):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    62
    def __init__(self, req):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    63
        self._ = req._
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    64
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    65
    def node_properties(self, entity):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    66
        """return default DOT drawing options for a state or transition"""
8212
a045d3821990 [workflow view] Display a non-localized version of state/transition names (closes #1912537).
Damien Garaud <damien.garaud@logilab.fr>
parents: 6405
diff changeset
    67
        return {'label': entity.dc_long_title(),
6093
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    68
                'href': entity.absolute_url(),
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    69
                'fontname': 'Courier', 'fontsize': 10, 'shape':'box',
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
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    72
    def edge_properties(self, transition, fromstate, tostate):
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    73
        return {'label': '', 'dir': 'forward',
9001a74fcc82 more generic dot graph generator
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
diff changeset
    74
                'color': 'black', 'style': 'filled'}