#!/usr/bin/python # # Copyright (c) 2007-2008 by Fred Morris Consult, Seattle WA USA # # Author: Fred Morris # Date: 16-Feb-2008 # # Licensed under the Perl Artistic License: "under the same terms # as Perl itself". """Data Example (Part One) Usage: Called as a CGI Script Fills a list from a REST/XML query. Parameters: None Version: 1.1 Modification History: FWM 20-Feb-2008 ...; Fetch data and template asynchronously. """ import sys from snakestation.handlers import TemplatedHTMLFinder from snakestation.utils import DataHandle from potusclient import PrecinctList class Handler (TemplatedHTMLFinder): """The business end of the stick.""" TEMPLATE_NAME = 'Potus' ASYNCHRONOUS_TEMPLATE = True ASYNCHRONOUS_REQUEST_RESPONSE = True def __init__(self,**args): TemplatedHTMLFinder.__init__(self,**args) self.precinct = '' self.leg_district = '' self.county = '' self.precincts = None return def analyze(self): request = self.request self.precinct = request.getfirst('precinct','') self.leg_district = request.getfirst('leg_district','') self.county = request.getfirst('county','') if (len(self.precinct) + len(self.leg_district) + len(self.county)) < 1: self.error = 'Whaaaat? (no arguments)' return return def post_template(self): if (len(self.precinct) + len(self.leg_district) + len(self.county)) > 0: self.precincts = self.request_response( PrecinctList(self.template_map['Precincts']), 'Precinct Lookup error: ', DataHandle([]), self.precinct, self.leg_district, self.county ) return def prepare(self): if self.precincts is not None: substitutions = [('precinct',self.dict_list_as_tuples(self.precincts.data))] else: substitutions = [ ('precinct', () ) ] for key in ('error',): substitutions += [ ( key, getattr(self,key,'') ) ] self.response.substitute( substitutions ) return def main(): handler = Handler() handler.template_map = { 'Potus' : 'http://flame.m3047.inwa.net/snakestation/potus-04.html', 'Precincts' : 'http://devil.m3047.inwa.net/elections/perl/election-product.cgi' } handler.main() if __name__ == '__main__': main()