1

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?

rhonda
  • 99
  • 2
  • 6

1 Answers1

2

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()
EricLavault
  • 12,130
  • 3
  • 23
  • 45
  • Hi @EricLavault, Thank you. I have tried using `get(key[,default])` and it works with my code with `writer.unparse`. I tried using ` def handle(self, dn, entry)` but nothing gets printed to output.ldif file. I might be missing something. P.S: I am new to programming. – rhonda Mar 19 '21 at 16:36
  • When calling `parser.parse()`, the `handle` method will be called for each entry found in the input file (the important line is `self.writer.unparse(dn, entry);` which writes to the output), you need to define this method in your MyLDIF class. – EricLavault Mar 19 '21 at 17:03
  • somehow `handle` block is not getting executed. I tried `def handle(self,dn,entry): print("testing block entry")` and this doesn't get printed. :( – rhonda Mar 19 '21 at 17:49
  • Mmmmh.. maybe an indentation issue after copy/pasting the code ? Double check the number of spaces/tabs in each code block. – EricLavault Mar 20 '21 at 10:07
  • Sorry for replying this late. I was traveling. Yes, I have checked the indentation. Seems just fine to me, but the code isn't executing :( – rhonda Mar 23 '21 at 14:12