ldif LDIF parser and generator

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.

See also

RFC 2849 - The LDAP Data Interchange Format (LDIF) - Technical Specification

Functions

ldif.CreateLDIF(dn: str, record: MutableMapping[str, list[bytes]] | Sequence[tuple[str, list[bytes]] | tuple[int, str, bytes | list[bytes] | None]], base64_attrs: list[str] | None = None, cols: int = 76) → str

Create LDIF single formatted record including trailing empty line. This is a compatibility function.

dn

string-representation of distinguished name

record

Either a dictionary holding the LDAP entry {attrtype:record} or a list with a modify list like for LDAPObject.modify().

base64_attrs

list of attribute types to be base64-encoded in any case

cols

Specifies how many columns a line may have before it’s folded into many lines.

Deprecated since version 3.0: ldif.CreateLDIF() is deprecated. It will be removed in version 3.1. Use ldif.LDIFWriter.unparse() with a file or io.StringIO instead.

ldif.ParseLDIF(f: TextIO | BinaryIO, ignore_attrs: list[str] | None = None, maxentries: int = 0) → list[tuple[str, MutableMapping[str, list[bytes]]]]

Parse LDIF records read from file. This is a compatibility function.

Deprecated since version 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

class ldif.LDIFWriter(output_file: TextIO, base64_attrs: list[str] | None = None, cols: int = 76, line_sep: str = '\n')

Write LDIF entry or change records to file object Copy LDIF input to a file output object containing all data retrieved via URLs

unparse(dn: str, record: MutableMapping[str, list[bytes]] | Sequence[tuple[str, list[bytes]] | tuple[int, str, bytes | list[bytes] | None]]) → None
dn

string-representation of distinguished name

record

Either a dictionary holding the LDAP entry {attrtype:record} or a list with a modify list like for LDAPObject.modify().

class ldif.LDIFParser(input_file: TextIO | BinaryIO, ignored_attr_types: list[str] | None = None, max_entries: int = 0, process_url_schemes: list[str] | None = None, line_sep: str = '\n')

Base class for a LDIF parser. Applications should sub-class this class and override method handle() to implement something meaningful.

Public class attributes:

records_read

Counter for records processed so far

handle(dn: str, entry: MutableMapping[str, list[bytes]]) → str | None

Process a single content LDIF record. This method should be implemented by applications using LDIFParser.

handle_add(dn: str, entry: MutableMapping[str, list[bytes]], controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single add operation. This method should be implemented by applications using LDIFParser.

Parameters:
  • dn (str) – DN of the new object to be created

  • entry (dict) – Data of the new object to be created

handle_delete(dn: str, controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single delete operation. This method should be implemented by applications using LDIFParser.

Parameters:

dn (str) – DN of the existing object to be deleted

handle_modify(dn: str, modops: Sequence[tuple[str, list[bytes]] | tuple[int, str, bytes | list[bytes] | None]], controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single modify operation. This method should be implemented by applications using LDIFParser.

handle_modrdn(dn: str, newrdn: str, deleteoldrdn: bool, newsuperior: str | None = None, controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single modrdn/rename operation. This method should be implemented by applications using LDIFParser.

Parameters:
  • dn (str) – DN of the existing object to be renamed/moved

  • newrdn (str) – RDN of the new object

  • deleteoldrdn (bool) – Whether the old RDN value(s) should be removed from the entry

  • newsuperior (str) – DN of the new parent

parse() → None

Invokes LDIFParser.parse_entry_records() for backward compatibility

parse_entry_records() → None

Continuously read and parse LDIF entry records

class ldif.LDIFRecordList(input_file: TextIO | BinaryIO, ignored_attr_types: list[str] | None = None, max_entries: int = 0, process_url_schemes: list[str] | None = None)

Collect all records of a LDIF file. It can be a memory hog!

Records are stored in all_records as a single list of 2-tuples (dn, entry), after calling parse().

all_records: list[tuple[str, MutableMapping[str, list[bytes]]]]

List storing parsed records.

handle(dn: str, entry: MutableMapping[str, list[bytes]]) → None

Append a single record to the list of all records (all_records).

handle_add(dn: str, entry: MutableMapping[str, list[bytes]], controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single add operation. This method should be implemented by applications using LDIFParser.

handle_delete(dn: str, controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single delete operation. This method should be implemented by applications using LDIFParser.

handle_modify(dn: str, modops: Sequence[tuple[str, list[bytes]] | tuple[int, str, bytes | list[bytes] | None]], controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single modify operation. This method should be implemented by applications using LDIFParser.

handle_modrdn(dn: str, newrdn: str, deleteoldrdn: bool, newsuperior: str | None = None, controls: list[tuple[str, str, str | None]] | None = None) → None

Process a single LDIF record representing a single modrdn/rename operation. This method should be implemented by applications using LDIFParser.

class ldif.LDIFCopy(input_file: TextIO | BinaryIO, output_file: TextIO, ignored_attr_types: list[str] | None = None, max_entries: int = 0, process_url_schemes: list[str] | None = None, base64_attrs: list[str] | None = None, cols: int = 76, line_sep: str = '\n')

Copy LDIF input to LDIF output containing all data retrieved via URLs

handle(dn: str, entry: MutableMapping[str, list[bytes]]) → None

Write single LDIF record to output file.

Example

The following example demonstrates how to write LDIF output of an LDAP entry with 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 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()