Commit | Line | Data |
---|---|---|
b816959a P |
1 | #!/usr/bin/env python |
2 | # -*- coding: utf-8 -*- | |
3 | """ | |
4 | Annuaire téléphonique dynamique pour les téléphones IP Thomson ST2030. | |
5 | ||
6 | Copyright : Agence universitaire de la Francophonie — www.auf.org | |
7 | Licence : GNU General Public Licence, version 2 | |
8 | Auteur : Jean Christophe André | |
9 | Date de création : 22 octobre 2010 | |
10 | ||
11 | Depends: libapache2-mod-wsgi auf-refer | |
12 | ||
13 | Attention : le code n'est pas encore “thread-safe”... | |
14 | """ | |
15 | import aufrefer | |
16 | ||
17 | phonebook_document = u'<ThomsonPhoneMenu>\n%s\n</ThomsonPhoneMenu>' | |
18 | phonebook_entry = u'<DirectoryEntry><Name>%(name)s</Name><Telephone>%(phone)s</Telephone></DirectoryEntry>' | |
19 | ||
20 | def search(pattern): | |
21 | phonebook_entries = [] | |
22 | for user in aufrefer.get('annuaire.json'): | |
23 | if user['tel_ip'] and pattern in user['adel']: | |
24 | name = user['adel'].split('@')[0] | |
25 | phone = user['tel_ip'].replace(' ','') | |
26 | entry = phonebook_entry % {'name': name, 'phone': phone} | |
27 | phonebook_entries.append(entry) | |
28 | return phonebook_document % '\n'.join(phonebook_entries[0:32]) | |
29 | ||
30 | def application(environ, start_response): | |
31 | query = environ.get('QUERY_STRING').lower() | |
32 | params = dict([x.split('=') for x in query.split('&')]) | |
33 | if not params.get('mac') or not params.get('search'): | |
34 | headers = [('Content-Type', 'text/plain; charset=utf-8'), ] | |
35 | start_response('401 Authorization Required', headers) | |
36 | return ['Authorization Required'] | |
37 | data = search(params.get('search')).encode('utf-8') | |
38 | mime_type = 'text/xml; charset=utf-8' | |
39 | length = str(len(data)) | |
40 | headers = [('Content-Type', mime_type), ('Content-Length', length), ] | |
41 | start_response('200 OK', headers) | |
42 | return [data] | |
43 | ||
44 | if __name__ == '__main__': | |
45 | # this runs when script is started directly from commandline | |
46 | try: | |
47 | # create a simple WSGI server and run the application | |
48 | from wsgiref import simple_server | |
49 | print "Running test application - point your browser at http://localhost:8000/ ..." | |
50 | httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler) | |
51 | httpd.set_app(application) | |
52 | httpd.serve_forever() | |
53 | except ImportError: | |
54 | # wsgiref not installed, just output html to stdout | |
55 | for content in application({}, lambda status, headers: None): | |
56 | print content | |
57 |