uilib.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 13 Oct 2011 09:44:14 +0200
branchstable
changeset 7944 1faa8edb5ae3
parent 7879 9aae456abab5
child 7913 d0c6a7993cec
permissions -rw-r--r--
init_facets: Add documentation init_facets is not included in the default sphinx documentation but this docstring should help people digging into cubicweb core. This could go in an Advanced developer section of the book.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
7411
238da9684f99 cleanup and update some copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7166
diff changeset
     2
# 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: 4466
diff changeset
     3
# 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: 4466
diff changeset
     4
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
     5
# 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: 4466
diff changeset
     6
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
     7
# 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: 4466
diff changeset
     8
# 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: 4466
diff changeset
     9
# 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: 4466
diff changeset
    10
# 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: 4466
diff changeset
    11
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    12
# 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: 4466
diff changeset
    13
# 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: 4466
diff changeset
    14
# 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: 4466
diff changeset
    15
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    16
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    17
# 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: 4466
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
"""user interface libraries
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    21
contains some functions designed to help implementation of cubicweb user
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    22
interface.
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    23
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
import csv
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
import re
1635
866563e2d0fc don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents: 1623
diff changeset
    29
from StringIO import StringIO
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
    31
from logilab.mtconverter import xml_escape, html_unescape
4466
8b0ca7904820 moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    32
from logilab.common.date import ustrftime
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
7575
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
    34
from cubicweb.utils import JSString, json_dumps
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
    35
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
def rql_for_eid(eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    """return the rql query necessary to fetch entity with the given eid.  This
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    function should only be used to generate link with rql inside, not to give
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    to cursor.execute (in which case you won't benefit from rql cache).
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    :Parameters:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
      - `eid`: the eid of the entity we should search
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    :rtype: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    :return: the rql query
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    return 'Any X WHERE X eid %s' % eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
6106
1e6d93f70d14 [selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5951
diff changeset
    49
def eid_param(name, eid):
7058
ea22892e82d4 [uilib] fix eid_param implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6688
diff changeset
    50
    assert name is not None
6106
1e6d93f70d14 [selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5951
diff changeset
    51
    assert eid is not None
1e6d93f70d14 [selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5951
diff changeset
    52
    return '%s:%s' % (name, eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
7661
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    54
def print_bytes(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    55
    return u''
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    56
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    57
def print_string(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    58
    # don't translate empty value if you don't want strange results
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    59
    if props is not None and value and props.get('internationalizable'):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    60
        return req._(value)
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    61
    return value
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    62
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    63
def print_date(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    64
    return ustrftime(value, req.property_value('ui.date-format'))
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    65
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    66
def print_time(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    67
    return ustrftime(value, req.property_value('ui.time-format'))
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    68
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    69
def print_tztime(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    70
    return ustrftime(value, req.property_value('ui.time-format')) + u' UTC'
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    71
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    72
def print_datetime(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    73
    if displaytime:
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    74
        return ustrftime(value, req.property_value('ui.datetime-format'))
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    75
    return ustrftime(value, req.property_value('ui.date-format'))
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    76
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    77
def print_tzdatetime(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    78
    if displaytime:
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    79
        return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    80
    return ustrftime(value, req.property_value('ui.date-format'))
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    81
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    82
def print_boolean(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    83
    if value:
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    84
        return req._('yes')
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    85
    return req._('no')
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    86
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    87
def print_float(value, req, props, displaytime=True):
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    88
    return unicode(req.property_value('ui.float-format') % value)
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    89
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    90
PRINTERS = {
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    91
    'Bytes': print_bytes,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    92
    'String': print_string,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    93
    'Date': print_date,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    94
    'Time': print_time,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    95
    'TZTime': print_tztime,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    96
    'Datetime': print_datetime,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    97
    'TZDatetime': print_tzdatetime,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    98
    'Boolean': print_boolean,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
    99
    'Float': print_float,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   100
    'Decimal': print_float,
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   101
    # XXX Interval
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   102
    }
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   103
3212
07d11bacfefe displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3165
diff changeset
   104
def printable_value(req, attrtype, value, props=None, displaytime=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    """return a displayable value (i.e. unicode string)"""
7661
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   106
    if value is None:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        return u''
7661
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   108
    try:
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   109
        printer = PRINTERS[attrtype]
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   110
    except KeyError:
e157174c595c [uilib] reimplement printable_value function using a dictionary so one can easily change how to base type is displayed everywhere in the application. Closes #1827465
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7575
diff changeset
   111
        return unicode(value)
7662
3217298823c1 [uilib] fix bad argument order, introduced by previous commit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7661
diff changeset
   112
    return printer(value, req, props, displaytime)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
# text publishing #############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
try:
6491
ee9a10b6620e pylint option update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6106
diff changeset
   118
    from cubicweb.ext.rest import rest_publish # pylint: disable=W0611
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
except ImportError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
    def rest_publish(entity, data):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        """default behaviour if docutils was not found"""
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   122
        return xml_escape(data)
1581
80ee6397c087 fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   123
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
TAG_PROG = re.compile(r'</?.*?>', re.U)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
def remove_html_tags(text):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    """Removes HTML tags from text
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
    >>> remove_html_tags('<td>hi <a href="http://www.google.fr">world</a></td>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
    'hi world'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    >>>
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
    return TAG_PROG.sub('', text)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
REF_PROG = re.compile(r"<ref\s+rql=([\'\"])([^\1]*?)\1\s*>([^<]*)</ref>", re.U)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
def _subst_rql(view, obj):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
    delim, rql, descr = obj.groups()
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3369
diff changeset
   138
    return u'<a href="%s">%s</a>' % (view._cw.build_url(rql=rql), descr)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
def html_publish(view, text):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    """replace <ref rql=''> links by <a href="...">"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    if not text:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
    return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
277
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   146
# fallback implementation, nicer one defined below if lxml> 2.0 is available
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   147
def safe_cut(text, length):
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   148
    """returns a string of length <length> based on <text>, removing any html
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   149
    tags from given text if cut is necessary."""
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   150
    if text is None:
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   151
        return u''
362
a6a319f000c3 use mtconverter's html_unescape rather than saxutils' escape to deal with any html entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 350
diff changeset
   152
    noenttext = html_unescape(text)
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   153
    text_nohtml = remove_html_tags(noenttext)
277
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   154
    # try to keep html tags if text is short enough
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   155
    if len(text_nohtml) <= length:
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   156
        return text
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   157
    # else if un-tagged text is too long, cut it
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   158
    return xml_escape(text_nohtml[:length] + u'...')
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   159
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   160
fallback_safe_cut = safe_cut
277
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   161
5730
784025c15a3c [xhtml] fix soup2xhtml to deal with malformed div,body and html tags which may leads to malformed return value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   162
REM_ROOT_HTML_TAGS = re.compile('</(body|html)>', re.U)
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6683
diff changeset
   163
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   164
from lxml import etree, html
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   165
from lxml.html import clean, defs
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6683
diff changeset
   166
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   167
ALLOWED_TAGS = (defs.general_block_tags | defs.list_tags | defs.table_tags |
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   168
                defs.phrase_tags | defs.font_style_tags |
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   169
                set(('span', 'a', 'br', 'img', 'map', 'area', 'sub', 'sup'))
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   170
                )
6685
eeedb3575d25 [uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6683
diff changeset
   171
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   172
CLEANER = clean.Cleaner(allow_tags=ALLOWED_TAGS, remove_unknown_tags=False,
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   173
                        style=True, safe_attrs_only=True,
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   174
                        add_nofollow=False,
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   175
                        )
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   177
def soup2xhtml(data, encoding):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   178
    """tidy html soup by allowing some element tags and return the result
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   179
    """
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   180
    # remove spurious </body> and </html> tags, then normalize line break
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   181
    # (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   182
    data = REM_ROOT_HTML_TAGS.sub('', u'\n'.join(data.splitlines()))
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   183
    xmltree = etree.HTML(CLEANER.clean_html('<div>%s</div>' % data))
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   184
    # NOTE: lxml 2.0 does support encoding='unicode', but last time I (syt)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   185
    # tried I got weird results (lxml 2.2.8)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   186
    body = etree.tostring(xmltree[0], encoding=encoding)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   187
    # remove <body> and </body> and decode to unicode
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   188
    snippet = body[6:-7].decode(encoding)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   189
    # take care to bad xhtml (for instance starting with </div>) which
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   190
    # may mess with the <div> we added below. Only remove it if it's
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   191
    # still there...
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   192
    if snippet.startswith('<div>') and snippet.endswith('</div>'):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   193
        snippet = snippet[5:-6]
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   194
    return snippet
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   196
    # lxml.Cleaner envelops text elements by internal logic (not accessible)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   197
    # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   198
    # TODO drop attributes in elements
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   199
    # TODO add policy configuration (content only, embedded content, ...)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   200
    # XXX this is buggy for "<p>text1</p><p>text2</p>"...
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   201
    # XXX drop these two snippets action and follow the lxml behaviour
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   202
    # XXX (tests need to be updated)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   203
    # if snippet.startswith('<div>') and snippet.endswith('</div>'):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   204
    #     snippet = snippet[5:-6]
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   205
    # if snippet.startswith('<p>') and snippet.endswith('</p>'):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   206
    #     snippet = snippet[3:-4]
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   207
    return snippet.decode(encoding)
165
c5ff97312b8a cleaning code
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 164
diff changeset
   208
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   209
if hasattr(etree.HTML('<div>test</div>'), 'iter'): # XXX still necessary?
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   210
    # pylint: disable=E0102
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   211
    def safe_cut(text, length):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   212
        """returns an html document of length <length> based on <text>,
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   213
        and cut is necessary.
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   214
        """
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   215
        if text is None:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   216
            return u''
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   217
        dom = etree.HTML(text)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   218
        curlength = 0
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   219
        add_ellipsis = False
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   220
        for element in dom.iter():
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   221
            if curlength >= length:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   222
                parent = element.getparent()
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   223
                parent.remove(element)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   224
                if curlength == length and (element.text or element.tail):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   225
                    add_ellipsis = True
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   226
            else:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   227
                if element.text is not None:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   228
                    element.text = cut(element.text, length - curlength)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   229
                    curlength += len(element.text)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   230
                if element.tail is not None:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   231
                    if curlength < length:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   232
                        element.tail = cut(element.tail, length - curlength)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   233
                        curlength += len(element.tail)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   234
                    elif curlength == length:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   235
                        element.tail = '...'
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   236
                    else:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   237
                        element.tail = ''
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   238
        text = etree.tounicode(dom[0])[6:-7] # remove wrapping <body></body>
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   239
        if add_ellipsis:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   240
            return text + u'...'
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   241
        return text
1157
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   242
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   243
def text_cut(text, nbwords=30, gotoperiod=True):
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   244
    """from the given plain text, return a text with at least <nbwords> words,
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   245
    trying to go to the end of the current sentence.
277
a11a3c231050 fix lxml is available, we can have a nicer version of soup2xhtml even if its lxml < 2.0
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 228
diff changeset
   246
1157
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   247
    :param nbwords: the minimum number of words required
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   248
    :param gotoperiod: specifies if the function should try to go to
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   249
                       the first period after the cut (i.e. finish
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   250
                       the sentence if possible)
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   251
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   252
    Note that spaces are normalized.
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   253
    """
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   254
    if text is None:
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   255
        return u''
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   256
    words = text.split()
927
bfcc610c3d5e text_cut must return unicode not string
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 525
diff changeset
   257
    text = u' '.join(words) # normalize spaces
1157
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   258
    textlength = minlength = len(' '.join(words[:nbwords]))
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   259
    if gotoperiod:
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   260
        textlength = text.find('.', minlength) + 1
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   261
        if textlength == 0: # no period found
81a383cdda5c text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 927
diff changeset
   262
            textlength = minlength
350
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   263
    return text[:textlength]
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   264
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   265
def cut(text, length):
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   266
    """returns a string of a maximum length <length> based on <text>
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   267
    (approximatively, since if text has been  cut, '...' is added to the end of the string,
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   268
    resulting in a string of len <length> + 3)
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   269
    """
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   270
    if text is None:
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   271
        return u''
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   272
    if len(text) <= length:
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   273
        return text
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   274
    # else if un-tagged text is too long, cut it
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   275
    return text[:length] + u'...'
f34ef2c64605 cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 277
diff changeset
   276
165
c5ff97312b8a cleaning code
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 164
diff changeset
   277
1581
80ee6397c087 fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   278
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
# HTML generation helper functions ############################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   281
class _JSId(object):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   282
    def __init__(self, id, parent=None):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   283
        self.id = id
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   284
        self.parent = parent
5951
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   285
    def __unicode__(self):
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   286
        if self.parent:
5951
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   287
            return u'%s.%s' % (self.parent, self.id)
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   288
        return unicode(self.id)
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   289
    def __str__(self):
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   290
        return unicode(self).encode('utf8')
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   291
    def __getattr__(self, attr):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   292
        return _JSId(attr, self)
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   293
    def __call__(self, *args):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   294
        return _JSCallArgs(args, self)
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   295
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   296
class _JSCallArgs(_JSId):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   297
    def __init__(self, args, parent=None):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   298
        assert isinstance(args, tuple)
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   299
        self.args = args
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   300
        self.parent = parent
5951
6026582ae4f1 [uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5949
diff changeset
   301
    def __unicode__(self):
7575
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   302
        args = []
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   303
        for arg in self.args:
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   304
            if isinstance(arg, JSString):
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   305
                args.append(arg)
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   306
            else:
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   307
                args.append(json_dumps(arg))
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   308
        if self.parent:
7575
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   309
            return u'%s(%s)' % (self.parent, ','.join(args))
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   310
        return ','.join(args)
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   311
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   312
class _JS(object):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   313
    def __getattr__(self, attr):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   314
        return _JSId(attr)
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   315
7575
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   316
js = _JS()
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   317
js.__doc__ = """\
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   318
magic object to return strings suitable to call some javascript function with
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   319
the given arguments (which should be correctly typed).
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   320
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   321
>>> str(js.pouet(1, "2"))
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   322
'pouet(1,"2")'
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   323
>>> str(js.cw.pouet(1, "2"))
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   324
'cw.pouet(1,"2")'
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   325
>>> str(js.cw.pouet(1, "2").pouet(None))
7575
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   326
'cw.pouet(1,"2").pouet(null)'
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   327
>>> str(js.cw.pouet(1, JSString("$")).pouet(None))
335f14e8e5a7 [uilib] new js_dumps function allowing usage of bare js string (wrapped into JSString)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7464
diff changeset
   328
'cw.pouet(1,$).pouet(null)'
5949
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   329
"""
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   330
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   331
def domid(string):
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   332
    """return a valid DOM id from a string (should also be usable in jQuery
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   333
    search expression...)
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   334
    """
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   335
    return string.replace('.', '_').replace('-', '_')
2a273c896a38 [box] provide a new generic base box class to edit relation to simple entities, backported from the 'tag' cube
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5730
diff changeset
   336
2398
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   337
HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param',
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   338
                              'img', 'area', 'input', 'col'))
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   339
2516
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   340
def sgml_attributes(attrs):
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   341
    return u' '.join(u'%s="%s"' % (attr, xml_escape(unicode(value)))
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   342
                     for attr, value in sorted(attrs.items())
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   343
                     if value is not None)
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   344
1623
sylvain.thenault@logilab.fr
parents: 1581
diff changeset
   345
def simple_sgml_tag(tag, content=None, escapecontent=True, **attrs):
525
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   346
    """generation of a simple sgml tag (eg without children tags) easier
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   347
2399
68799e25f893 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   348
    content and attri butes will be escaped
525
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   349
    """
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   350
    value = u'<%s' % tag
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   351
    if attrs:
980
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 862
diff changeset
   352
        try:
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 862
diff changeset
   353
            attrs['class'] = attrs.pop('klass')
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 862
diff changeset
   354
        except KeyError:
59552ba2015f more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents: 862
diff changeset
   355
            pass
2516
b58826130680 extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2458
diff changeset
   356
        value += u' ' + sgml_attributes(attrs)
525
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   357
    if content:
1623
sylvain.thenault@logilab.fr
parents: 1581
diff changeset
   358
        if escapecontent:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   359
            content = xml_escape(unicode(content))
1623
sylvain.thenault@logilab.fr
parents: 1581
diff changeset
   360
        value += u'>%s</%s>' % (content, tag)
525
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   361
    else:
2398
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   362
        if tag in HTML4_EMPTY_TAGS:
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   363
            value += u' />'
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   364
        else:
a8d18e320ef3 a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2375
diff changeset
   365
            value += u'></%s>' % tag
525
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   366
    return value
bd4e03297cf0 function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents: 362
diff changeset
   367
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
def tooltipize(text, tooltip, url=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
    """make an HTML tooltip"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
    url = url or '#'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
    return u'<a href="%s" title="%s">%s</a>' % (url, tooltip, text)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
def toggle_action(nodeid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
    """builds a HTML link that uses the js toggleVisibility function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    return u"javascript: toggleVisibility('%s')" % nodeid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
def toggle_link(nodeid, label):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
    """builds a HTML link that uses the js toggleVisibility function"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
    return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
def ureport_as_html(layout):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
    from logilab.common.ureports import HTMLWriter
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
    formater = HTMLWriter(True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
    stream = StringIO() #UStringIO() don't want unicode assertion
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
    formater.format(layout, stream)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
    res = stream.getvalue()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
    if isinstance(res, str):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
        res = unicode(res, 'UTF8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
    return res
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
# traceback formatting ########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
import traceback
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
6683
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   396
def exc_message(ex, encoding):
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   397
    try:
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   398
        return unicode(ex)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7662
diff changeset
   399
    except Exception:
6683
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   400
        try:
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   401
            return unicode(str(ex), encoding, 'replace')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7662
diff changeset
   402
        except Exception:
6683
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   403
            return unicode(repr(ex), encoding, 'replace')
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   404
68cfebd3b9f3 fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6491
diff changeset
   405
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
def rest_traceback(info, exception):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
    """return a ReST formated traceback"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
    res = [u'Traceback\n---------\n::\n']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
    for stackentry in traceback.extract_tb(info[2]):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   410
        res.append(u'\tFile %s, line %s, function %s' % tuple(stackentry[:3]))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   411
        if stackentry[3]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
            res.append(u'\t  %s' % stackentry[3].decode('utf-8', 'replace'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
    res.append(u'\n')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
        res.append(u'\t Error: %s\n' % exception)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7662
diff changeset
   416
    except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
        pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   418
    return u'\n'.join(res)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
def html_traceback(info, exception, title='',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
                   encoding='ISO-8859-1', body=''):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
    """ return an html formatted traceback from python exception infos.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
    tcbk = info[2]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
    stacktb = traceback.extract_tb(tcbk)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
    strings = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
    if body:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
        strings.append(u'<div class="error_body">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
        # FIXME
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   431
        strings.append(body)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
        strings.append(u'</div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
    if title:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   434
        strings.append(u'<h1 class="error">%s</h1>'% xml_escape(title))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
    try:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   436
        strings.append(u'<p class="error">%s</p>' % xml_escape(str(exception)).replace("\n","<br />"))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
    except UnicodeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
        pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
    strings.append(u'<div class="error_traceback">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
    for index, stackentry in enumerate(stacktb):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
        strings.append(u'<b>File</b> <b class="file">%s</b>, <b>line</b> '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
                       u'<b class="line">%s</b>, <b>function</b> '
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
                       u'<b class="function">%s</b>:<br/>'%(
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   444
            xml_escape(stackentry[0]), stackentry[1], xml_escape(stackentry[2])))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   445
        if stackentry[3]:
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   446
            string = xml_escape(stackentry[3]).decode('utf-8', 'replace')
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2516
diff changeset
   447
            strings.append(u'&#160;&#160;%s<br/>\n' % (string))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
        # add locals info for each entry
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
            local_context = tcbk.tb_frame.f_locals
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
            html_info = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
            chars = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
            for name, value in local_context.iteritems():
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   454
                value = xml_escape(repr(value))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
                info = u'<span class="name">%s</span>=%s, ' % (name, value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
                line_length = len(name) + len(value)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
                chars += line_length
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
                # 150 is the result of *years* of research ;-) (CSS might be helpful here)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
                if chars > 150:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   460
                    info = u'<br/>' + info
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
                    chars = line_length
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
                html_info.append(info)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
            boxid = 'ctxlevel%d' % index
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
            strings.append(u'[%s]' % toggle_link(boxid, '+'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
            strings.append(u'<div id="%s" class="pycontext hidden">%s</div>' %
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
                           (boxid, ''.join(html_info)))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
            tcbk = tcbk.tb_next
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
        except Exception:
1581
80ee6397c087 fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   469
            pass # doesn't really matter if we have no context info
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
    strings.append(u'</div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
    return '\n'.join(strings)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   472
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
# csv files / unicode support #################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
class UnicodeCSVWriter:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
    """proxies calls to csv.writer.writerow to be able to deal with unicode"""
1581
80ee6397c087 fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
   477
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
    def __init__(self, wfunc, encoding, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
        self.writer = csv.writer(self, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
        self.wfunc = wfunc
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   481
        self.encoding = encoding
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   482
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
    def write(self, data):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
        self.wfunc(data)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
    def writerow(self, row):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
        csvrow = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
        for elt in row:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
            if isinstance(elt, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
                csvrow.append(elt.encode(self.encoding))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
                csvrow.append(str(elt))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
        self.writer.writerow(csvrow)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
    def writerows(self, rows):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
        for row in rows:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
            self.writerow(row)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
# some decorators #############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
class limitsize(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
    def __init__(self, maxsize):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
        self.maxsize = maxsize
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   506
    def __call__(self, function):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   507
        def newfunc(*args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
            ret = function(*args, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
            if isinstance(ret, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
                return ret[:self.maxsize]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
            return ret
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
        return newfunc
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
def htmlescape(function):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
    def newfunc(*args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
        ret = function(*args, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
        assert isinstance(ret, basestring)
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2208
diff changeset
   519
        return xml_escape(ret)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
    return newfunc