#!/usr/bin/python
"""airports.py -- CGI interface to ../data4/aptdata.py

"""
__version__ = '$Id: airports 1210 2006-07-11 22:53:43Z connolly $'

# ugh... kind of a mess...
import sys
sys.path.append("kid-0.9.2-py2.3.egg")
sys.path.append("elementtree-1.2.6_20050316-py2.3.egg")
import kid

sys.path.append("../data4") # to get aptdata
import aptdata

import cgi
import cgitb; cgitb.enable()

def gateway():
    #webget = aptdata.batteries_included_get()
    webget = aptdata.WebCache("wikipedia-cache").get
    tpl = kid.Template(file="../data4/aptinfo.kid")

    form = cgi.FieldStorage()
    if 'iata' not in form:
        print "Status: 404"
        print "Content-Type: text/plain"
        print
        print "try ?iata=LGA or some such"
        return
    
    print "Status: 200"
    print "Content-Type: application/rdf+xml"
    print

    tpl.contacts = [aptdata.airportCard(webget, form['iata'].value)]
    tpl.path = 'airports?iata='
    for txt in tpl.generate(output='xml', encoding='utf-8'):
        sys.stdout.write(txt)

if __name__ == '__main__':
    gateway()
