Next / Previous / Contents / Shipman's homepage

81.4. maps2004 main()

This is the main program for maps2004.

maps2004
# - - -   m a i n   - - -

def main():
    '''Main program for maps2004.
    '''

Before we do anything that might cause an error message, we need to add our LOG_FILE to the places where the Log() singleton will write error messages.

maps2004
    #-- 1 --
    # [ Log()  :=  the Log() singleton writing to LOG_FILE ]
    Log().addLogFile(LOG_FILE)

    #-- 2 --
    # [ if sys.argv[1:] contains one argument ->
    #     inFileName  :=  that argument
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    argList = sys.argv[1:]
    if  len(argList) != 1:
        Log().fatal("Takes one argument, the input file name.")
    else:
        inFileName = argList[0]
        Log().write("=== %s %s: %s ===" %
            (sys.argv[0], EXTERNAL_VERSION, inFileName))

See Section 11, “class MapsStationSet: Station set object”.

maps2004
    #-- 3 --
    # [ if the current directory contains a readable, valid MAPS
    #   stations authority file ->
    #     stationSet  :=  a MapsStationSet object representing
    #                     that file
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    try:
        stationSet = MapsStationSet()
    except IOError, detail:
        Log().fatal("Can't open the stations authority file: "
            "'%s': %s" %
            (MapsStationSet.AUTHORITY_FILE, detail))

    #-- 4 --
    # [ if the current directory contains a readable, valid
    #   MAPS 2004 species authority file ->
    #     speciesSet  :=  a Maps2004SpeciesSet representing that file
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    try:
        speciesSet = Maps2004SpeciesSet()
    except IOError, detail:
        Log().fatal("Can't open the species authority file: "
            "'%s': %s" %
            (Maps2004SpeciesSet.AUTHORITY_FILE, detail))

The Maps2004Compiler class is the machine that takes those two authority files and an input file. First we instantiate the compiler object.

maps2004
    #-- 5 --
    # [ if (inFileName is a validly-named, readable input file) and
    #   (inFileName+OUT_EXTENSION) can be opened new for writing) ->
    #     compiler  :=  a new Maps2006Compiler object that
    #         for that file, using authority objects
    #         stationSet and speciesSet
    #     outFile  :=  a new file named (inFileName+OUT_EXTENSION)
    #                  opened for writing
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    try:
        compiler = Maps2004Compiler(inFileName,
                        stationSet, speciesSet)
        outFileName = inFileName + OUT_EXTENSION
        outFile = open(outFileName, "w")
    except ValueError, detail:
        Log().fatal("Input file '%s' problem: %s" %
                      (inFileName, detail))
    except IOError, detail:
        Log().fatal("Can't open output file '%s': %s" %
                      (outFileName, detail))

The compiler object is an iterator that reads through the input file, generating a sequence of BaseEncounter objects representing the valid lines. Each such object is flattened using its .flatten() method, then written to the output file.

maps2004
    #-- 6 --
    # [ outFile  +:=  flattened objects generated by compiler ]
    count = 0
    for  encounter in compiler:
        print >>outFile, encounter.flatten()
        count  +=  1

Three statistics are reported at the end of the run: the number of valid records written; the number of errors sent to the Log() object since its instantiation; and the number of warnings.

maps2004
    #-- 7 --
    outFile.close()
    text = ("=== %d records written; errors %d; warnings %d. ===" %
               (count, Log().count(), Log().count(WARNING_KIND)))
    Log().write(text)