web/schemaviewer.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Mon, 02 Jun 2014 16:10:16 +0200
changeset 9833 1485aab7ece6
parent 9704 43c9896d0e9f
child 10612 84468b90e9c1
permissions -rw-r--r--
[tests/querier] use the new connection api (part 3/3) Some adaptations to devtools/repotest: * dead code removal * remove session related code In the tests: * many tests actually don't check the querier but some generic rql property -- when such a test needs several statements to complete a commit, switch to session.new_cnx * provide an assertRQLEqual helper
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5460
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4467
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5460
diff changeset
    18
"""an helper class to display CubicWeb schema using ureports"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1998
diff changeset
    21
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from logilab.common.ureports import Section, Title, Table, Link, Span, Text
4087
2b8bc4cfd6ee api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3877
diff changeset
    24
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
    25
from yams.schema2dot import CARD_MAP
4087
2b8bc4cfd6ee api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3877
diff changeset
    26
from yams.schema import RelationDefinitionSchema
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    27
from operator import attrgetter
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    28
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    29
TYPE_GETTER = attrgetter('type')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
I18NSTRINGS = [_('read'), _('add'), _('delete'), _('update'), _('order')]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
    33
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
class SchemaViewer(object):
9704
43c9896d0e9f an -> a (misc)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 7407
diff changeset
    35
    """return a ureport layout for some part of a schema"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    def __init__(self, req=None, encoding=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        self.req = req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        if req is not None:
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    39
            req.add_css('cubicweb.schema.css')
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    40
            if encoding is None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
                encoding = req.encoding
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    42
            self._ = req._
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    43
        else:
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    44
            encoding = 'ascii'
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    45
            self._ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        self.encoding = encoding
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
    47
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    48
    # no self.req managements
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    49
5460
52ae8f68d9e2 [web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5426
diff changeset
    50
    def may_read(self, rdef, action='read'):
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    51
        """Return true if request user may read the given schema.
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    52
        Always return True when no request is provided.
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    53
        """
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    54
        if self.req is None:
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    55
            return True
5460
52ae8f68d9e2 [web/schemaviewer] fix bad call
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5426
diff changeset
    56
        return rdef.may_have_permission(action, self.req)
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    57
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    58
    def format_eschema(self, eschema):
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    59
        text = eschema.type
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    60
        if self.req is None:
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    61
            return Text(text)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    62
        return Link(self.req.build_url('cwetype/%s' % eschema), text)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    63
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    64
    def format_rschema(self, rschema, label=None):
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    65
        if label is None:
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    66
            label = rschema.type
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    67
        if self.req is None:
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    68
            return Text(label)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    69
        return Link(self.req.build_url('cwrtype/%s' % rschema), label)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    70
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    71
    # end of no self.req managements
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    72
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1998
diff changeset
    73
    def visit_schema(self, schema, display_relations=0, skiptypes=()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        """get a layout for a whole schema"""
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    75
        title = Title(self._('Schema %s') % schema.name,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
                      klass='titleUnderline')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        layout = Section(children=(title,))
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    78
        esection = Section(children=(Title(self._('Entities'),
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
                                           klass='titleUnderline'),))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
        layout.append(esection)
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    81
        eschemas = [eschema for eschema in schema.entities()
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
    82
                    if not (eschema.final or eschema in skiptypes)]
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    83
        for eschema in sorted(eschemas, key=TYPE_GETTER):
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1998
diff changeset
    84
            esection.append(self.visit_entityschema(eschema, skiptypes))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        if display_relations:
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    86
            title = Title(self._('Relations'), klass='titleUnderline')
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
    87
            rsection = Section(children=(title,))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
            layout.append(rsection)
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    89
            relations = [rschema for rschema in sorted(schema.relations(), key=TYPE_GETTER)
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
    90
                         if not (rschema.final or rschema.type in skiptypes)]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
            keys = [(rschema.type, rschema) for rschema in relations]
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    92
            for key, rschema in sorted(keys, cmp=(lambda x, y: cmp(x[1], y[1]))):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
                relstr = self.visit_relationschema(rschema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
                rsection.append(relstr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        return layout
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
    def _entity_attributes_data(self, eschema):
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
    98
        _ = self._
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        data = [_('attribute'), _('type'), _('default'), _('constraints')]
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   100
        attributes = sorted(eschema.attribute_definitions(), cmp=(lambda x, y: cmp(x[0].type, y[0].type)))
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   101
        for rschema, aschema in attributes:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
   102
            rdef = eschema.rdef(rschema)
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   103
            if not self.may_read(rdef):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
            aname = rschema.type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
            if aname == 'eid':
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
   107
                continue
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
            data.append('%s (%s)' % (aname, _(aname)))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
            data.append(_(aschema.type))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
            defaultval = eschema.default(aname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
            if defaultval is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
                default = self.to_string(defaultval)
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
   113
            elif rdef.cardinality[0] == '1':
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
                default = _('required field')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
                default = ''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
            data.append(default)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
            constraints = rschema.rproperty(eschema.type, aschema.type,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                                            'constraints')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
            data.append(', '.join(str(constr) for constr in constraints))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        return data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
    def stereotype(self, name):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        return Span((' <<%s>>' % name,), klass='stereotype')
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
   126
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1998
diff changeset
   127
    def visit_entityschema(self, eschema, skiptypes=()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        """get a layout for an entity schema"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        etype = eschema.type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        layout = Section(children=' ', klass='clear')
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2650
diff changeset
   131
        layout.append(Link(etype,'&#160;' , id=etype)) # anchor
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   132
        title = self.format_eschema(eschema)
5239
471554b842d2 [schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5234
diff changeset
   133
        boxchild = [Section(children=(title,), klass='title')]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        data = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        data.append(Section(children=boxchild, klass='box'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        data.append(Section(children='', klass='vl'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        data.append(Section(children='', klass='hl'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        t_vars = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        rels = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        first = True
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   141
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   142
        rel_defs = sorted(eschema.relation_definitions(),
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   143
                          cmp=(lambda x, y: cmp((x[0].type, x[0].cardinality),
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   144
                          (y[0].type, y[0].cardinality))))
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   145
        for rschema, targetschemas, role in rel_defs:
2126
a25859917ccc stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1998
diff changeset
   146
            if rschema.type in skiptypes:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
                continue
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   148
            for oeschema in sorted(targetschemas, key=TYPE_GETTER):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
   149
                rdef = rschema.role_rdef(eschema, oeschema, role)
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   150
                if not self.may_read(rdef):
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
   151
                    continue
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
                label = rschema.type
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3768
diff changeset
   153
                if role == 'subject':
7407
a78cc15227ef [deprecation] drop remaining warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   154
                    cards = rschema.rdef(eschema, oeschema).cardinality
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
                else:
7407
a78cc15227ef [deprecation] drop remaining warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7152
diff changeset
   156
                    cards = rschema.rdef(oeschema, eschema).cardinality
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
                    cards = cards[::-1]
5239
471554b842d2 [schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5234
diff changeset
   158
                label = '%s %s %s' % (CARD_MAP[cards[1]], label,
471554b842d2 [schema view] the final touch. Things are getting nicely displayed, and code clean
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5234
diff changeset
   159
                                      CARD_MAP[cards[0]])
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   160
                rlink = self.format_rschema(rschema, label)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   161
                elink = self.format_eschema(oeschema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
                if first:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
                    t_vars.append(Section(children=(elink,), klass='firstvar'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
                    rels.append(Section(children=(rlink,), klass='firstrel'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
                    first = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
                else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
                    t_vars.append(Section(children=(elink,), klass='var'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
                    rels.append(Section(children=(rlink,), klass='rel'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        data.append(Section(children=rels, klass='rels'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
        data.append(Section(children=t_vars, klass='vars'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        layout.append(Section(children=data, klass='entityAttributes'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        return layout
1731
13f948678a6a delete-trailing-spaces, fix typo
sylvain.thenault@logilab.fr
parents: 1522
diff changeset
   173
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
    def visit_relationschema(self, rschema, title=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        """get a layout for a relation schema"""
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   176
        _ = self._
3768
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   177
        if title:
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   178
            title = self.format_rschema(rschema)
3768
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   179
            stereotypes = []
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   180
            if rschema.meta:
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   181
                stereotypes.append('meta')
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   182
            if rschema.symmetric:
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   183
                stereotypes.append('symmetric')
3768
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   184
            if rschema.inlined:
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   185
                stereotypes.append('inlined')
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   186
            title = Section(children=(title,), klass='title')
3768
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   187
            if stereotypes:
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   188
                title.append(self.stereotype(','.join(stereotypes)))
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   189
            layout = Section(children=(title,), klass='schema')
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   190
        else:
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   191
            layout = Section(klass='schema')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
        data = [_('from'), _('to')]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        schema = rschema.schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
        rschema_objects = rschema.objects()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        if rschema_objects:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
            # might be empty
4087
2b8bc4cfd6ee api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3877
diff changeset
   197
            properties = [p for p in RelationDefinitionSchema.rproperty_defs(rschema_objects[0])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
                          if not p in ('cardinality', 'composite', 'eid')]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
            properties = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        data += [_(prop) for prop in properties]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        cols = len(data)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
        done = set()
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   204
        for subjtype, objtypes in sorted(rschema.associations()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
            for objtype in objtypes:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
                if (subjtype, objtype) in done:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
                done.add((subjtype, objtype))
4467
0e73d299730a fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   209
                if rschema.symmetric:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
                    done.add((objtype, subjtype))
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   211
                data.append(self.format_eschema(schema[subjtype]))
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   212
                data.append(self.format_eschema(schema[objtype]))
4089
ff92c7d692bf typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4087
diff changeset
   213
                rdef = rschema.rdef(subjtype, objtype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
                for prop in properties:
4087
2b8bc4cfd6ee api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3877
diff changeset
   215
                    val = getattr(rdef, prop)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
                    if val is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
                        val = ''
5234
e2476d78b060 ticket #787103, relation schema view enhancements
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 5233
diff changeset
   218
                    elif prop == 'constraints':
7152
39c1ffc7d93f [schema, refactoring] use RQLExpression as base class for RQL constraint classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5460
diff changeset
   219
                        val = ', '.join([c.expression for c in val])
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   220
                    elif isinstance(val, dict):
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   221
                        for key, value in val.iteritems():
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   222
                            if isinstance(value, (list, tuple)):
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   223
                                val[key] = ', '.join(sorted( str(v) for v in value))
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   224
                        val = str(val)
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   225
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
                    elif isinstance(val, (list, tuple)):
5372
b74eed7e8b37 Add a basic script to compare the db_schema to the fs_schema.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5239
diff changeset
   227
                        val = sorted(val)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
                        val = ', '.join(str(v) for v in val)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
                    elif val and isinstance(val, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
                        val = _(val)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
                    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
                        val = str(val)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
                    data.append(Text(val))
3768
8c85a2f7f5ad nicer relation|entity schema view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   234
        table = Table(cols=cols, rheaders=1, children=data, klass='listing')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        layout.append(Section(children=(table,), klass='relationDefinition'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        layout.append(Section(children='', klass='clear'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        return layout
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    def to_string(self, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        """used to converte arbitrary values to encoded string"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
        if isinstance(value, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
            return value.encode(self.encoding, 'replace')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
        return str(value)