ProtectUTF8.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » util » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » util » ProtectUTF8.py
"""Temporarily convert a UTF-8 string to Unicode to prevent breakage.

WARNING: This code is duplicated!  Please update both::

    godspeed/godlib/protect_utf8.py
    godspeed/ui/webui/aquarium/aquarium/util/ProtectUTF8.py

The code needs to be duplicated because it must be used in two completely
separate projects that can't share a dependency.

"""

__docformat__ = "restructuredtext"

# Created: Fri Apr 28 07:08:49 PDT 2006
# Author: Shannon -jj Behrens
# Email: jjinux@users.sourceforge.net
#
# Copyright (c) Shannon -jj Behrens.  All rights reserved.


def protect_utf8(wrapped_function, encoding='UTF-8'):

    """Temporarily convert a UTF-8 string to Unicode to prevent breakage.

    ``protect_utf8`` is a function decorator that can prevent naive
    functions from breaking UTF-8.

    If the wrapped function takes a string, and that string happens to be valid
    UTF-8, convert it to a unicode object and call the wrapped function.  If a
    conversion was done and if a unicode object was returned, convert it back
    to a UTF-8 string.

    The wrapped function should take a string as its first parameter and it may
    return an object of the same type.  Anything else is optional.  For
    example::

        def truncate(s):
            return s[:1]

    Pass ``encoding`` if you want to protect something other than UTF-8.

    Ideally, we'd have unicode objects everywhere, but sometimes life is not
    ideal. :)

    """

    def proxy_function(s, *args, **kargs):
        unconvert = False
        if isinstance(s, str):
            try:
                s = s.decode(encoding)
    unconvert = True
      except UnicodeDecodeError:
          pass
  ret = wrapped_function(s, *args, **kargs)
  if unconvert and isinstance(ret, unicode):
      ret = ret.encode(encoding)
  return ret

    return proxy_function


def truncate(s, length=1, etc="..."):
    """Truncate a string to the given length.

    If truncation is necessary, append the value of ``etc``.

    This is really just a silly test.

    """
    if len(s) < length:
        return s
    else:
        return s[:length] + etc
truncate = protect_utf8(truncate)           # I'm stuck on Python 2.3.


if __name__ == '__main__':
    assert (truncate('\xe3\x82\xa6\xe3\x82\xb6\xe3\x83\x86', etc="") ==
            '\xe3\x82\xa6')
    assert truncate('abc') == 'a...'
    assert truncate(u'\u30a0\u30b1\u30c3', etc="") == u'\u30a0'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.