Hi all,
i've improve ldif parser who can manage delete action now.
I think he can be usefull to someone else.

thanks
-- 
Nicolas Delaby
Nexedi: Consulting and Development of Libre / Open Source Software
http://www.nexedi.com/
--- ldif.py	2007-08-17 15:43:51.000000000 +0000
+++ ldif-erp5.py	2007-08-17 15:44:00.000000000 +0000
@@ -25,6 +25,7 @@
 ]
 
 import string,urlparse,urllib,base64,re,types
+import ldap
 
 try:
   from cStringIO import StringIO
@@ -52,6 +53,9 @@
 for c in CHANGE_TYPES:
   valid_changetype_dict[c]=None
 
+MODIFY_MAPPING_DICT = {'add'      : ldap.MOD_ADD,
+                       #'replace'  : ldap.MOD_REPLACE,
+                       'delete'   : ldap.MOD_DELETE}
 
 SAFE_STRING_PATTERN = '(^(\000|\n|\r| |:|<)|[\000\n\r\200-\377]+|[ ]+$)'
 safe_string_re = re.compile(SAFE_STRING_PATTERN)
@@ -365,22 +369,38 @@
         if attr_type=='dn':
           # attr type and value pair was DN of LDIF record
           if dn!=None:
-	    raise ValueError, 'Two lines starting with dn: in one record.'
+            raise ValueError, 'Two lines starting with dn: in one record.'
           if not is_dn(attr_value):
-	    raise ValueError, 'No valid string-representation of distinguished name %s.' % (repr(attr_value))
+            raise ValueError, 'No valid string-representation of distinguished name %s.' % (repr(attr_value))
           dn = attr_value
         elif attr_type=='version' and dn is None:
           version = 1
         elif attr_type=='changetype':
           # attr type and value pair was DN of LDIF record
           if dn is None:
-	    raise ValueError, 'Read changetype: before getting valid dn: line.'
+            raise ValueError, 'Read changetype: before getting valid dn: line.'
           if changetype!=None:
-	    raise ValueError, 'Two lines starting with changetype: in one record.'
+            raise ValueError, 'Two lines starting with changetype: in one record.'
           if not valid_changetype_dict.has_key(attr_value):
-	    raise ValueError, 'changetype value %s is invalid.' % (repr(attr_value))
-          dn = attr_value
-        elif attr_value!=None and \
+            raise ValueError, 'changetype value %s is invalid.' % (repr(attr_value))
+          changetype = attr_value
+          attr_type, attr_value = self._parseAttrTypeandValue()
+          modify_list = []
+          entry[changetype] = []
+          while (attr_type and attr_value) is not None:
+            mod_op = MODIFY_MAPPING_DICT[attr_type]
+            mod_type = attr_value
+            multivalued_list = []
+            while attr_value is not None:
+              attr_type, attr_value = self._parseAttrTypeandValue()
+              if attr_value is not None:
+                multivalued_list.append(attr_value)
+            modify_list.append((mod_op, mod_type, multivalued_list))
+            entry[changetype] = [modify_list]
+            attr_type, attr_value = self._parseAttrTypeandValue()
+          #don't add new entry for the same dn
+          break
+        elif attr_value not in (None, '') and \
              not self._ignored_attr_types.has_key(string.lower(attr_type)):
           # Add the attribute to the entry if not ignored attribute
           if entry.has_key(attr_type):
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Python-LDAP-dev mailing list
Python-LDAP-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/python-ldap-dev

Reply via email to