From f75f35fda0509f93378a74a395bc212b35d83c5f Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 15 Feb 2010 22:12:24 -0500 Subject: Converting webapp to a wsgi app. --- exchange/wsgi.py | 24 ++++++++++++++++++++++++ server.py | 54 ++++++++++++------------------------------------------ util.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 exchange/wsgi.py mode change 100644 => 100755 server.py create mode 100644 util.py 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 @@ +# vim: set filencoding=utf8 +""" +Calendar WSGI App + +@author: Mike Crute (mcrute@ag.com) +@organization: American Greetings Interactive +@date: February 15, 2010 +""" + +from exchange.commands import FetchCalendar +from exchange.authenticators import CookieSession + + +class CalendarApp(object): + + def __init__(self, exchange_server, user, password): + self.session = CookieSession(exchange_server, + username=user, password=password) + + def __call__(self, environ, start_response): + start_response('200 OK', []) + command = FetchCalendar(self.session) + calendar = command.execute() + return calendar.as_string() diff --git a/server.py b/server.py old mode 100644 new mode 100755 index 0510b2b..78911c3 --- a/server.py +++ b/server.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # vim: set filencoding=utf8 """ Exchange Calendar Proxy Server @@ -7,52 +8,21 @@ Exchange Calendar Proxy Server @date: April 26, 2009 """ -from getpass import getpass -from ConfigParser import ConfigParser -from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler +from util import config_dict +from exchange.wsgi import CalendarApp +from wsgiref.simple_server import make_server -from exchange.commands import FetchCalendar -from exchange.authenticators import CookieAuthenticator - -class CalendarHandler(BaseHTTPRequestHandler): - - def do_GET(self): - print('* Fetching Calendars') - - fetcher = FetchCalendar(self.server.exchange_server) - authenticator = CookieAuthenticator(self.server.exchange_server) - - authenticator.authenticate(self.server.user, self.server.password) - fetcher.authenticator = authenticator - - calendar = fetcher.execute() - self.wfile.write(calendar.as_string()) - - # This seems to work on Linux but not Mac OS. ~mcrute - if hasattr(self.wfile, 'close'): - self.wfile.close() - - -def main(config_file='exchange.cfg'): - config = ConfigParser() - config.read(config_file) - bind_address = config.get('local_server', 'address') - bind_port = int(config.get('local_server', 'port')) - - print('Exchange iCal Proxy Running on port {0:d}'.format(bind_port)) - - server = HTTPServer((bind_address, bind_port), CalendarHandler) - server.exchange_server = config.get('exchange', 'server') - server.user = config.get('exchange', 'user') - - if config.has_option('exchange', 'password'): - server.password = config.get('exchange', 'password') - else: - server.password = getpass('Exchange Password: ') +def main(): + config = config_dict('exchange.cfg') try: - server.serve_forever() + app = CalendarApp(config['exchange']['server'], + config['exchange']['user'], + config['exchange']['password']) + + make_server(config['local_server']['address'], + config['local_server']['port'], app).serve_forever() except KeyboardInterrupt: print '\n All done, shutting down.' diff --git a/util.py b/util.py new file mode 100644 index 0000000..4596e8a --- /dev/null +++ b/util.py @@ -0,0 +1,31 @@ +# vim: set filencoding=utf8 +""" +Exchange Proxy Utility Functions + +@author: Mike Crute (mcrute@ag.com) +@organization: American Greetings Interactive +@date: February 15, 2010 +""" + +from collections import defaultdict +from ConfigParser import ConfigParser + + +def config_dict(config_file): + """ + Load a ConfigParser config as a dictionary. Will also + attempt to do simple stuff like convert ints. + """ + config = ConfigParser() + config.read(config_file) + output = defaultdict(dict) + + for section in config.sections(): + for key, value in config.items(section): + if value.isdigit(): + value = int(value) + + output[section][key] = value + + return dict(output) + -- cgit v1.2.3