author | Julien Cristau <julien.cristau@logilab.fr> |
Mon, 19 Oct 2015 14:28:30 +0200 | |
changeset 10816 | d4a26e94f54a |
parent 10784 | 21bb9a00adeb |
child 10817 | 7b154e0fa194 |
permissions | -rw-r--r-- |
0 | 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 | 19 |
"""user interface libraries |
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 | 24 |
|
25 |
__docformat__ = "restructuredtext en" |
|
26 |
||
27 |
import csv |
|
28 |
import re |
|
10617
c36cda9074c5
[py3k] io.StringIO
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10613
diff
changeset
|
29 |
from io import StringIO |
0 | 30 |
|
10784
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
31 |
from six import PY2, PY3, text_type, binary_type, string_types, integer_types |
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10332
diff
changeset
|
32 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
33 |
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
|
34 |
from logilab.common.date import ustrftime |
7914
fb757a7d887e
[request, ui] printable_value is now a method of request, and may be given dict of formatters to use (closes #1984743)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7913
diff
changeset
|
35 |
from logilab.common.deprecation import deprecated |
0 | 36 |
|
10332
da1cb2b12fe1
[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents:
10012
diff
changeset
|
37 |
from cubicweb.utils import js_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
|
38 |
|
0 | 39 |
|
40 |
def rql_for_eid(eid): |
|
41 |
"""return the rql query necessary to fetch entity with the given eid. This |
|
42 |
function should only be used to generate link with rql inside, not to give |
|
43 |
to cursor.execute (in which case you won't benefit from rql cache). |
|
44 |
||
45 |
:Parameters: |
|
46 |
- `eid`: the eid of the entity we should search |
|
47 |
:rtype: str |
|
48 |
:return: the rql query |
|
49 |
""" |
|
50 |
return 'Any X WHERE X eid %s' % eid |
|
51 |
||
6106
1e6d93f70d14
[selectors] fix match_transition & introduce new edited_attribute selector
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5951
diff
changeset
|
52 |
def eid_param(name, eid): |
7058
ea22892e82d4
[uilib] fix eid_param implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6688
diff
changeset
|
53 |
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
|
54 |
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
|
55 |
return '%s:%s' % (name, eid) |
0 | 56 |
|
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
|
57 |
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
|
58 |
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
|
59 |
|
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 |
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
|
61 |
# 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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
|
7971
3e51c2a577dd
[uilib] add missing printer for integers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7914
diff
changeset
|
66 |
def print_int(value, req, props, displaytime=True): |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
67 |
return text_type(value) |
7971
3e51c2a577dd
[uilib] add missing printer for integers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7914
diff
changeset
|
68 |
|
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
|
69 |
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
|
70 |
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
|
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_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
|
73 |
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
|
74 |
|
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 |
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
|
76 |
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
|
77 |
|
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 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
|
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 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
|
7913
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
88 |
_('%d years') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
89 |
_('%d months') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
90 |
_('%d weeks') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
91 |
_('%d days') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
92 |
_('%d hours') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
93 |
_('%d minutes') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
94 |
_('%d seconds') |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
95 |
|
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
96 |
def print_timedelta(value, req, props, displaytime=True): |
10613
8d9fe02387e3
[py3k] six.integer_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10612
diff
changeset
|
97 |
if isinstance(value, integer_types): |
7913
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
98 |
# `date - date`, unlike `datetime - datetime` gives an int |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
99 |
# (number of days), not a timedelta |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
100 |
# XXX should rql be fixed to return Int instead of Interval in |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
101 |
# that case? that would be probably the proper fix but we |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
102 |
# loose information on the way... |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
103 |
value = timedelta(days=value) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
104 |
if value.days > 730 or value.days < -730: # 2 years |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
105 |
return req._('%d years') % (value.days // 365) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
106 |
elif value.days > 60 or value.days < -60: # 2 months |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
107 |
return req._('%d months') % (value.days // 30) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
108 |
elif value.days > 14 or value.days < -14: # 2 weeks |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
109 |
return req._('%d weeks') % (value.days // 7) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
110 |
elif value.days > 2 or value.days < -2: |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
111 |
return req._('%d days') % int(value.days) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
112 |
else: |
8301
1385e05afe52
[uilib] don't print timedeltas between 0 and 23h as negative (closes #2236352)
Julien Cristau <julien.cristau@logilab.fr>
parents:
8093
diff
changeset
|
113 |
minus = 1 if value.days >= 0 else -1 |
7913
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
114 |
if value.seconds > 3600: |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
115 |
return req._('%d hours') % (int(value.seconds // 3600) * minus) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
116 |
elif value.seconds >= 120: |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
117 |
return req._('%d minutes') % (int(value.seconds // 60) * minus) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
118 |
else: |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
119 |
return req._('%d seconds') % (int(value.seconds) * minus) |
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
120 |
|
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
|
121 |
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
|
122 |
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
|
123 |
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
|
124 |
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
|
125 |
|
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
|
126 |
def print_float(value, req, props, displaytime=True): |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
127 |
return text_type(req.property_value('ui.float-format') % value) # XXX cast needed ? |
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
|
128 |
|
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
|
129 |
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
|
130 |
'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
|
131 |
'String': print_string, |
7971
3e51c2a577dd
[uilib] add missing printer for integers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7914
diff
changeset
|
132 |
'Int': print_int, |
3e51c2a577dd
[uilib] add missing printer for integers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7914
diff
changeset
|
133 |
'BigInt': print_int, |
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
|
134 |
'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
|
135 |
'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
|
136 |
'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
|
137 |
'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
|
138 |
'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
|
139 |
'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
|
140 |
'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
|
141 |
'Decimal': print_float, |
7913
d0c6a7993cec
[web, formatting] move Interval data type display logic from final view to printable_value (closes #1984742)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7879
diff
changeset
|
142 |
'Interval': print_timedelta, |
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
|
143 |
} |
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
|
144 |
|
7914
fb757a7d887e
[request, ui] printable_value is now a method of request, and may be given dict of formatters to use (closes #1984743)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7913
diff
changeset
|
145 |
@deprecated('[3.14] use req.printable_value(attrtype, value, ...)') |
3212
07d11bacfefe
displaytime attribute should not have been removed from there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3165
diff
changeset
|
146 |
def printable_value(req, attrtype, value, props=None, displaytime=True): |
7914
fb757a7d887e
[request, ui] printable_value is now a method of request, and may be given dict of formatters to use (closes #1984743)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7913
diff
changeset
|
147 |
return req.printable_value(attrtype, value, props, displaytime) |
0 | 148 |
|
8093
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
149 |
def css_em_num_value(vreg, propname, default): |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
150 |
""" we try to read an 'em' css property |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
151 |
if we get another unit we're out of luck and resort to the given default |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
152 |
(hence, it is strongly advised not to specify but ems for this css prop) |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
153 |
""" |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
154 |
propvalue = vreg.config.uiprops[propname].lower().strip() |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
155 |
if propvalue.endswith('em'): |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
156 |
try: |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
157 |
return float(propvalue[:-2]) |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
158 |
except Exception: |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
159 |
vreg.warning('css property %s looks malformed (%r)', |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
160 |
propname, propvalue) |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
161 |
else: |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
162 |
vreg.warning('css property %s should use em (currently is %r)', |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
163 |
propname, propvalue) |
3efb83e4e8f3
[facets] do no stretch to the right when there are many facets; instead use a floating layout (closes #2093160)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
7971
diff
changeset
|
164 |
return default |
0 | 165 |
|
166 |
# text publishing ############################################################# |
|
167 |
||
10012
8c2c6fdd8d56
[RichString] Add markdown support
Christophe de Vienne <christophe@unlish.com>
parents:
9921
diff
changeset
|
168 |
from cubicweb.ext.markdown import markdown_publish # pylint: disable=W0611 |
8c2c6fdd8d56
[RichString] Add markdown support
Christophe de Vienne <christophe@unlish.com>
parents:
9921
diff
changeset
|
169 |
|
0 | 170 |
try: |
6491
ee9a10b6620e
pylint option update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6106
diff
changeset
|
171 |
from cubicweb.ext.rest import rest_publish # pylint: disable=W0611 |
0 | 172 |
except ImportError: |
173 |
def rest_publish(entity, data): |
|
174 |
"""default behaviour if docutils was not found""" |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
175 |
return xml_escape(data) |
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
176 |
|
10012
8c2c6fdd8d56
[RichString] Add markdown support
Christophe de Vienne <christophe@unlish.com>
parents:
9921
diff
changeset
|
177 |
|
0 | 178 |
TAG_PROG = re.compile(r'</?.*?>', re.U) |
179 |
def remove_html_tags(text): |
|
180 |
"""Removes HTML tags from text |
|
181 |
||
182 |
>>> remove_html_tags('<td>hi <a href="http://www.google.fr">world</a></td>') |
|
183 |
'hi world' |
|
184 |
>>> |
|
185 |
""" |
|
186 |
return TAG_PROG.sub('', text) |
|
187 |
||
188 |
||
189 |
REF_PROG = re.compile(r"<ref\s+rql=([\'\"])([^\1]*?)\1\s*>([^<]*)</ref>", re.U) |
|
190 |
def _subst_rql(view, obj): |
|
191 |
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
|
192 |
return u'<a href="%s">%s</a>' % (view._cw.build_url(rql=rql), descr) |
0 | 193 |
|
194 |
def html_publish(view, text): |
|
195 |
"""replace <ref rql=''> links by <a href="...">""" |
|
196 |
if not text: |
|
197 |
return u'' |
|
198 |
return REF_PROG.sub(lambda obj, view=view:_subst_rql(view, obj), text) |
|
199 |
||
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
|
200 |
# 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
|
201 |
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
|
202 |
"""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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
noenttext = html_unescape(text) |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
207 |
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
|
208 |
# 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
|
209 |
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
|
210 |
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
|
211 |
# 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
|
212 |
return xml_escape(text_nohtml[:length] + u'...') |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
213 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
214 |
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
|
215 |
|
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
|
216 |
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
|
217 |
|
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
|
218 |
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
|
219 |
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
|
220 |
|
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
|
221 |
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
|
222 |
defs.phrase_tags | defs.font_style_tags | |
9524
c4581bc582e1
[uilib] allow canvas tags in the html cleaner
Julien Cristau <julien.cristau@logilab.fr>
parents:
9326
diff
changeset
|
223 |
set(('span', 'a', 'br', 'img', 'map', 'area', 'sub', 'sup', 'canvas')) |
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
|
224 |
) |
6685
eeedb3575d25
[uilib] soup2xhtml uses now lxml.html.Cleaner
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6683
diff
changeset
|
225 |
|
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
) |
0 | 230 |
|
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
|
231 |
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
|
232 |
"""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
|
233 |
""" |
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 |
# 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
|
235 |
# (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
|
236 |
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
|
237 |
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
|
238 |
# 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
|
239 |
# 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
|
240 |
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
|
241 |
# 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
|
242 |
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
|
243 |
# 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
|
244 |
# 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
|
245 |
# 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
|
246 |
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
|
247 |
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
|
248 |
return snippet |
0 | 249 |
|
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
|
250 |
# 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
|
251 |
# 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
|
252 |
# 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
|
253 |
# 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
|
254 |
# 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
|
255 |
# 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
|
256 |
# 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
|
257 |
# 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
|
258 |
# 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
|
259 |
# 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
|
260 |
# 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
|
261 |
return snippet.decode(encoding) |
165 | 262 |
|
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
|
263 |
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
|
264 |
# 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
|
265 |
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
|
266 |
"""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
|
267 |
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
|
268 |
""" |
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
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
|
294 |
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
|
295 |
return text |
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
296 |
|
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
297 |
def text_cut(text, nbwords=30, gotoperiod=True): |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
298 |
"""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
|
299 |
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
|
300 |
|
1157
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
301 |
: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
|
302 |
: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
|
303 |
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
|
304 |
the sentence if possible) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
305 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
306 |
Note that spaces are normalized. |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
307 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
308 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
309 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
310 |
words = text.split() |
927
bfcc610c3d5e
text_cut must return unicode not string
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents:
525
diff
changeset
|
311 |
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
|
312 |
textlength = minlength = len(' '.join(words[:nbwords])) |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
313 |
if gotoperiod: |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
314 |
textlength = text.find('.', minlength) + 1 |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
315 |
if textlength == 0: # no period found |
81a383cdda5c
text_cut() accepts a gotoperiod parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
927
diff
changeset
|
316 |
textlength = minlength |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
317 |
return text[:textlength] |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
318 |
|
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
319 |
def cut(text, length): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
320 |
"""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
|
321 |
(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
|
322 |
resulting in a string of len <length> + 3) |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
323 |
""" |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
324 |
if text is None: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
325 |
return u'' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
326 |
if len(text) <= length: |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
327 |
return text |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
328 |
# 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
|
329 |
return text[:length] + u'...' |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
277
diff
changeset
|
330 |
|
165 | 331 |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
332 |
|
0 | 333 |
# HTML generation helper functions ############################################ |
334 |
||
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
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
self.parent = parent |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
339 |
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
|
340 |
if self.parent: |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
341 |
return u'%s.%s' % (self.parent, self.id) |
10702
f94c812c3669
[py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10695
diff
changeset
|
342 |
return text_type(self.id) |
10703
3cb87b61b067
[py3k] __unicode__ and __str__
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10702
diff
changeset
|
343 |
__str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8') |
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
|
344 |
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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
|
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
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
|
353 |
self.parent = parent |
5951
6026582ae4f1
[uilib] js objects implements __unicode__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5949
diff
changeset
|
354 |
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
|
355 |
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
|
356 |
for arg in self.args: |
10332
da1cb2b12fe1
[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents:
10012
diff
changeset
|
357 |
args.append(js_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
|
358 |
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
|
359 |
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
|
360 |
return ','.join(args) |
10703
3cb87b61b067
[py3k] __unicode__ and __str__
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10702
diff
changeset
|
361 |
__str__ = __unicode__ if PY3 else lambda self: self.__unicode__().encode('utf-8') |
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
|
362 |
|
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
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
|
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
|
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
|
372 |
>>> 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
|
373 |
'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
|
374 |
>>> 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
|
375 |
'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
|
376 |
>>> 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
|
377 |
'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
|
378 |
>>> 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
|
379 |
'cw.pouet(1,$).pouet(null)' |
10332
da1cb2b12fe1
[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents:
10012
diff
changeset
|
380 |
>>> str(js.cw.pouet(1, {'callback': JSString("cw.cb")}).pouet(None)) |
da1cb2b12fe1
[uilib] `uilib.js` helper now honors explicit JSString (closes #4959538)
Alain Leufroy <alain@leufroy.fr>
parents:
10012
diff
changeset
|
381 |
'cw.pouet(1,{callback: cw.cb}).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
|
382 |
""" |
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
|
383 |
|
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
|
384 |
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
|
385 |
"""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
|
386 |
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
|
387 |
""" |
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
|
388 |
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
|
389 |
|
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
390 |
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
|
391 |
'img', 'area', 'input', 'col')) |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
392 |
|
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
393 |
def sgml_attributes(attrs): |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
394 |
return u' '.join(u'%s="%s"' % (attr, xml_escape(text_type(value))) |
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
395 |
for attr, value in sorted(attrs.items()) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
396 |
if value is not None) |
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
397 |
|
1623 | 398 |
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
|
399 |
"""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
|
400 |
|
2399
68799e25f893
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
401 |
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
|
402 |
""" |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
403 |
value = u'<%s' % tag |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
404 |
if attrs: |
980
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
405 |
try: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
406 |
attrs['class'] = attrs.pop('klass') |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
407 |
except KeyError: |
59552ba2015f
more tags, map klass -> class avoiding needs to use **dict
sylvain.thenault@logilab.fr
parents:
862
diff
changeset
|
408 |
pass |
2516
b58826130680
extract function: sgml_attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
409 |
value += u' ' + sgml_attributes(attrs) |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
410 |
if content: |
1623 | 411 |
if escapecontent: |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
412 |
content = xml_escape(text_type(content)) |
1623 | 413 |
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
|
414 |
else: |
2398
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
415 |
if tag in HTML4_EMPTY_TAGS: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
416 |
value += u' />' |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
417 |
else: |
a8d18e320ef3
a standard-conformant fix for sgml_tags
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2375
diff
changeset
|
418 |
value += u'></%s>' % tag |
525
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
419 |
return value |
bd4e03297cf0
function to make generation of a simple sgml tag
sylvain.thenault@logilab.fr
parents:
362
diff
changeset
|
420 |
|
0 | 421 |
def tooltipize(text, tooltip, url=None): |
422 |
"""make an HTML tooltip""" |
|
423 |
url = url or '#' |
|
424 |
return u'<a href="%s" title="%s">%s</a>' % (url, tooltip, text) |
|
425 |
||
426 |
def toggle_action(nodeid): |
|
427 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
428 |
return u"javascript: toggleVisibility('%s')" % nodeid |
|
429 |
||
430 |
def toggle_link(nodeid, label): |
|
431 |
"""builds a HTML link that uses the js toggleVisibility function""" |
|
432 |
return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label) |
|
433 |
||
434 |
||
435 |
def ureport_as_html(layout): |
|
436 |
from logilab.common.ureports import HTMLWriter |
|
437 |
formater = HTMLWriter(True) |
|
438 |
stream = StringIO() #UStringIO() don't want unicode assertion |
|
439 |
formater.format(layout, stream) |
|
440 |
res = stream.getvalue() |
|
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
441 |
if isinstance(res, binary_type): |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
442 |
res = res.decode('UTF8') |
0 | 443 |
return res |
444 |
||
445 |
# traceback formatting ######################################################## |
|
446 |
||
447 |
import traceback |
|
448 |
||
6683
68cfebd3b9f3
fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6491
diff
changeset
|
449 |
def exc_message(ex, encoding): |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
450 |
if PY3: |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
451 |
excmsg = str(ex) |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
452 |
else: |
6683
68cfebd3b9f3
fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6491
diff
changeset
|
453 |
try: |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
454 |
excmsg = 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
|
455 |
except Exception: |
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
456 |
try: |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
457 |
excmsg = unicode(str(ex), encoding, 'replace') |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
458 |
except Exception: |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
459 |
excmsg = unicode(repr(ex), encoding, 'replace') |
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
460 |
exctype = ex.__class__.__name__ |
9921
8227358aa983
[web] Return useful error messages when exceptions arise in ajax controllers (closes #3932503)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9524
diff
changeset
|
461 |
return u'%s: %s' % (exctype, excmsg) |
6683
68cfebd3b9f3
fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6491
diff
changeset
|
462 |
|
68cfebd3b9f3
fix #724689: exception's display during ajax call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6491
diff
changeset
|
463 |
|
0 | 464 |
def rest_traceback(info, exception): |
9326
9d145b4c4e53
[web] make sign_text unicode aware, avoid crash with non-ascii chars. Closes #3289774
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8301
diff
changeset
|
465 |
"""return a unicode ReST formated traceback""" |
0 | 466 |
res = [u'Traceback\n---------\n::\n'] |
467 |
for stackentry in traceback.extract_tb(info[2]): |
|
468 |
res.append(u'\tFile %s, line %s, function %s' % tuple(stackentry[:3])) |
|
469 |
if stackentry[3]: |
|
10784
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
470 |
data = xml_escape(stackentry[3]) |
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
471 |
if PY2: |
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
472 |
data = data.decode('utf-8', 'replace') |
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
473 |
res.append(u'\t %s' % data) |
0 | 474 |
res.append(u'\n') |
475 |
try: |
|
476 |
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
|
477 |
except Exception: |
0 | 478 |
pass |
479 |
return u'\n'.join(res) |
|
480 |
||
481 |
||
482 |
def html_traceback(info, exception, title='', |
|
483 |
encoding='ISO-8859-1', body=''): |
|
484 |
""" return an html formatted traceback from python exception infos. |
|
485 |
""" |
|
486 |
tcbk = info[2] |
|
487 |
stacktb = traceback.extract_tb(tcbk) |
|
488 |
strings = [] |
|
489 |
if body: |
|
490 |
strings.append(u'<div class="error_body">') |
|
491 |
# FIXME |
|
492 |
strings.append(body) |
|
493 |
strings.append(u'</div>') |
|
494 |
if title: |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
495 |
strings.append(u'<h1 class="error">%s</h1>'% xml_escape(title)) |
0 | 496 |
try: |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
497 |
strings.append(u'<p class="error">%s</p>' % xml_escape(str(exception)).replace("\n","<br />")) |
0 | 498 |
except UnicodeError: |
499 |
pass |
|
500 |
strings.append(u'<div class="error_traceback">') |
|
501 |
for index, stackentry in enumerate(stacktb): |
|
502 |
strings.append(u'<b>File</b> <b class="file">%s</b>, <b>line</b> ' |
|
503 |
u'<b class="line">%s</b>, <b>function</b> ' |
|
504 |
u'<b class="function">%s</b>:<br/>'%( |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
505 |
xml_escape(stackentry[0]), stackentry[1], xml_escape(stackentry[2]))) |
0 | 506 |
if stackentry[3]: |
10784
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
507 |
string = xml_escape(stackentry[3]) |
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
508 |
if PY2: |
21bb9a00adeb
[uilib] fix traceback on python3
Julien Cristau <julien.cristau@logilab.fr>
parents:
10703
diff
changeset
|
509 |
string = string.decode('utf-8', 'replace') |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2516
diff
changeset
|
510 |
strings.append(u'  %s<br/>\n' % (string)) |
0 | 511 |
# add locals info for each entry |
512 |
try: |
|
513 |
local_context = tcbk.tb_frame.f_locals |
|
514 |
html_info = [] |
|
515 |
chars = 0 |
|
10662
10942ed172de
[py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10617
diff
changeset
|
516 |
for name, value in local_context.items(): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
517 |
value = xml_escape(repr(value)) |
0 | 518 |
info = u'<span class="name">%s</span>=%s, ' % (name, value) |
519 |
line_length = len(name) + len(value) |
|
520 |
chars += line_length |
|
521 |
# 150 is the result of *years* of research ;-) (CSS might be helpful here) |
|
522 |
if chars > 150: |
|
523 |
info = u'<br/>' + info |
|
524 |
chars = line_length |
|
525 |
html_info.append(info) |
|
526 |
boxid = 'ctxlevel%d' % index |
|
527 |
strings.append(u'[%s]' % toggle_link(boxid, '+')) |
|
528 |
strings.append(u'<div id="%s" class="pycontext hidden">%s</div>' % |
|
529 |
(boxid, ''.join(html_info))) |
|
530 |
tcbk = tcbk.tb_next |
|
531 |
except Exception: |
|
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
532 |
pass # doesn't really matter if we have no context info |
0 | 533 |
strings.append(u'</div>') |
534 |
return '\n'.join(strings) |
|
535 |
||
536 |
# csv files / unicode support ################################################# |
|
537 |
||
538 |
class UnicodeCSVWriter: |
|
10695
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
539 |
"""proxies calls to csv.writer.writerow to be able to deal with unicode |
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
540 |
|
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
541 |
Under Python 3, this code no longer encodes anything.""" |
1581
80ee6397c087
fix rest import, html_escape in null rest_publish
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
542 |
|
0 | 543 |
def __init__(self, wfunc, encoding, **kwargs): |
544 |
self.writer = csv.writer(self, **kwargs) |
|
545 |
self.wfunc = wfunc |
|
546 |
self.encoding = encoding |
|
547 |
||
548 |
def write(self, data): |
|
549 |
self.wfunc(data) |
|
550 |
||
551 |
def writerow(self, row): |
|
10695
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
552 |
if PY3: |
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
553 |
self.writer.writerow(row) |
321b99973b69
[web/views] Port csvexport views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10689
diff
changeset
|
554 |
return |
0 | 555 |
csvrow = [] |
556 |
for elt in row: |
|
10689
49a62b8f6d43
[py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10662
diff
changeset
|
557 |
if isinstance(elt, text_type): |
0 | 558 |
csvrow.append(elt.encode(self.encoding)) |
559 |
else: |
|
560 |
csvrow.append(str(elt)) |
|
561 |
self.writer.writerow(csvrow) |
|
562 |
||
563 |
def writerows(self, rows): |
|
564 |
for row in rows: |
|
565 |
self.writerow(row) |
|
566 |
||
567 |
||
568 |
# some decorators ############################################################# |
|
569 |
||
570 |
class limitsize(object): |
|
571 |
def __init__(self, maxsize): |
|
572 |
self.maxsize = maxsize |
|
573 |
||
574 |
def __call__(self, function): |
|
575 |
def newfunc(*args, **kwargs): |
|
576 |
ret = function(*args, **kwargs) |
|
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10332
diff
changeset
|
577 |
if isinstance(ret, string_types): |
0 | 578 |
return ret[:self.maxsize] |
579 |
return ret |
|
580 |
return newfunc |
|
581 |
||
582 |
||
583 |
def htmlescape(function): |
|
584 |
def newfunc(*args, **kwargs): |
|
585 |
ret = function(*args, **kwargs) |
|
10612
84468b90e9c1
[py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10332
diff
changeset
|
586 |
assert isinstance(ret, string_types) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2208
diff
changeset
|
587 |
return xml_escape(ret) |
0 | 588 |
return newfunc |