Next / Previous / Contents / Shipman's homepage

66. class StationCodeField: Station code

This class represents a station code. See the specification.

Although we look up the code in the compiler's .stationSet, we don't store the value as a Station object. We store only the station code. The reason for this is that sometimes we have to enter sets that contain station codes that aren't yet defined in the authority files. So if the station code is not so defined, we'll store the station code anyway, and IBP can resolve it later.
# - - - - -   c l a s s   S t a t i o n C o d e F i e l d   - - - - -

class StationCodeField(FieldItem):
    '''Represents a station code.

      Exports: as inherited.

66.1. StationCodeField.scanField()

This method scans a station code field. If the code isn't in the compiler's station code set, it's only a warning.
# - - -   S t a t i o n C o d e F i e l d . s c a n F i e l d   - - -

    def scanField(encounter, scan, fieldName):
        '''Scan a station code field.

We don't have to do a lot of syntax checking on a station code. They should all be in the station codes authority file.
        #-- 1 -
        # [ if the line in scan starts with STA_CODE_L characters ->
        #     scan  :=  scan advanced by STA_CODE_L
        #     code  :=  next STA_CODE_L characters from scan, uppercased
        #   else ->
        #     Log()  +:=  error message
        #     raise SyntaxError ]
            code = scan.move(STA_CODE_L).upper()
        except IndexError:
            scan.syntax("Expecting a %d-character station code field." %

See Section 9.3, “Location.lookupStaCode().
        #-- 2 --
        # [ encounter.compiler describes a multi-station set ->
        #     if code is a station code defined in
        #     encounter.compiler.stationSet ->
        #       I
        #     else ->
        #       Log()  +:=  a warning message ]
        location = encounter.compiler.location
            station = location.lookupStaCode(code)
        except KeyError:
            scan.warning("Station code '%s' is not defined in "
                "location '%s'." %
                (code, location.locCode))

        #-- 3 --
        # [ encounter.(STATION_ATTR)  :=  code ]
        setattr(encounter, STATION_ATTR,
                StationCodeField(encounter, code))