Next / Previous / Contents / Shipman's homepage

4.7.  processFile(): Process one input file

This function handles all the processing for one DocBook source file.

litdocbook5
# - - -   p r o c e s s F i l e

def processFile(args, fileName):
    '''Process one input file.

      [ P: args is an Args instance
        P: inFileName is a string
        if inFileName names a readable, valid DocBook XML file ->
          output files named in lit-elts from that file  :=
            lit-content of those lit-elts
            sys.stdout  +:=  report on processing that file as
                specified by args
            sys.stderr  +:=  error messages from processing that
                             file, if any
          else ->
            sys.stderr  +:=  error message ]
    '''

The fragments in a given file may be directed to several different output files. To keep track of the output files we have seen so far, we'll use a dictionary named fileMap, whose keys are file names, and each corresponding value is an open, writeable file handle for that file. We'll write the text to each file as it is encountered, and leave all the files open until the end, at which point we'll close them all.

litdocbook5
    #-- 1
    fileMap = {}
    if args.isVerbose:
        print("\n=== Processing {0!r}".format(fileName))

Next we call the etree package to parse the XML file and make it into an element tree. This may raise either of two exceptions:

litdocbook5
    #-- 2
    # [ if fileName names a readable, valid XML file ->
    #     doc  :=  an ElementTree representing that file
    #   else ->
    #     sys.stderr  +:=  error message(s)
    #     return ]
    try:
        doc = et.parse(fileName)
    except IOError as x:
        message("*** I/O error opening '{0}': {1}".format(
                fileName, str(x)))
        return
    except et.ParseError as x:
        message("*** XML syntax error opening '{0}': {1}".format(
                fileName, str(x)))
        return

Now that doc contains the document tree, send it off for processing to Section 4.8, “processDoc(): Process one document tree”.

litdocbook5
    #-- 3
    # [ P: doc is an ElementTree
    #   P: fileMap is a dictionary whose keys are file names and
    #      each corresponding value is a writeable file handle
    #      for that file
    #   fileMap  :=  fileMap with new file names added from
    #       lit-dests in doc
    #   file handles in fileMap  :=  lit-content of those files
    #   sys.stderr  +:=  error messages from processing doc,
    #       if any ]
    processDoc(fileMap, doc)

Finally, we close all the output files that are values in the fileMap dictionary.

litdocbook5
    #-- 4
    # [ fileMap is a dictionary whose values are file objects ->
    #     those values  :=  those values, closed
    #     sys.stdout  +:=  on file names and sizes as specified
    #                      by args ]
    for outFileName in sorted(fileMap):
        outFile = fileMap[outFileName]
        if args.isVerbose:
            print("{0:8d} {1}".format(
                  outFile.tell(), outFileName))
        outFile.close()