#!/usr/local/bin/python #-- # abbrlookup.cgi: Look up a six-letter bird code using the MySQL # nomenclature database # $Revision: 1.4 $ $Date: 2001/05/27 02:15:13 $ #-- # This script handles the bird code lookup form on page lookup.html, # which has these elements: # abbr: Six-letter bird code to be looked up #-- from types import ListType # To check for lists import cgi # Main CGI handler import string # Standard string functions import hier, txny # Taxonomic hierarchy and taxonomic database from showtaxa import * # Function to display taxon with ancestors # - - - s h o w C o l l i s i o n - - - def showCollision ( badAbbr, collisionList ): """Display a collision list [ if (abbr is a collision code) and (collisionList is the related list of valid abbrs) and (tx is a Txny) -> +:= abbr + (collisionList and related taxa) ] """ print ( "

Code `%s' is a collision form. " "Use one of:

" % badAbbr ) for goodAbbr in collisionList: taxon = tx.lookupAbbr(goodAbbr) print ( "%-6s %s [%s]
" % (string.upper(goodAbbr), taxon.eng, taxon.sci) ) # - - - a b b r L o o k u p - - - def abbrLookup(abbr): """Look up `abbr' in the nomenclature database and display its meaning. [ if abbr is a string -> if abbr is defined in the nomenclature database -> +:= the code's source name and its related taxon's English and scientific names, as HTML else -> +:= error message in HTML ] """ #-- 2 -- # [ if abbr is defined in tx -> # abbrDef := the AbbrDef object for abbr # else if abbr is a conflict form -> # +:= list of alternatives # return # else -> # +:= error as HTML # return ] abbrDef = tx.lookupAbbrDef ( abbr ) if abbrDef is None: collisionList = tx.lookupCollision ( abbr ) if collisionList is None: print "

Bird code `%s' is undefined." % abbr return else: showCollision ( abbr, collisionList ) return #-- 3 -- # [ if abbrDef.sci is defined in tx -> # taxon := the Taxon object for that code # else -> # +:= error as HTML # return ] taxon = tx.lookupSci ( abbrDef.sci ) if taxon is None: print "

Bird code `%s' is undefined." % abbr return #-- 4 -- # [ +:= display of taxon with all its ancestors ] htmlTaxon(taxon) #-- 5 -- # [ if abbrDef.eng != taxon.eng -> # +:= abbr, abbrDef.eng, taxon.eng, and taxon.sci ] if abbrDef.eng != taxon.eng: print ( "

%s: %s -> %s [%s]" % ( abbr, abbrDef.eng, taxon.eng, taxon.sci ) ) # - - - - m a i n - - - - - form = cgi.FieldStorage() # Get the form object print "Content-type: text/html" # Print the required header for HTML print # Blank line to signify end of headers print "" print "" print " Six-letter bird code lookup" print "" print "" #-- 1 -- try: abbr = string.upper(form["abbr"].value) except KeyError: print "

You must supply an abbreviation." print "

Six-letter bird code lookup: %s

" % abbr #-- 2 -- # [ if taxonomy and hierarchy databases can be opened -> # h := the Hier object for the hierarchy database # tx := the Txny object for the taxonomy database # else -> # +:= error as HTML # return ] try: h = hier.Hier() tx = txny.Txny(h) except Exception, detail: print "

Database access error: %s" % `detail` sys.exit(0) #-- 3 -- abbrLookup ( abbr ) print "" print ""