Use Python's LDIF Parser to skip some attributes and write to a new LDIF file

Viewed 338

I am using the python LDIF parser (link) to edit some attributes and generate a modified version of the file.

My LDIF file is

# entry-id: 1
dn: cn=Directory Administrators, dc=organization,dc=corp
nsUniqueId: 2947f1b3-1dd211b2-80b89250-3a51c428
objectClass: top
objectClass: groupofuniquenames
cn: Directory Administrators
creatorsName: cn=directory manager
modifiersName: cn=directory manager
createTimestamp: 20151110180921Z
modifyTimestamp: 20151110180921Z

# entry-id: 2
dn: cn=214-All-Matrix-100342-ALXD,ou=groups,dc=organization,dc=corp
modifyTimestamp: 20190905182416Z
modifiersName: cn=directory manager
owner: uid=hwuebker,ou=people,dc=organization,dc=corp
nsUniqueId: 4350c83d-1dd211b2-80a59250-3a51c428
uniqueMember: uid=anthonys,ou=people,dc=organization,dc=corp
GroupType: Core
Application: AppUID
adminGroupAdmin: cn=IDM System Managers,ou=groups,dc=organization,dc=corp

# entry-id: 3
dn: uid=twalsh,ou=people,dc=organization,dc=corp
nsUniqueId: 3df58701-1dd211b2-80489250-3a51c428
modifyTimestamp: 20180606194655Z
modifiersName: cn=directory manager
initials: 1
Document: 0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAAC
 AAAAAQAAAAAAAAAAEAAAAgAAAAIAAAD+////AAAAAAAAAACAAAAA////////////////////////
 /////////////////////////////////////////////////////////AAAAAA==
Document: RE  E-mail details .msg

My Python Code sofar for LDIF parser is

parser = MyLDIF(open(fileName, 'rb'), sys.stdout)
parser.parse()

#LDIF Parser go through records
for dn, entry in parser.parse():
    
        Doc = entry['Document']
        uidList= entry['uid']  
        uid = uidList[0]
        DocValue = Doc[0]
        DocSource = Doc[1]
    
        writer = LDIFWriter(open("data.ldif", "ab"))
        writer.unparse(dn, {
            "nsUniqueId": entry['nsUniqueId'],  
            "Docs": entry['Document'],

        })

As the 1st & 2nd entry doesn't have a DOCUMENT attribute, this throws an error.

Doc = entry['Document']
  KeyError: 'Document'  

How can I manipulate the 3rd entry while copying the 1st and 2nd entry without any changes?

1 Answers

You can use the built-in method get(key[,default]) to get the value for key in a dictionary or a default value if key is not set.

I suggest you use the handle method in your class to achieve this, and also directly write to the output file (instead of writing to stdout and creating another writer for the output file, you will still be able to print data from the handle method) :

class MyLDIF(LDIFParser):
   def __init__(self, input, output):
      LDIFParser.__init__(self, input)
      self.writer = LDIFWriter(output)

   def handle(self, dn, entry):
      if entry.get('Document', False):
          # modify entry if 'Document' is set
          entry['attribute1'] = 'value1'
          entry['attribute2'] = 'value2'
          # ...
      self.writer.unparse(dn, entry)

parser = MyLDIF(open('data.ldif', 'r'), open('output.ldif', 'w'))
parser.parse()
Related