Ejemplo n.º 1
0
    def test_replace_non_printable(self):
        """Test :func:`datagrid.utils.stringutils.replace_non_printable`."""
        self.assertEqual(
            replace_non_printable(
                "Some string\nWith\tsome %s non-printable, %s chars" % (
                    chr(20), chr(30))),
            u"Some string\nWith\tsome � non-printable, � chars")

        self.assertEqual(
            replace_non_printable(
                u"Ração %s para %s búfalos" % (chr(20), chr(30))),
            u"Ração � para � búfalos")
Ejemplo n.º 2
0
    def test_replace_non_printable(self):
        """Test :func:`datagrid.utils.stringutils.replace_non_printable`."""
        self.assertEqual(
            replace_non_printable(
                "Some string\nWith\tsome %s non-printable, %s chars" %
                (chr(20), chr(30))),
            u"Some string\nWith\tsome � non-printable, � chars")

        self.assertEqual(
            replace_non_printable(u"Ração %s para %s búfalos" %
                                  (chr(20), chr(30))),
            u"Ração � para � búfalos")
Ejemplo n.º 3
0
def string_transform(value, max_length=None, oneline=True,
                     decode_fallback=None):
    """String transformation.

    :param object value: the value that will be converted to
        a string
    :param int max_length: if not `None`, will be used to
        ellipsize the string if greater than that.
    :param bool oneline: if we should join all the lines together
        in one line
    :param callable decode_fallback: a callable to use
        to decode value in case it cannot be converted to unicode directly
    :return: the string representation of the value
    :rtype: str
    """
    if value is None:
        return '<NULL>'

    if isinstance(value, str):
        value = unicode(value, 'utf-8', 'replace')
    else:
        try:
            value = unicode(value)
        except UnicodeDecodeError:
            if decode_fallback is None:
                raise
            value = decode_fallback(value)

    # Replace non-printable characters on the string so the user will
    # know that there's something there even though it is not printable.
    value = stringutils.replace_non_printable(value)

    if oneline:
        value = u' '.join(v.strip() for v in value.splitlines() if v.strip())

    # Don't show more than max_length chars in treeview. Helps with performance
    if max_length is not None and len(value) > max_length:
        value = u'%s [...]' % (value[:max_length], )

    # At the end, if value is unicode, it needs to be converted to
    # an utf-8 encoded str or it won't be rendered in the treeview.
    return value.encode('utf-8')