[Da-tools-commits] ./da-tools/userdir-ldap-cgi-python r10: Fixed some bugs, fixed doku.

Robin Wittler real at geek-at-work.org
Tue Jan 20 19:54:58 UTC 2009


------------------------------------------------------------
revno: 10
committer: Robin Wittler <real at geek-at-work.org>
branch nick: userdir-ldap-cgi-python
timestamp: Tue 2009-01-20 20:54:58 +0100
message:
  Fixed some bugs, fixed doku.
modified:
  ud_ldap_ng/lib/ud_ldap_ng.py
-------------- next part --------------
=== modified file 'ud_ldap_ng/lib/ud_ldap_ng.py'
--- a/ud_ldap_ng/lib/ud_ldap_ng.py	2009-01-20 17:29:13 +0000
+++ b/ud_ldap_ng/lib/ud_ldap_ng.py	2009-01-20 19:54:58 +0000
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf8 -*-
-__version__ = '0.0.4'
+__version__ = '0.0.5'
 __author__ = 'Robin Wittler <real at geek-at-work.org>'
 __license__ = 'GPL3'
 
@@ -13,8 +13,8 @@
 	
 		not adjusted format:
 		[
-			((host='test',ou='hosts',dc='debian',dc='org'), {'hostname': ['test'], 'purpose': ['prod'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']}),
-			((host='test2',ou='hosts',dc='debian',dc='org'), {'hostname': ['test2'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']})
+			({'hostname': ['test'], 'purpose': ['prod'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']}),
+			({'hostname': ['test2'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']})
 		]
 
 		in the not adjusted format the second entry misses one attribute: purpose
@@ -22,26 +22,44 @@
 		
 		adjusted format:
 		[
-			((host='test',ou='hosts',dc='debian',dc='org'), {'hostname': ['test'], 'purpose': ['prod'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']}),
-			((host='test2',ou='hosts',dc='debian',dc='org'), {'hostname': ['test2'], 'purpose': ['Unknown'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']})
+			({'hostname': ['test'], 'purpose': ['prod'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']}),
+			({'hostname': ['test2'], 'purpose': ['Unknown'], 'sponsor': ['Nice Guy Sponsor'], 'status': ['down']})
 		]
 		
-		after the adjustResults method, the second entry has the missing attribut.
-		So, the adjustResults method looks over all attributes 
-		
+		after the adjustResults method, the second entry has the missing attribut. The value is taken from the 
+		default_value argument (which defaults to ['Unknown']).
+		So, spoken in short term -the adjustResults method looks over all attributes and adds all missing Attributs with 
+		a default_value.
+
+		If you inherit from this class, you can safely override the __init__ method.
+
+		@param results				A 'raw' ldap result list
+		@type results				list
+		@param adjust				If True, the class adds automatic any missing attribute
+		@type adjust				bool		
 		'''
-	def __new__(cls, results, adjust=0):
+	def __new__(cls, results, adjust=False):
 		cls.isRawLDAPResult(results)
+		if not type(adjust) == bool:
+			raise TypeError('adjust must be typ bool')
 		cls.__original_results = results
+		cls.__results = cls.alterDNFromResults(results)
 		if adjust:
-			cls.__results = cls.adjustResults(results)
+			cls.adjusted = True
+			cls.__results = cls.adjustResults(cls.__results)
 		else:
-			cls.__results = results
+			cls.adjusted = False
 		return object.__new__(cls)
 
 	@classmethod
 	def isRawLDAPResult(cls, results):
-		'''Checks if a iterable is a wellformed ldap result'''
+		'''Checks if a iterable is a wellformed raw ldap result and
+		returns True - if not, it raises a exception with more information.
+		
+		@param results			A 'raw' ldap result list
+		@type results			list
+		@return					bool
+		'''
 		try:
 			i = iter(results)
 		except TypeError:
@@ -59,7 +77,13 @@
 
 	@classmethod
 	def isDebianLDAPResult(cls, results):
-		'''Checks if a iterable is a wellformed DebianLDAPResult.'''
+		'''Checks if a iterable is a wellformed Debian ldap result and
+		returns True - if not, it raises a exception with more information.
+		
+		@param results			A 'Debian' ldap result list
+		@type results			list
+		@return					bool
+		'''
 		try:
 			i = iter(results)
 		except TypeError:
@@ -70,14 +94,37 @@
 		return True
 	
 	@classmethod
-	def adjustResults(cls, results, default_value=['Unknown',]):
-		all_attr = cls.__getAllAttrFromRawResult(results)
+	def alterDNFromResults(cls, results):
+		'''Removes all dn's from the results and return the results.
+
+		@param results			A 'raw' ldap result list
+		@type results			list
+		@return					list
+		'''
+		cls.isRawLDAPResult(results)
 		ret_list = list()
-		cls.isRawLDAPResult(results)
 		for dn, result in results:
 			if not result:
 				results.remove((dn, result))
 				continue
+			ret_list.append(result)
+		return ret_list
+	
+	@classmethod
+	def adjustResults(cls, results, default_value=['Unknown',]):
+		'''Adjust results by adding all missing attributes, so all results have
+		the same attributes.
+
+		@param results			A Debian ldap result list
+		@type results			list
+		@param default_value	An Object that should be taken as value for missing	attributes.
+		@type default_value		all types of objects
+		@return					list
+		'''
+		cls.isDebianLDAPResult(results)
+		all_attr = cls.getAllAttrFromResults(results)
+		ret_list = list()
+		for result in results:
 			for key in all_attr:
 				if key not in result:
 					result.setdefault(key, default_value)
@@ -85,26 +132,45 @@
 		return ret_list
 	
 	@classmethod
-	def sortDebianResults(cls, keyword, reverse):
-		if not type(keyword) == str and keyword:
+	def sortResults(cls, keyword, reverse=False):
+		'''Sort the result dicts and returns it as a iterator.
+
+		@param keyword			The keyword to sort
+		@type keyword			string
+		@param reverse			reverse the sorting
+		@type reverse			bool
+		@return					iterator
+		'''
+		#TODO: This is all a bit ugly, please show me a better way to sort many dicts. ;)
+		if not type(keyword) == str:
 			raise TypeError('keyword must be type str, None or False')
-		if not keyword in cls.__getAllAttrFromDebianResult(cls.__results):
+		if not keyword in cls.getAllAttrFromResults(cls.__results):
 			raise KeyError('keyword exist not in results')
 		if not type(reverse) == bool:
 			raise TypeError('reverse must be type bool')
 		ret_list = list()
 		for result in cls.__results:
 			if not keyword in result:
-				raise KeyError('keyword is not in result: %s' %(result))
-			ret_list.append((result.get(keyword), result))
+				if not cls.adjusted:
+					ret_list.append(('zzzzzzzzzz', result))
+				else:
+					raise KeyError('keyword is not in adjusted result: %s' %(result))
+			else:
+				ret_list.append((result.get(keyword), result))
 		ret_list = sorted(ret_list, reverse=reverse)
-		for keyword, result in [(i,k) for i,k in ret_list]:
-			ret_list.remove((keyword, result))
+		for key, result in [(k,r) for k,r in ret_list]:
+			ret_list.remove((key, result))
 			ret_list.append(result)
-		return ret_list
+		return iter(ret_list)
 
 	@classmethod
-	def __getAllAttrFromRawResult(cls, results):
+	def getAllAttrFromRawResults(cls, results):
+		'''Extract all Attributes from a raw ldap result and returns it as list.
+
+		@param results			A 'raw' ldap result list
+		@type results			list
+		@return					list
+		'''
 		all_attr = set()
 		cls.isRawLDAPResult(results)
 		for dn, result in results:
@@ -113,7 +179,13 @@
 		return all_attr
 
 	@classmethod
-	def __getAllAttrFromDebianResult(cls, results):
+	def getAllAttrFromResults(cls, results):
+		'''Extract all Attributes from a Debian ldap result and returns it as list.
+
+		@param results			A 'Debian' ldap result list
+		@type results			list
+		@return					list
+		'''
 		all_attr = set()
 		cls.isDebianLDAPResult(results)
 		for result in results:
@@ -122,12 +194,22 @@
 		return all_attr
 
 	@classmethod
-	def getOriginalResults(cls):
+	def iterOriginalResults(cls):
+		'''returns the 'raw' ldap result without any changes as iterator.
+
+		@return				iterator
+		'''
 		return iter(cls.__original_results)
 	
 	@classmethod
 	def  __iter__(cls):
-		return iter(cls.__results)
+		'''returns the debian ldap result as iterator.
+
+		@return				iterator
+		'''
+		for result in cls.__results:
+			yield result
+		#return iter(cls.__results)
 
 class MyLDAP(object):
 	'''This is a simple wrapper around ldap.
@@ -287,7 +369,7 @@
 		@type host			str
 		@param attrlist		An Optional list of Attributes to query for
 		@type attrlist		list
-		@return				A generator object with the ldap answer
+		@return				Ldap Result Object
 		'''
 		if not getattr(self, 'con', None):
 			raise Exception('You must call the "connect" Method before.')
@@ -298,30 +380,25 @@
 		return DebianLDAPResult(self.ldapSearch(dn, scope, attrlist=attrlist))
 
 	
-	def getDebianHosts(self, attrlist=None, keyword='hostname', reversed=False):
+	def getDebianHosts(self, attrlist=None, adjust=False):
 		'''getDebianHosts returns recursive all hosts and all hosts attributes from debian ldap.
 		Optional you can submit a list of attributes in which you are only interested.
-		You can submit a keyword (which defaults to 'hostname') and then the returned list will
-		be sorted by this keyword. If the optional arg reversed set to True, 
-		the returned list will be reversed.
+		If you set adjust to True, then every result will automaticly have all the same count of
+		Attributes.
 
 		@param attrlist		An Optional list of Attributes to query for
 		@type attrlist		list
-		@param keyword		The keyword to sort
-		@type keyword		str
-		@param reversed		reverse the sort
-		@type reversed		bool
-		@return				ResultObject
+		@param adjust		If True, the class adds automatic any missing attribute
+		@type adjust		bool		
+		@return				Ldap Result Object
 		'''
 		if not getattr(self, 'con', None):
 			raise Exception('You must call the "connect" Method before.')
-		if not type(keyword) == str:
-			raise TypeError('keyword must be type str')
-		if not type(reversed) == bool:
-			raise TypeError('reversed must be type bool')
+		if not type(adjust) == bool:
+			raise TypeError('adjust must be type bool')
 		dn = 'ou=hosts,dc=debian,dc=org'
 		scope = ldap.SCOPE_SUBTREE
-		return DebianLDAPResult(self.ldapSearch(dn, scope, attrlist=attrlist))
+		return DebianLDAPResult(self.ldapSearch(dn, scope, attrlist=attrlist), adjust=adjust)
 
 __all__ = ['MyLDAP', 'DebianLDAP']
 



More information about the Da-tools-commits mailing list