summaryrefslogtreecommitdiff
path: root/app/lib/utils/strutils.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
committerluxagraf <sng@luxagraf.net>2012-09-22 22:27:04 -0400
commitefb623af0bcb47d510501c282e1326b11343a29c (patch)
tree3a35fb19f5eba3b219c65277a5fb712cbe9604ac /app/lib/utils/strutils.py
parent0b481fd7931c2ae20ca21f89a87f2ba6a6c01e10 (diff)
site reorg
Diffstat (limited to 'app/lib/utils/strutils.py')
-rw-r--r--app/lib/utils/strutils.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/app/lib/utils/strutils.py b/app/lib/utils/strutils.py
new file mode 100644
index 0000000..368d3d8
--- /dev/null
+++ b/app/lib/utils/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 &nbsp; &gt; etc."""
+ return re.sub(r'&(#?)(.+?);',convertentity,s)