#! /usr/bin/python2.2 """ A simple script to run a very simple restaurant database. """ # here's the legal bumpf; note that this is the BSD license # which basically means "do whatever you like with it" """ (c) 2003 Tom Anderson All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Tom Anderson nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import sys import cgi class enumerate: "This emulates the builtin function of the same name in python 2.3." def __init__( self, base ): self.idx = 0 self.base = iter( base ) def __iter__( self ): return self def next( self ): tuple = (self.idx, self.base.next()) self.idx = self.idx + 1 return tuple def parseRestaurant( line ): "A restaurant line is a TSV row with fields name, address, cuisine, rating" cells = line.rstrip( "\n" ).split( "\t" ) return map( str.strip, cells ) dbPathFmt = "/srv/www/urchin.earth.li/users/twic/wml/restaurants-%s.tsv" def query( locale, cuisine ): "This is the bit that would query the real database. It returns a list of restaurant tuples" dbPath = dbPathFmt % locale dbFile = file( dbPath ) restaurants = map( parseRestaurant, dbFile.readlines() ) return filter( (lambda r: r[2] == cuisine ), restaurants ) fields = cgi.FieldStorage() locale = fields.getfirst( "locale" ) cuisine = fields.getfirst( "cuisine" ) restaurants = query( locale, cuisine ) out = sys.stdout out.write( "Content-type: text/vnd.wap.wml\n" ) out.write( "\n" ) out.write( "\n" ) out.write( "\n" ) out.write( "\n" ) out.write( "\n" ) out.write( "

There were " ) out.write( str( len( restaurants ) ) ) out.write( " matches.

\n" ) for (idx, restaurant) in enumerate( restaurants ): out.write( "

\n" ) out.write( "" ) out.write( restaurant[0] ) out.write( "
\n" ) out.write( "Address: " ) out.write( restaurant[1] ) out.write( "
\n" ) out.write( "Rating: " ) out.write( restaurant[3] ) out.write( "\n" ) out.write( "

\n" ) out.write( "
\n" ) out.write( "
\n" ) out.flush()