summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exchange/wsgi.py24
-rwxr-xr-x[-rw-r--r--]server.py54
-rw-r--r--util.py31
3 files changed, 67 insertions, 42 deletions
diff --git a/exchange/wsgi.py b/exchange/wsgi.py
new file mode 100644
index 0000000..bf2baf0
--- /dev/null
+++ b/exchange/wsgi.py
@@ -0,0 +1,24 @@
1# vim: set filencoding=utf8
2"""
3Calendar WSGI App
4
5@author: Mike Crute (mcrute@ag.com)
6@organization: American Greetings Interactive
7@date: February 15, 2010
8"""
9
10from exchange.commands import FetchCalendar
11from exchange.authenticators import CookieSession
12
13
14class CalendarApp(object):
15
16 def __init__(self, exchange_server, user, password):
17 self.session = CookieSession(exchange_server,
18 username=user, password=password)
19
20 def __call__(self, environ, start_response):
21 start_response('200 OK', [])
22 command = FetchCalendar(self.session)
23 calendar = command.execute()
24 return calendar.as_string()
diff --git a/server.py b/server.py
index 0510b2b..78911c3 100644..100755
--- a/server.py
+++ b/server.py
@@ -1,3 +1,4 @@
1#!/usr/bin/env python
1# vim: set filencoding=utf8 2# vim: set filencoding=utf8
2""" 3"""
3Exchange Calendar Proxy Server 4Exchange Calendar Proxy Server
@@ -7,52 +8,21 @@ Exchange Calendar Proxy Server
7@date: April 26, 2009 8@date: April 26, 2009
8""" 9"""
9 10
10from getpass import getpass 11from util import config_dict
11from ConfigParser import ConfigParser 12from exchange.wsgi import CalendarApp
12from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler 13from wsgiref.simple_server import make_server
13 14
14from exchange.commands import FetchCalendar
15from exchange.authenticators import CookieAuthenticator
16 15
17 16def main():
18class CalendarHandler(BaseHTTPRequestHandler): 17 config = config_dict('exchange.cfg')
19
20 def do_GET(self):
21 print('* Fetching Calendars')
22
23 fetcher = FetchCalendar(self.server.exchange_server)
24 authenticator = CookieAuthenticator(self.server.exchange_server)
25
26 authenticator.authenticate(self.server.user, self.server.password)
27 fetcher.authenticator = authenticator
28
29 calendar = fetcher.execute()
30 self.wfile.write(calendar.as_string())
31
32 # This seems to work on Linux but not Mac OS. ~mcrute
33 if hasattr(self.wfile, 'close'):
34 self.wfile.close()
35
36
37def main(config_file='exchange.cfg'):
38 config = ConfigParser()
39 config.read(config_file)
40 bind_address = config.get('local_server', 'address')
41 bind_port = int(config.get('local_server', 'port'))
42
43 print('Exchange iCal Proxy Running on port {0:d}'.format(bind_port))
44
45 server = HTTPServer((bind_address, bind_port), CalendarHandler)
46 server.exchange_server = config.get('exchange', 'server')
47 server.user = config.get('exchange', 'user')
48
49 if config.has_option('exchange', 'password'):
50 server.password = config.get('exchange', 'password')
51 else:
52 server.password = getpass('Exchange Password: ')
53 18
54 try: 19 try:
55 server.serve_forever() 20 app = CalendarApp(config['exchange']['server'],
21 config['exchange']['user'],
22 config['exchange']['password'])
23
24 make_server(config['local_server']['address'],
25 config['local_server']['port'], app).serve_forever()
56 except KeyboardInterrupt: 26 except KeyboardInterrupt:
57 print '\n All done, shutting down.' 27 print '\n All done, shutting down.'
58 28
diff --git a/util.py b/util.py
new file mode 100644
index 0000000..4596e8a
--- /dev/null
+++ b/util.py
@@ -0,0 +1,31 @@
1# vim: set filencoding=utf8
2"""
3Exchange Proxy Utility Functions
4
5@author: Mike Crute (mcrute@ag.com)
6@organization: American Greetings Interactive
7@date: February 15, 2010
8"""
9
10from collections import defaultdict
11from ConfigParser import ConfigParser
12
13
14def config_dict(config_file):
15 """
16 Load a ConfigParser config as a dictionary. Will also
17 attempt to do simple stuff like convert ints.
18 """
19 config = ConfigParser()
20 config.read(config_file)
21 output = defaultdict(dict)
22
23 for section in config.sections():
24 for key, value in config.items(section):
25 if value.isdigit():
26 value = int(value)
27
28 output[section][key] = value
29
30 return dict(output)
31