Next / Previous / Contents / Shipman's homepage

80.4. iband7 main()

This is the main program for iband7.

iband7
# - - -   m a i n   - - -

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

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.

iband7
    #-- 1 --
    # [ Log()  :=  the Log() singleton writing to LOG_FILE ]
    Log().addLogFile(LOG_FILE)
    Log().write("=== %s %s ===" %
        ("iband7", EXTERNAL_VERSION))

There should be only one command line argument, the name of the input file.

iband7
    #-- 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]

Next, we instantiate the objects representing the stations authority file object and the species authority files. See Section 11, “class MapsStationSet: Station set object”.

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

See Section 14, “class Maps2004SpeciesSet.

iband7
    #-- 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" %
            (Maps2004SpecieSet.AUTHORITY_FILE, detail))

The Maps2006Compiler class is the machine that takes those two authority files and an input file. First we instantiate the compiler object: see Section 20, “class Maps2006Compiler: Compiler for MAPS 2006 files”.

iband7
    #-- 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 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 = Maps2006Compiler(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 using Microsoft line terminators (CR+LF).

iband7
    #-- 6 --
    # [ outFile  +:=  flattened objects generated by compiler ]
    count = 0
    for  encounter in compiler:
        outFile.write("%s\r\n" % 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.

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