Next / Previous / Contents / Shipman's homepage

83.4. imaws2007 main()

This is the main program for imaws2007.

imaws2007
# - - -   m a i n   - - -

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

    #-- 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))

Here's the first deviation from iband7: different stations and species authority files.

imaws2007
    #-- 3 --
    # [ if the current directory contains a readable, valid MAWS
    #   stations authority file ->
    #     stationSet  :=  a MawsStationSet object representing
    #                     that file
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    try:
        stationSet = MawsStationSet()
    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
    #   MAWS 2002 species authority file ->
    #     speciesSet  :=  a Maws2002SpeciesSet representing that file
    #   else ->
    #     Log()  +:=  error message
    #     stop execution ]
    try:
        speciesSet = Maws2002SpeciesSet()
    except IOError, detail:
        Log().fatal("Can't open the species authority file: "
            "'%s': %s" %
            (Maws2002SpeciesSet.AUTHORITY_FILE, detail))

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

imaws2007
    #-- 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 = Maws2007Compiler(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 rest of the logic is the same as in iband7.

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

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