This is the main program for imaws2004.
Its sole different from imaws2007 is
that instead of the Maws2007Compiler
class, it uses Section 23, “class Maws2004Compiler: MAWS compiler”.
# - - - m a i n - - -
def main():
'''Main program for imaws2004.
'''
#-- 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))
#-- 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))
#-- 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 = Maws2004Compiler(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:
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)