Next / Previous / Contents / Shipman's homepage

85.3. showstas: main

The main has three tasks: instantiate a MapsStationSet containing the station codes from the authority file; process the command line argument, which must be a station number or location code; and, assuming the argument is valid, display all the stations in the location containing that argument.

showstas
# - - - - -   m a i n   - - - - -

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

      [ the current directory contains a readable, valid
        MAPS stations authority file ->
          if sys.argv contains a single station number defined
          in that 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 that file ->
            sys.stdout  +:=  list of all stations in that location
          else ->
            Log()  +:=  error message ]
    '''

The first step is to read the stations authority file by instantiating the MapsStationSet object described in Section 11, “class MapsStationSet: Station set object”.

showstas
    #-- 1 --
    # [ if the current directory contains a readable, valid
    # MAPS stations authority file ->
    #     stationSet  :=  a MapsStationSet object representing
    #         that file
    #   else ->
    #     Log()  +:=  error message(s)
    #     stop execution ]
    try:
        stationSet = MapsStationSet()
    except IOError, detail:
        Log().fatal("Error in the stations authority file: %s" %
                      detail)

Next, we check the command line arguments. There had better be exactly one.

showstas
    #-- 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.")

If arg starts with a digit, we assume it's a station number; otherwise it's considered a location code. In either case, we want to wind up with loc set to the Location object specified by that argument. See Section 8.2, “BaseStationSet.stationNoLookup() and Section 8.1, “BaseStationSet.locationLookup().

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

Finally, we display all the stations in loc, sorted in ascending order by station code (not station number). The formatting of each line of this report is done by Location.__str__. See Section 9.3, “Location.lookupStaCode().

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