--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+Annuaire téléphonique dynamique pour les téléphones IP Thomson ST2030.
+
+Copyright : Agence universitaire de la Francophonie — www.auf.org
+Licence : GNU General Public Licence, version 2
+Auteur : Jean Christophe André
+Date de création : 22 octobre 2010
+
+Depends: libapache2-mod-wsgi auf-refer
+
+Attention : le code n'est pas encore “thread-safe”...
+"""
+import aufrefer
+
+phonebook_document = u'<ThomsonPhoneMenu>\n%s\n</ThomsonPhoneMenu>'
+phonebook_entry = u'<DirectoryEntry><Name>%(name)s</Name><Telephone>%(phone)s</Telephone></DirectoryEntry>'
+
+def search(pattern):
+ phonebook_entries = []
+ for user in aufrefer.get('annuaire.json'):
+ if user['tel_ip'] and pattern in user['adel']:
+ name = user['adel'].split('@')[0]
+ phone = user['tel_ip'].replace(' ','')
+ entry = phonebook_entry % {'name': name, 'phone': phone}
+ phonebook_entries.append(entry)
+ return phonebook_document % '\n'.join(phonebook_entries[0:32])
+
+def application(environ, start_response):
+ query = environ.get('QUERY_STRING').lower()
+ params = dict([x.split('=') for x in query.split('&')])
+ if not params.get('mac') or not params.get('search'):
+ headers = [('Content-Type', 'text/plain; charset=utf-8'), ]
+ start_response('401 Authorization Required', headers)
+ return ['Authorization Required']
+ data = search(params.get('search')).encode('utf-8')
+ mime_type = 'text/xml; charset=utf-8'
+ length = str(len(data))
+ headers = [('Content-Type', mime_type), ('Content-Length', length), ]
+ start_response('200 OK', headers)
+ return [data]
+
+if __name__ == '__main__':
+ # this runs when script is started directly from commandline
+ try:
+ # create a simple WSGI server and run the application
+ from wsgiref import simple_server
+ print "Running test application - point your browser at http://localhost:8000/ ..."
+ httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler)
+ httpd.set_app(application)
+ httpd.serve_forever()
+ except ImportError:
+ # wsgiref not installed, just output html to stdout
+ for content in application({}, lambda status, headers: None):
+ print content
+