Last active
December 10, 2015 15:19
-
-
Save mbrownnycnyc/4453803 to your computer and use it in GitHub Desktop.
from http://www.packtpub.com/article/python-ldap-applications-ldap-opearations [not totally implemented or tested: to_ldif() doesn't function ?]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ldif, sys | |
from StringIO import StringIO | |
from ldap.cidict import cidict | |
def get_search_results(results): | |
#Given a set of results, return a list of LDAPSearchResult objects. | |
res = [] | |
if type(results) == tuple and len(results) == 2 : | |
(code, arr) = results | |
elif type(results) == list: | |
arr = results | |
if len(results) == 0: | |
return res | |
for item in arr: | |
res.append( LDAPSearchResult(item) ) | |
return res | |
sys.stderr.write("exiting get_search_results\n\n") | |
class LDAPSearchResult: | |
#A class to model LDAP results. | |
dn = '' | |
def __init__(self, entry_tuple): | |
#Create a new LDAPSearchResult object. | |
(dn, attrs) = entry_tuple | |
if dn: | |
self.dn = dn | |
else: | |
return | |
self.attrs = cidict(attrs) | |
def get_attributes(self): | |
'''Get a dictionary of all attributes. | |
get_attributes()->{'name1':['value1','value2',...], | |
'name2: [value1...]} | |
''' | |
return self.attrs | |
def set_attributes(self, attr_dict): | |
'''Set the list of attributes for this record. | |
The format of the dictionary should be string key, list of | |
string alues. e.g. {'cn': ['M Butcher','Matt Butcher']} | |
set_attributes(attr_dictionary) | |
''' | |
self.attrs = cidict(attr_dict) | |
def has_attribute(self, attr_name): | |
'''Returns true if there is an attribute by this name in the record. | |
has_attribute(string attr_name)->boolean | |
return self.attrs.has_key( attr_name ) | |
''' | |
def get_attr_values(self, key): | |
'''Get a list of attribute values. | |
get_attr_values(string key)->['value1','value2'] | |
''' | |
try: | |
return self.attrs[key] | |
except: | |
return "['']" | |
def get_attr_names(self): | |
'''Get a list of attribute names. | |
get_attr_names()->['name1','name2',...] | |
''' | |
return self.attrs.keys() | |
def get_dn(self): | |
'''Get the DN string for the record. | |
get_dn()->string dn | |
''' | |
try: | |
return self.dn | |
except: | |
return "['']" | |
def pretty_print(self): | |
'''Create a nice string representation of this object. | |
pretty_print()->string | |
''' | |
str = "DN: " + self.dn + "n" | |
for a, v_list in self.attrs.iteritems(): | |
str = str + "Name: " + a + "n" | |
for v in v_list: | |
str = str + " Value: " + v + "n" | |
str = str + "========" | |
return str | |
def to_ldif(self): | |
'''Get an LDIF representation of this record. | |
to_ldif()->string | |
''' | |
out = StringIO() | |
ldif_out = ldif.LDIFWriter(out) | |
sys.stderr.write(str(type(self.dn))+"\n") | |
sys.stderr.write(str(type(self.attrs))+"\n") | |
ldif_out.unparse(self.dn, self.attrs) | |
return out.getvalue() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment