summaryrefslogtreecommitdiff
path: root/lib/utilslib/strutils.py
blob: 368d3d8cf6f170f411835d36a3d86ce42af5b73e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#
# String/unicode conversion utils.
#
    
def safestr(s):
    """
    Safely corerce *anything* to a string. If the object can't be str'd, an
    empty string will be returned.
    
    You can (and I do) use this for really crappy unicode handling, but it's
    a bit like killing a mosquito with a bazooka.
    """
    if s is None:
        return ""
    if isinstance(s, unicode):
        return s.encode('ascii', 'xmlcharrefreplace')
    else:
        try:
            return str(s)
        except:
            return ""
            
def safeint(s):
    """Like safestr(), but always returns an int. Returns 0 on failure."""
    try:
        return int(safestr(s))
    except ValueError:
        return 0


def convertentity(m):
    import htmlentitydefs
    """Convert a HTML entity into normal string (ISO-8859-1)"""
    if m.group(1)=='#':
        try:
            return chr(int(m.group(2)))
        except ValueError:
            return '&#%s;' % m.group(2)
    try:
        return htmlentitydefs.entitydefs[m.group(2)]
    except KeyError:
        return '&%s;' % m.group(2)

def unquotehtml(s):
    import re
    """Convert a HTML quoted string into normal string (ISO-8859-1).
   
    Works with &#XX; and with   > etc."""
    return re.sub(r'&(#?)(.+?);',convertentity,s)