This code is identical to showstas, except
that it uses MawsStationSet instead of
MapsStationSet; see
Section 12, “class MawsStationSet: Station set object”.
# - - - - - m a i n - - - - -
def main():
'''Main program for showmaws.
[ MAWS_STATIONS_FILE exists and is readable ->
if sys.argv contains a single station number defined
in MAWS_STATIONS_FILE ->
sys.stdout +:= list of all stations in the location
containing that station number
else if sys.argv contains a single location code
defined in MAWS_STATIONS_FILE ->
sys.stdout +:= list of all stations in that location
else ->
Log() +:= error message ]
'''
#-- 1 --
# [ if MAWS_STATIONS_FILE is a readable, valid MAWS stations
# authority file ->
# stationSet := a MawsStationSet object representing
# that file
# else ->
# Log() +:= error message(s)
# stop execution ]
try:
stationSet = MawsStationSet()
except IOError, detail:
Log().fatal("Error in the stations authority file: %s" %
detail)
#-- 2 --
# [ if sys.argv contains exactly one argument ->
# arg := that argument
# else ->
# Log() +:= error message
# stop execution ]
argList = sys.argv[1:]
if len(argList) == 1:
arg = argList[0]
else:
Log().message("Usage:")
Log().message(" %s locCode" % sys.argv[0])
Log().message(" %s staNo" % sys.argv[0])
Log().fatal("Wrong argument count.")
#-- 3 --
# [ if (arg starts with a digit) and
# (arg is a station number defined in stationSet ->
# loc := the location from stationSet containing
# that station number
# else if arg is a location code defined in stationSet ->
# loc := the location from stationSet for that location
# code
# else ->
# Log() +:= error message
# stop execution ]
if ((len(arg) > 0) and
(arg[0].isdigit())):
#-- 3.1 --
# [ if arg is a station number defined in stationSet ->
# loc := the location from stationSet containing
# that station number
# else ->
# Log() +:= error message
# stop execution
try:
sta = stationSet.stationNoLookup(arg)
loc = sta.loc
except KeyError:
Log().fatal("No such station number: '%s'" % arg)
else:
#-- 3.2 --
# [ if arg is a location code defined in stationSet ->
# loc := the corresponding location from stationSet
# else ->
# Log() +:= error message
# stop execution
try:
loc = stationSet.locationLookup(arg)
except KeyError:
Log().fatal("No such location code: '%s'" % arg)
#-- 4 --
# [ sys.stdout +:= (display of loc and all its stations) ]
staCodeList = loc.staCodeMap.keys()
staCodeList.sort()
for staCode in staCodeList:
sta = loc.lookupStaCode(staCode)
print " ", sta