grenade grenade - 1 year ago 100
Python Question

web2py application configuration file

Is there a good way to store configuration settings for a web2py app?

I have written a little app that includes a script which looks up user attributes (names, email addresses, etc) from LDAP. Our corporate LDAP server requires a bind, before it allows a search for user details.

I want to share my app from a GitHub repository but not before I remove the credentials used for the bind.

Coming from a .Net background, I'm used to putting configuration like this into an app|web.config file. But this seems to be frowned on in web2py.

Whats a Pythonic or web2pyonic way of doing this?

def user_info(username):
#todo: move these parameters to config
ldap_host = ''
ldap_port = 389
ldap_base_dn = 'OU=DK,DC=example,DC=com'
ldap_bind_dn = 'CN=<removed>,OU=DK,DC=example,DC=com'
ldap_bind_pw = '<removed>'
ldap_attr_uid = 'sAMAccountName'
ldap_attr_forename = 'givenName'
ldap_attr_surname = 'sn'
ldap_attr_display_name = 'displayName'
ldap_attr_department = 'department'
ldap_attr_employee_type = 'employeeType'
ldap_attr_email = 'mail'

l = ldap.initialize('ldap://%s:%s' % (ldap_host, ldap_port))
l.simple_bind_s(ldap_bind_dn, ldap_bind_pw)
r = l.search_s(base=ldap_base_dn,
filterstr='(%s=%s)' % (ldap_attr_uid, username),
if r:
dn, e = r[0]
return {
'dn': dn,
'forename': e[ldap_attr_forename][0],
'surname': e[ldap_attr_surname][0],
'email': e[ldap_attr_email][0],
'display_name': e[ldap_attr_display_name][0],
'department': e[ldap_attr_department][0],
'employee_type': e[ldap_attr_employee_type][0]}
return None

Answer Source

There are different options, but one approach is just to put the settings in a module and import. In /yourapp/modules/

ldap_host = ''
ldap_port = 389

And then in your function:

def user_info(username):
    from ldap_settings import *
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download