Next / Previous / Contents / Shipman's homepage

79.4. iband8: main()

iband8
# - - - - -   m a i n

def main():
    '''Main program for iband8.
    '''
    #-- 1 --
    # [ Log()  :=  the Log() singleton writing to LOG_FILE ]
    Log().addLogFile(LOG_FILE)
    Log().write("=== %s %s ===" %
        ("iband8", EXTERNAL_VERSION))

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

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

    #-- 5 --
    # [ if (inFileName is a validly-named, readable input file) and
    #   (inFileName+OUT_EXTENSION) can be opened new for writing) ->
    #     compiler  :=  a new BaseCompiler 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 = BaseCompiler(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))

    #-- 6 --
    # [ outFile  +:=  flattened objects generated by compiler ]
    count = 0
    for  encounter in compiler:
        outFile.write("%s\r\n" % encounter.flatten())
        count  +=  1

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