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, record, base64_attrs=None, cols=76)¶ Create LDIF single formatted record including trailing empty line. This is a compability function. Use is deprecated!
- 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.
-
ldif.
ParseLDIF
(f, ignore_attrs=None, maxentries=0)¶ Parse LDIF records read from file. This is a compability function. Use is deprecated!
Classes¶
-
class
ldif.
LDIFWriter
(output_file, base64_attrs=None, cols=76, line_sep='n')¶ Write LDIF entry or change records to file object Copy LDIF input to a file output object containing all data retrieved via URLs
-
class
ldif.
LDIFParser
(input_file, ignored_attr_types=None, max_entries=0, process_url_schemes=None, line_sep='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
-
class
ldif.
LDIFRecordList
(input_file, ignored_attr_types=None, max_entries=0, process_url_schemes=None)¶ Collect all records of LDIF input into a single list. of 2-tuples (dn,entry). It can be a memory hog!
-
class
ldif.
LDIFCopy
(input_file, output_file, ignored_attr_types=None, max_entries=0, process_url_schemes=None, base64_attrs=None, cols=76, line_sep='n')¶ Copy LDIF input to LDIF output containing all data retrieved via URLs
Example¶
The following example demonstrates how to write LDIF output
of an LDAP entry with ldif
module.
>>> import sys,ldif
>>> entry={'objectClass':['top','person'],'cn':['Michael Stroeder'],'sn':['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()