Package ldaptor :: Package protocols :: Package ldap :: Module ldif
[hide private]
[frames] | no frames]

Source Code for Module ldaptor.protocols.ldap.ldif

 1  """ 
 2  Support for writing a set of directory entries as LDIF. 
 3  You probably want to use this only indirectly, as in 
 4  str(LDAPEntry(...)). 
 5   
 6  TODO support writing modify operations 
 7  TODO support reading modify operations 
 8   
 9  TODO implement rest of syntax from RFC2849 
10   
11  """ 
12   
13  # RFC2849: The LDAP Data Interchange Format (LDIF) - Technical Specification 
14   
15  import base64 
16   
17 -def base64_encode(s):
18 return ''.join(base64.encodestring(s).split('\n'))+'\n'
19
20 -def attributeAsLDIF_base64(attribute, value):
21 return "%s:: %s" % (attribute, base64_encode(value))
22
23 -def containsNonprintable(s):
24 for c in s: 25 if ord(c) > 127 or c in ('\0', '\n', '\r'): 26 return 1 27 return 0
28
29 -def attributeAsLDIF(attribute, value):
30 if value.startswith('\0') \ 31 or value.startswith('\n') \ 32 or value.startswith('\r') \ 33 or value.startswith(' ') \ 34 or value.startswith(':') \ 35 or value.startswith('<') \ 36 or value.endswith(' ') \ 37 or containsNonprintable(value): 38 return attributeAsLDIF_base64(attribute, value) 39 else: 40 return "%s: %s\n" % (attribute, value)
41
42 -def asLDIF(dn, attributes):
43 s="dn: %s\n"%dn 44 for k,vs in attributes: 45 for v in vs: 46 s=s+attributeAsLDIF(k, v) 47 s=s+"\n" 48 return s
49
50 -def header():
51 return "version: 1\n\n"
52
53 -def manyAsLDIF(objects):
54 s=[header()] 55 for dn, attributes in objects: 56 s.append(asLDIF(dn, attributes)) 57 return ''.join(s)
58