[Da-tools-commits] ./da-tools/userdir-ldap-cgi-python r8: added TODO file
Robin Wittler
real at geek-at-work.org
Tue Jan 13 21:13:30 UTC 2009
------------------------------------------------------------
revno: 8
committer: Robin Wittler <real at geek-at-work.org>
branch nick: userdir-ldap-cgi-python
timestamp: Tue 2009-01-13 22:13:30 +0100
message:
added TODO file
written some functions in lib/ud_ldap_ng.py
made some changes in templates and controllers
added:
TODO
modified:
ud_ldap_ng/controllers/udldapng.py
ud_ldap_ng/lib/ud_ldap_ng.py
ud_ldap_ng/templates/machines.html
-------------- next part --------------
=== added file 'TODO'
--- a/TODO 1970-01-01 00:00:00 +0000
+++ b/TODO 2009-01-13 21:13:30 +0000
@@ -0,0 +1,8 @@
+TODO
+====
+
+ - Exception Handling
+ - building functions for parsing and formatting [[, * etc. pp.
+ - sort handling
+ - ...
+
=== modified file 'ud_ldap_ng/controllers/udldapng.py'
--- a/ud_ldap_ng/controllers/udldapng.py 2009-01-11 21:54:40 +0000
+++ b/ud_ldap_ng/controllers/udldapng.py 2009-01-13 21:13:30 +0000
@@ -8,40 +8,22 @@
log = logging.getLogger(__name__)
-import re
-from ud_ldap_ng.lib.ud_ldap_ng import connectLDAP, bindLDAP, searchHosts, searchUser
+from ud_ldap_ng.lib.ud_ldap_ng import connectDebianLDAP, getDebianHostsAttributes, getDebianUser
class UdldapngController(BaseController):
-
- def index(self):
- con = connectLDAP('db.debian.org')
- bindLDAP(con)
- results = searchHosts(con)[1:]
- wanted = ['architecture', 'sponsor', 'purpose', 'status']
- c.result = dict()
- for result in results:
- hostname = result[1].get('hostname', None)
- if not hostname or type(hostname) != list:
- continue
- else:
- hostname = hostname.pop()
-
- c.result.setdefault(hostname, dict())
-
- for b in wanted:
- d = result[1].get(b, None)
- if not d or type(d) != list:
- d = []
- c.result.get(hostname).setdefault(b, d)
- return render('/machines.html')
-
- def lala(self):
- con = connectLDAP('db.debian.org')
- bindLDAP(con)
- user = request.params['uid']
- results = searchUser(con, user)
- return repr(results)
+ def index(self):
+ con = connectDebianLDAP()
+ c.result = getDebianHostsAttributes(con)
+ #c.result = dict()
+ return render('/machines.html')
+
+ def lala(self):
+ con = connectLDAP('db.debian.org')
+ bindLDAP(con)
+ user = request.params['uid']
+ results = searchUser(con, user)
+ return repr(results)
#result_set = []
#result_type, result_data = con.result(results, 0)
#if result_type == ldap.RES_SEARCH_ENTRY:
=== modified file 'ud_ldap_ng/lib/ud_ldap_ng.py'
--- a/ud_ldap_ng/lib/ud_ldap_ng.py 2009-01-11 21:30:10 +0000
+++ b/ud_ldap_ng/lib/ud_ldap_ng.py 2009-01-13 21:13:30 +0000
@@ -1,33 +1,169 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
-__version__ = '0.0.1'
+__version__ = '0.0.2'
__author__ = 'Robin Wittler <real at geek-at-work.org>'
__license__ = 'GPL3'
import ldap
-def connectLDAP(host):
- return ldap.open(host)
-
-def bindLDAP(con, uid=None, password=None):
- if not uid:
- uid = ""
- if not password:
- password = ""
- con.simple_bind_s(uid, password)
- return True
-
-def searchHosts(con):
- dn = "ou=hosts,dc=debian,dc=org"
- scope = ldap.SCOPE_SUBTREE
- return con.search_s(dn, scope)
-
-def searchUser(con, user):
- dn = "ou=users,dc=debian,dc=org"
- scope = ldap.SCOPE_SUBTREE
- searchFilter = "(uid=%s)" % (user)
- return con.search_s(dn,scope,searchFilter)
-
+def connectDebianLDAP(uid=None, password=None):
+ '''Connect to the Debian LDAP with uid and password.
+ If uid and/or password is None (the default)
+ then connectLDAP uses an empty uid and password.
+
+ @param uid The uid that should be used to connect
+ @type uid str or None
+ @param password The password tha should used to connect
+ @type password str or None
+ @return The connection object
+ '''
+ host = 'db.debian.org'
+ con = ldap.open(host)
+ if not uid:
+ uid = ""
+ if not password:
+ password = ""
+ con.simple_bind_s(uid, password)
+ return con
+
+def getDebianHost(con, host):
+ '''getDebianHost takes a ldap connection object and returns
+ recursive all host attributes from debian ldap for given host.
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @param host The hostname
+ @type host str
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ if not type(host) == str:
+ raise TypeError('host must be type str')
+ dn = 'host=%s,ou=hosts,dc=debian,dc=org' %(host)
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope))
+
+def getDebianHostAttributes(con, host, attr_list):
+ '''getDebianHostAttributes takes a ldap connection object and returns
+ all given host attributes
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @param host The hostname
+ @type host str
+ @param attr_list The list of Attributes to query
+ @type attr_list list
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ if not type(host) == str:
+ raise TypeError('host must be type str')
+ if not type(attr_list) == list:
+ raise TypeError('attr_list must be type list')
+ for entry in attr_list:
+ if not type(entry) == str:
+ raise TypeError('entry %s in attr_list must be type str' %(repr(entry)))
+ dn = 'host=%s,ou=hosts,dc=debian,dc=org' %(host)
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope, attrlist=attr_list))
+
+def getDebianHosts(con):
+ '''getDebianHosts takes a ldap connection object and returns
+ recursive all host entries with all attributes from the debian ldap.
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ dn = 'ou=hosts,dc=debian,dc=org'
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope))
+
+def getDebianHostsAttributes(con, attr_list=['hostname', 'architecture', 'sponsor', 'purpose', 'status']):
+ '''getDebianHostsAttributes takes a ldap connection object
+ and returns the given optional attributes recursive for all hosts
+ from debian ldap.
+ Defaults for attr_list are: hostname, architecture, sponsor, purpose, status
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @param attr_list The list of attributes that should be searched for (optional)
+ @type attr_list list
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ if not type(attr_list) == list:
+ raise TypeError('attr_list must be type list')
+ for entry in attr_list:
+ if not type(entry) == str:
+ raise TypeError('entry %s in attr_list must be type str' %(repr(entry)))
+ dn = 'ou=hosts,dc=debian,dc=org'
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope, attrlist=attr_list))
+
+def getDebianUser(con, user):
+ '''getDebianUser takes a ldap connection object and returns
+ all attributes for the given user.
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @param user The username you wanne query for
+ @type user str
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ if not type(user) == str:
+ raise TypeError('user must be type str')
+ dn = 'uid=%s,ou=users,dc=debian,dc=org' %(user)
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope))
+
+def getDebianUserAttributes(con, user, attr_list):
+ '''getDebianUserAttributes takes a ldap connection object and returns
+ the given attributes from attr_list for a given user from the ldap.
+
+ @param con The ldap connection object
+ @type con ldap connection object
+ @param user The username you wanne query for
+ @type user str
+ @param attr_list The list of attributes that should be searched for
+ @type attr_list list
+ @return A generator object with the ldap answer
+ '''
+ if not hasattr(con, 'search_s'):
+ raise AttributeError('con must have a "search_s" attribute')
+ if not type(user) == str:
+ raise TypeError('user must be type str')
+ for entry in attr_list:
+ if not type(entry) == str:
+ raise TypeError('entry %s in attr_list must be type str' %(repr(entry)))
+ dn = 'uid=%s,ou=users,dc=debian,dc=org' %(user)
+ scope = ldap.SCOPE_SUBTREE
+ return prepareLDAPResult(con.search_s(dn, scope, attrlist=attr_list))
+
+def prepareLDAPResult(ldapresult):
+ '''prepareLDAPResult takes an ldap result, removes the
+ ldap path and yield the results.
+
+ @param ldapresult The ldap result
+ @type ldapresult list
+ @yield the ldap result
+ '''
+ if not type(ldapresult) == list:
+ raise TypeError('ldapresult must be type list')
+ for entrys in ldapresult:
+ for entry in entrys:
+ if type(entry) == dict and entry:
+ yield entry
+
+__all__ = ['connectDebianLDAP', 'getDebianHost', 'getDebianHostAttributes', 'getDebianHosts',
+ 'getDebianHostsAttributes', 'getDebianUser', 'getDebianUserAttributes', 'prepareLDAPResult']
if __name__ == '__main__':
pass
=== modified file 'ud_ldap_ng/templates/machines.html'
--- a/ud_ldap_ng/templates/machines.html 2009-01-10 22:32:01 +0000
+++ b/ud_ldap_ng/templates/machines.html 2009-01-13 21:13:30 +0000
@@ -41,32 +41,35 @@
-->
<div id="text">
<table border="1">
- <tr><th>Hostname</th><th>Architecture</th><th>Purpose</th><th>Sponsor</th><th>Status</th></tr>
- % for hostname in sorted(c.result):
- <tr>
- <td>${hostname}</td>
- % for key, values in sorted(c.result.get(hostname).items()):
- <td>
- % if len(values) > 1:
- <ul>
- % for item in values:
- <li>${item}</li>
- % endfor
- </ul>
- % else:
- % for item in values:
- ${item}
- % endfor
- % endif
- </td>
- % endfor
- </tr>
- % endfor
+ <tr>
+ <th>Hostname</th>
+ <th>Architecture</th>
+ <th>Purpose</th>
+ <th>Sponsor</th>
+ <th>Status</th>
+ </tr>
+ % for result in c.result:
+ <tr>
+ <td>
+ % for hostname in result.get('hostname', ['Unkown',]):
+ ${hostname}
+ % endfor
+ </td>
+ % for key in ['architecture', 'purpose', 'sponsor', 'status']:
+ <td>
+ <ul>
+ % for value in result.get(key, ['Unknown',]):
+ <li>${value}</li>
+ % endfor
+ </ul>
+ </td>
+ % endfor
+ </tr>
+ % endfor
</table>
</div>
</div>
</div>
-
</body>
</html>
More information about the Da-tools-commits
mailing list