Next / Previous / Contents / Shipman's homepage

9. main(): Main logic of listext2

listext2
# - - - - -   m a i n

def main():
    '''Main program sequence.

      [ if (all command line options are valid) and
        (specified input file can be opened for reading) and
        (effective output file can be created new) ->
          effective output file  :=  a PDF representing a straight
              text listing of the input file
    '''

We instantiate the Args() singleton here to check all the command line arguments before we go any further; see Section 18, “class Args: Command line arguments”. Next, we open the input file, just to make sure it'll open.

listext2
    #-- 1
    # [ if the command line arguments are valid ->
    #     Args()  :=  a singleton representing those arguments
    #   else ->
    #     sys.stderr  +:=  usage and error messages
    #     stop execution ]
    Args()

The purpose of Section 19, “class BodyLayout: Page geometry” is to compute the final dimensions of the page features.

listext2
    #-- 2
    # [ bodyLayout  :=  a BodyLayout instance describing a page
    #                   as configured by Args() ]
    bodyLayout = BodyLayout()

The purpose of Section 20, “class InputFile: Input sliced to fit” is to read the input file and calculate line breaks for overflow lines, and column breaks, whether explicit or caused by a full column. Its constructor needs the bodyLayout instance to predict how many characters will fit on a line and how many lines will fit in a column.

listext2
    #-- 3
    # [ inputFile  :=  a new InputFile instance representing the
    #       contents of inFile, with line and column breaks applied
    #       where specified by Args() and bodyLayout ]
    inputFile = InputFile(Args().inFile, bodyLayout)

We have everything we need now to build the output XSL-FO tree. Two results are returned by Section 10, “treeSetup(): Build the XSL-FO layout”: tree is an h.FlowTree instance representing the entire output document, and pages is the page-sequence element within that tree where we will add the running heads and the main content.

listext2
    #-- 4
    # [ tree  :=  a new h.FlowTree instance containing XSL-FO formatted
    #             according to Args() and bodyLayout
    #   flow  :=  the flow element of that tree ]
    tree, pages = treeSetup(bodyLayout)

For the logic that fills in the running heads and main content, see Section 14, “addFlow(): Populate the page-sequence element”.

listext2
    #-- 5
    # [ pages  +:=  (running heads as specified by Args()) +
    #      (XSL-FO renderings of lines from inputFile using the
    #      layout described by (bodyLayout) ]
    addFlow(pages, bodyLayout, inputFile)

All that remains is to serialize the document as XML.

listext2
    #-- 6
    # [ if file (Args().outFileName) can be created new ->
    #     that file  :=  tree, serialized as XML
    #   else ->
    #     sys.stderr  +:=  error message ]
    try:
        with file(Args().outFileName, 'w') as outFile:
            tree.write(outFile)
    except IOError, details:
        fatal("Can't open output file '%s': %s" %
                (Args().outFileName, details))

Finally, report the statistics on the input file.

listext2
    #-- 7
    print( "--- Input file '{0}' ---".format(Args().inFileName))
    print("{0:9d} input line count".format(inputFile.nInLines))
    print("{0:9d} input lines overflow count".format(
          inputFile.nOverflows))
    print("{0:9d} output line count".format(inputFile.nOutLines))
    print("{0:9d} output page count".format(inputFile.nOutPages))
    Args().inFile.close()