uilib.py
changeset 10689 49a62b8f6d43
parent 10662 10942ed172de
child 10695 321b99973b69
equal deleted inserted replaced
10688:fa29f3628a1b 10689:49a62b8f6d43
    26 
    26 
    27 import csv
    27 import csv
    28 import re
    28 import re
    29 from io import StringIO
    29 from io import StringIO
    30 
    30 
    31 from six import string_types, integer_types
    31 from six import PY3, text_type, binary_type, string_types, integer_types
    32 
    32 
    33 from logilab.mtconverter import xml_escape, html_unescape
    33 from logilab.mtconverter import xml_escape, html_unescape
    34 from logilab.common.date import ustrftime
    34 from logilab.common.date import ustrftime
    35 from logilab.common.deprecation import deprecated
    35 from logilab.common.deprecation import deprecated
    36 
    36 
    62     if props is not None and value and props.get('internationalizable'):
    62     if props is not None and value and props.get('internationalizable'):
    63         return req._(value)
    63         return req._(value)
    64     return value
    64     return value
    65 
    65 
    66 def print_int(value, req, props, displaytime=True):
    66 def print_int(value, req, props, displaytime=True):
    67     return unicode(value)
    67     return text_type(value)
    68 
    68 
    69 def print_date(value, req, props, displaytime=True):
    69 def print_date(value, req, props, displaytime=True):
    70     return ustrftime(value, req.property_value('ui.date-format'))
    70     return ustrftime(value, req.property_value('ui.date-format'))
    71 
    71 
    72 def print_time(value, req, props, displaytime=True):
    72 def print_time(value, req, props, displaytime=True):
   122     if value:
   122     if value:
   123         return req._('yes')
   123         return req._('yes')
   124     return req._('no')
   124     return req._('no')
   125 
   125 
   126 def print_float(value, req, props, displaytime=True):
   126 def print_float(value, req, props, displaytime=True):
   127     return unicode(req.property_value('ui.float-format') % value)
   127     return text_type(req.property_value('ui.float-format') % value) # XXX cast needed ?
   128 
   128 
   129 PRINTERS = {
   129 PRINTERS = {
   130     'Bytes': print_bytes,
   130     'Bytes': print_bytes,
   131     'String': print_string,
   131     'String': print_string,
   132     'Int': print_int,
   132     'Int': print_int,
   389 
   389 
   390 HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param',
   390 HTML4_EMPTY_TAGS = frozenset(('base', 'meta', 'link', 'hr', 'br', 'param',
   391                               'img', 'area', 'input', 'col'))
   391                               'img', 'area', 'input', 'col'))
   392 
   392 
   393 def sgml_attributes(attrs):
   393 def sgml_attributes(attrs):
   394     return u' '.join(u'%s="%s"' % (attr, xml_escape(unicode(value)))
   394     return u' '.join(u'%s="%s"' % (attr, xml_escape(text_type(value)))
   395                      for attr, value in sorted(attrs.items())
   395                      for attr, value in sorted(attrs.items())
   396                      if value is not None)
   396                      if value is not None)
   397 
   397 
   398 def simple_sgml_tag(tag, content=None, escapecontent=True, **attrs):
   398 def simple_sgml_tag(tag, content=None, escapecontent=True, **attrs):
   399     """generation of a simple sgml tag (eg without children tags) easier
   399     """generation of a simple sgml tag (eg without children tags) easier
   407         except KeyError:
   407         except KeyError:
   408             pass
   408             pass
   409         value += u' ' + sgml_attributes(attrs)
   409         value += u' ' + sgml_attributes(attrs)
   410     if content:
   410     if content:
   411         if escapecontent:
   411         if escapecontent:
   412             content = xml_escape(unicode(content))
   412             content = xml_escape(text_type(content))
   413         value += u'>%s</%s>' % (content, tag)
   413         value += u'>%s</%s>' % (content, tag)
   414     else:
   414     else:
   415         if tag in HTML4_EMPTY_TAGS:
   415         if tag in HTML4_EMPTY_TAGS:
   416             value += u' />'
   416             value += u' />'
   417         else:
   417         else:
   436     from logilab.common.ureports import HTMLWriter
   436     from logilab.common.ureports import HTMLWriter
   437     formater = HTMLWriter(True)
   437     formater = HTMLWriter(True)
   438     stream = StringIO() #UStringIO() don't want unicode assertion
   438     stream = StringIO() #UStringIO() don't want unicode assertion
   439     formater.format(layout, stream)
   439     formater.format(layout, stream)
   440     res = stream.getvalue()
   440     res = stream.getvalue()
   441     if isinstance(res, str):
   441     if isinstance(res, binary_type):
   442         res = unicode(res, 'UTF8')
   442         res = res.decode('UTF8')
   443     return res
   443     return res
   444 
   444 
   445 # traceback formatting ########################################################
   445 # traceback formatting ########################################################
   446 
   446 
   447 import traceback
   447 import traceback
   448 
   448 
   449 def exc_message(ex, encoding):
   449 def exc_message(ex, encoding):
   450     try:
   450     if PY3:
   451         excmsg = unicode(ex)
   451         excmsg = str(ex)
   452     except Exception:
   452     else:
   453         try:
   453         try:
   454             excmsg = unicode(str(ex), encoding, 'replace')
   454             excmsg = unicode(ex)
   455         except Exception:
   455         except Exception:
   456             excmsg = unicode(repr(ex), encoding, 'replace')
   456             try:
   457     exctype = unicode(ex.__class__.__name__)
   457                 excmsg = unicode(str(ex), encoding, 'replace')
       
   458             except Exception:
       
   459                 excmsg = unicode(repr(ex), encoding, 'replace')
       
   460     exctype = ex.__class__.__name__
   458     return u'%s: %s' % (exctype, excmsg)
   461     return u'%s: %s' % (exctype, excmsg)
   459 
   462 
   460 
   463 
   461 def rest_traceback(info, exception):
   464 def rest_traceback(info, exception):
   462     """return a unicode ReST formated traceback"""
   465     """return a unicode ReST formated traceback"""
   539         self.wfunc(data)
   542         self.wfunc(data)
   540 
   543 
   541     def writerow(self, row):
   544     def writerow(self, row):
   542         csvrow = []
   545         csvrow = []
   543         for elt in row:
   546         for elt in row:
   544             if isinstance(elt, unicode):
   547             if isinstance(elt, text_type):
   545                 csvrow.append(elt.encode(self.encoding))
   548                 csvrow.append(elt.encode(self.encoding))
   546             else:
   549             else:
   547                 csvrow.append(str(elt))
   550                 csvrow.append(str(elt))
   548         self.writer.writerow(csvrow)
   551         self.writer.writerow(csvrow)
   549 
   552