Next / Previous / Contents / Shipman's homepage

18. buildRow(): Build one row of the index table

This function builds the row holding all the months in a single year of the index table.

noteweb
# - - -   b u i l d R o w

def buildRow ( tbody, yearRow ):
    '''Builds one row of the index table, holding one year.

      [ (tbody is an et.Element) and
        (yearRow is a YearRow instance) ->
          tbody  +:=  a tr element made from yearRow ]
    '''

The first step is to add the tr element to hold the table row. There is one minor complication: if the year number is divisible by the constant defined in Section 10.11, “YEAR_GROUP_FREQUENCY, we'll add a class='year-group' attribute to produce a thicker lower border on that row, to improve legibility of the table.

noteweb
    #-- 1 --
    # [ tbody  :=  a new 'tr' element 
    #   tr     :=  that element ]
    tr  =  et.SubElement ( tbody, 'tr' )

    #-- 2 --
    # [ if int(yearRow.yyyy) is divisible by YEAR_GROUP_FREQUENCY ->
    #     tr  +:=  a class=YEAR_GROUP_CLASS attribute
    #   else ->
    #     tr  +:=  a class=YEAR_ROW_CLASS attribute ]
    if ( int(yearRow.yyyy) % YEAR_GROUP_FREQUENCY ) == 0:
        tr.attrib['class']  =  YEAR_GROUP_CLASS
    else:
        tr.attrib['class']  =  YEAR_ROW_CLASS

The first cell in the row is always a th containing the row heading; it carries the CSS class name given in Section 10.14, “ROW_LABEL_CLASS, and is linked to the CSS rule in Section 5.6, “th.row-label: The row label”.

noteweb
    #-- 3 --
    # [ tr  +:=  a th element with class=ROW_LABEL_CLASS
    #            containing yearRow.yyyy ]
    rowLabel  =  et.SubElement ( tr, 'th' )
    rowLabel.attrib['class']  =  ROW_LABEL_CLASS    
    rowLabel.text  =  yearRow.yyyy

We'll need to add exactly twelve td elements, regardless of how many months have actual data (and it might even be that none of them do). Therefore, the loop that builds these cells is driven by iterating over the possible month keys ('01', '02', ..., '12'): for each key, we try to pull out the corresponding MonthCell from yearRow; this causes a KeyErrror if the year does not have that month. See Section 19, “buildMonthCell(): Build one monthly cell in the index table”.

noteweb
    #-- 4 --
    # [ tr  +:=  twelve cells containing links to the elements
    #       of yearRow corresponding to the twelve months, or
    #       non-breaking spaces where months are absent ]
    for monthKey in [ '%02d' % x
                      for x in range(1,13) ]:
        #-- 4 body --
        # [ monthKey is in the range ['01', '02', ..., '12'] ->
        #     if yearRow[monthKey] exists ->
        #         tr  +:=  a td element containing a link to
        #             the month page for yearRow[monthKey]
        #     else ->
        #         tr  +:=  a td element containing a non-breaking
        #                  space ]
        buildMonthCell ( tr, yearRow, monthKey )