ldif.rst 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. .. % $Id: ldif.rst,v 1.8 2011/09/14 18:29:18 stroeder Exp $
  2. #####################################
  3. :mod:`ldif` LDIF parser and generator
  4. #####################################
  5. .. py: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. Functions
  15. ^^^^^^^^^
  16. .. autofunction:: ldif.CreateLDIF
  17. .. autofunction:: ldif.ParseLDIF
  18. Classes
  19. ^^^^^^^
  20. .. autoclass:: ldif.LDIFWriter
  21. .. autoclass:: ldif.LDIFParser
  22. .. autoclass:: LDIFRecordList
  23. .. autoclass:: LDIFCopy
  24. .. _ldif-example:
  25. Example
  26. ^^^^^^^
  27. The following example demonstrates how to write LDIF output
  28. of an LDAP entry with :mod:`ldif` module.
  29. >>> import sys,ldif
  30. >>> entry={'objectClass':['top','person'],'cn':['Michael Stroeder'],'sn':['Stroeder']}
  31. >>> dn='cn=Michael Stroeder,ou=Test'
  32. >>> ldif_writer=ldif.LDIFWriter(sys.stdout)
  33. >>> ldif_writer.unparse(dn,entry)
  34. dn: cn=Michael Stroeder,ou=Test
  35. cn: Michael Stroeder
  36. objectClass: top
  37. objectClass: person
  38. sn: Stroeder
  39. The following example demonstrates how to parse an LDIF file
  40. with :mod:`ldif` module, skip some entries and write the result to stdout. ::
  41. import sys
  42. from ldif import LDIFParser,LDIFWriter
  43. SKIP_DN = ["uid=foo,ou=People,dc=example,dc=com",
  44. "uid=bar,ou=People,dc=example,dc=com"]
  45. class MyLDIF(LDIFParser):
  46. def __init__(self,input,output):
  47. LDIFParser.__init__(self,input)
  48. self.writer = LDIFWriter(output)
  49. def handle(self,dn,entry):
  50. if dn in SKIP_DN:
  51. return
  52. self.writer.unparse(dn,entry)
  53. parser = MyLDIF(open("input.ldif", 'rb'), sys.stdout)
  54. parser.parse()