Next / Previous / Contents / TCC Help System / NM Tech homepage

10.9. hist.cgi: buildXhtml()

As discussed in Section 10, “hist.cgi: Generate a history detail page”, for XHTML output we use the sox module to avoid the memory requirements of building a potentially huge table that would be required if we used an lxml ElementTree. Also note that the page header and footer are generated by the tccpagesox module.

At this point, nothing has been sent to sys.stdout. We'll need to start by generating the HTTP header, then use the TCCPage constructor to generate the page's head element and start its body element.

hist.cgi
# - - -   b u i l d X h t m l

def buildXhtml(db, txny, circleList, cbcHist):
    '''Create the report in XHTML format.

      [ (db is a pycbc.CBCDatabase instance) and
        (txny is an xnomo3.Txny instance) and
        (circleList is a list of pycbc.Circle instances) and
        (cbcHist is a lib.CbcHist instance) ->
          sys.stdout  +:= the report requested by HistArgs() using
                db and txny, as an XHTML page ]
    '''
    #-- 1
    # [ sys.stdout  +:=  (CGI header for HTML output) + (page
    #       content through body title)
    #   sox  :=  a new sox.Sox instance writing to sys.stdout and
    #       containing tp.TCCPage starting content with NAV_LIST
    #   page  :=  a new tp.TCCPage instance writing to sox ]
    print "Content-type: text/html"
    print
    sox = Sox(sys.stdout)
    page = tp.TCCPage(sox, "Christmas Bird Count: History report",
                      lib.NAV_LIST, cssUrl="http://www.nmt.edu/~shipman/z/cbc/cbchist/cbchist.css")

All the logic that opens input resources and generates the page content is contained in a try/except block here, so that any bit of code anywhere that encounters a problem need only raise a ScriptError, and control will go directly to the error page generation logic; see Section 10.10, “hist.cgi: buildXhtmlBody(): Main report logic” and Section 18, “buildErrorSox().

hist.cgi
    #-- 2
    # [ sox  +:=  web content displaying circleList and cbcHist
    #       using db and txny ]
    buildXhtmlBody(sox, db, txny, circleList, cbcHist)

    #-- 3
    # [ sox  +:=  link to documentation ]
    lib.addDocLinkSox(sox)

    #-- 4
    # [ sox  +:=  (closing elements of page) ]
    page.end()