diff options
author | luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f <luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f> | 2010-07-04 00:42:45 +0000 |
---|---|---|
committer | luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f <luxagraf@c63593aa-01b0-44d9-8516-4b9c7e931d7f> | 2010-07-04 00:42:45 +0000 |
commit | a4abbbc5589e948e469849c58b67d179212785d0 (patch) | |
tree | 4d7e3768248ffa328726fdc44074f0d6e8fd5b49 /lib/utilslib/strutils.py | |
parent | 0fd90e919df654447885c866c739f33790d0469b (diff) |
fuck up
Diffstat (limited to 'lib/utilslib/strutils.py')
-rw-r--r-- | lib/utilslib/strutils.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/utilslib/strutils.py b/lib/utilslib/strutils.py new file mode 100644 index 0000000..368d3d8 --- /dev/null +++ b/lib/utilslib/strutils.py @@ -0,0 +1,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) |