summaryrefslogtreecommitdiffstats
path: root/ipa-python/ipautil.py
diff options
context:
space:
mode:
authorKarl MacMillan <kmacmillan@mentalrootkit.com>2007-08-28 09:58:10 -0400
committerKarl MacMillan <kmacmillan@mentalrootkit.com>2007-08-28 09:58:10 -0400
commit6eea6664e079d187c3b0420b4283af35205d3b03 (patch)
treec579e484bd63dfc8d9d5db1369b36554ddb06531 /ipa-python/ipautil.py
parente31b526c8174e7c55f69b1fdf31a6ee78197e8bc (diff)
downloadfreeipa-6eea6664e079d187c3b0420b4283af35205d3b03.tar.gz
freeipa-6eea6664e079d187c3b0420b4283af35205d3b03.tar.xz
freeipa-6eea6664e079d187c3b0420b4283af35205d3b03.zip
This patch wraps binary data in an xmlrpclib Binary object. This
removes the need for LDIF conversion. It will make TurboGears direct code faster, but should keep xmlrpc about the same speed. The patch also swaps out ldap.cidict for the IPA CIDict class. IPA code should only use the CIDict class now.
Diffstat (limited to 'ipa-python/ipautil.py')
-rw-r--r--ipa-python/ipautil.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/ipa-python/ipautil.py b/ipa-python/ipautil.py
index 51337a8eb..74f7cfff6 100644
--- a/ipa-python/ipautil.py
+++ b/ipa-python/ipautil.py
@@ -18,6 +18,8 @@
#
from string import lower
+import re
+import xmlrpclib
class CIDict(dict):
"""
@@ -106,3 +108,65 @@ class CIDict(dict):
return (key,value)
+#
+# The safe_string_re regexp and needs_base64 function are extracted from the
+# python-ldap ldif module, which was
+# written by Michael Stroeder <michael@stroeder.com>
+# http://python-ldap.sourceforge.net
+#
+# It was extracted because ipaldap.py is naughtily reaching into the ldif
+# module and squashing this regexp.
+#
+SAFE_STRING_PATTERN = '(^(\000|\n|\r| |:|<)|[\000\n\r\200-\377]+|[ ]+$)'
+safe_string_re = re.compile(SAFE_STRING_PATTERN)
+
+def needs_base64(s):
+ """
+ returns 1 if s has to be base-64 encoded because of special chars
+ """
+ return not safe_string_re.search(s) is None
+
+
+def wrap_binary_data(data):
+ """Converts all binary data strings into Binary objects for transport
+ back over xmlrpc."""
+ if isinstance(data, str):
+ if needs_base64(data):
+ return xmlrpclib.Binary(data)
+ else:
+ return data
+ elif isinstance(data, list) or isinstance(data,tuple):
+ retval = []
+ for value in data:
+ retval.append(wrap_binary_data(value))
+ return retval
+ elif isinstance(data, dict):
+ retval = {}
+ for (k,v) in data.iteritems():
+ retval[k] = wrap_binary_data(v)
+ return retval
+ else:
+ return data
+
+
+def unwrap_binary_data(data):
+ """Converts all Binary objects back into strings."""
+ if isinstance(data, xmlrpclib.Binary):
+ # The data is decoded by the xmlproxy, but is stored
+ # in a binary object for us.
+ return str(data)
+ elif isinstance(data, str):
+ return data
+ elif isinstance(data, list) or isinstance(data,tuple):
+ retval = []
+ for value in data:
+ retval.append(unwrap_binary_data(value))
+ return retval
+ elif isinstance(data, dict):
+ retval = {}
+ for (k,v) in data.iteritems():
+ retval[k] = unwrap_binary_data(v)
+ return retval
+ else:
+ return data
+