ldif.rst 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. .. % $Id: ldif.rst,v 1.6 2011/02/19 13:04:41 stroeder Exp $
  2. #####################################
  3. :mod:`ldif` LDIF parser and generator
  4. #####################################
  5. .. module:: ldif
  6. :synopsis: Parses and generates LDIF files
  7. .. moduleauthor:: python-ldap project (see http://www.python-ldap.org/)
  8. This module parses and generates LDAP data in the format LDIF. It is
  9. implemented in pure Python and does not rely on any non-standard modules.
  10. Therefore it can be used stand-alone without the rest of the python-ldap
  11. package.
  12. .. seealso::
  13. :rfc:`2849` - The LDAP Data Interchange Format (LDIF) - Technical Specification
  14. .. _ldif-example:
  15. Example
  16. ^^^^^^^^
  17. The following example demonstrates how to write LDIF output
  18. of an LDAP entry with :mod:`ldif` module.
  19. >>> import sys,ldif
  20. >>> entry={'objectClass':['top','person'],'cn':['Michael Stroeder'],'sn':['Stroeder']}
  21. >>> dn='cn=Michael Stroeder,ou=Test'
  22. >>> ldif_writer=ldif.LDIFWriter(sys.stdout)
  23. >>> ldif_writer.unparse(dn,entry)
  24. dn: cn=Michael Stroeder,ou=Test
  25. cn: Michael Stroeder
  26. objectClass: top
  27. objectClass: person
  28. sn: Stroeder
  29. The following example demonstrates how to parse an LDIF file
  30. with :mod:`ldif` module, skip some entries and write the result to stdout. ::
  31. import sys
  32. from ldif import LDIFParser,LDIFWriter
  33. SKIP_DN = ["uid=foo,ou=People,dc=example,dc=com",
  34. "uid=bar,ou=People,dc=example,dc=com"]
  35. class MyLDIF(LDIFParser):
  36. def __init__(self,input,output):
  37. LDIFParser.__init__(self,input)
  38. self.writer = LDIFWriter(output)
  39. def handle(self,dn,entry):
  40. if dn in SKIP_DN:
  41. return
  42. self.writer.unparse(dn,entry)
  43. parser = MyLDIF(open("input.ldif", 'rb'), sys.stdout)
  44. parser.parse()