1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#####################################
:mod:`ldif` LDIF parser and generator
#####################################
.. py:module:: ldif
:synopsis: Parses and generates LDIF files
.. moduleauthor:: python-ldap project (see https://www.python-ldap.org/)
This module parses and generates LDAP data in the format LDIF. It is
implemented in pure Python and does not rely on any non-standard modules.
Therefore it can be used stand-alone without the rest of the python-ldap
package.
.. seealso::
:rfc:`2849` - The LDAP Data Interchange Format (LDIF) - Technical Specification
Functions
^^^^^^^^^
.. autofunction:: ldif.CreateLDIF
.. deprecated:: 3.0
``ldif.CreateLDIF()`` is deprecated. It will be removed in version 3.1.
Use :meth:`ldif.LDIFWriter.unparse` with a file or ``io.StringIO``
instead.
.. autofunction:: ldif.ParseLDIF
.. deprecated:: 3.0
``ldif.ParseLDIF()`` is deprecated. It will be removed in version 3.1.
Use the ``all_records`` attribute of the returned value of
``ldif.LDIFRecordList.parse()`` instead.
Classes
^^^^^^^
.. autoclass:: ldif.LDIFWriter
:members:
.. autoclass:: ldif.LDIFParser
:members:
.. autoclass:: LDIFRecordList
:members:
.. autoclass:: LDIFCopy
:members:
.. _ldif-example:
Example
^^^^^^^
The following example demonstrates how to write LDIF output
of an LDAP entry with :mod:`ldif` module.
>>> import sys, ldif
>>> entry={'objectClass': [b'top', b'person'], 'cn': [b'Michael Stroeder'], 'sn': [b'Stroeder']}
>>> dn='cn=Michael Stroeder,ou=Test'
>>> ldif_writer=ldif.LDIFWriter(sys.stdout)
>>> ldif_writer.unparse(dn, entry)
dn: cn=Michael Stroeder,ou=Test
cn: Michael Stroeder
objectClass: top
objectClass: person
sn: Stroeder
The following example demonstrates how to parse an LDIF file
with :mod:`ldif` module, skip some entries and write the result to stdout. ::
import sys
from ldif import LDIFParser,LDIFWriter
SKIP_DN = ["uid=foo,ou=People,dc=example,dc=com",
"uid=bar,ou=People,dc=example,dc=com"]
class MyLDIF(LDIFParser):
def __init__(self,input,output):
LDIFParser.__init__(self,input)
self.writer = LDIFWriter(output)
def handle(self,dn,entry):
if dn in SKIP_DN:
return
self.writer.unparse(dn,entry)
parser = MyLDIF(open("input.ldif", 'rb'), sys.stdout)
parser.parse()
|