This class is used to parse the four-letter species code (e.g.,
"CRHA" for Crane Hawk) used in MAPS sets. For
its format, see the
specification.
# - - - - - c l a s s S p e c 4 F i e l d - - - - -
class Spec4Field(FieldItem):
'''Represents a 4-letter species field.
Exports, other than those inherited:
Spec4Field.scanField(encounter, scan, fieldName): # Static
[ (encounter is a BaseEncounter object) and
(scan is a Scan object) and
(fieldName is a field name in self.OUT_FIELD_LIST) ->
if scan starts with a valid 4-letter species code in
the context of encounter ->
scan := scan advanced past that field
encounter.(fieldName) := a Species object representing
that field
else ->
scan := scan advanced no further than end of line
Log() +:= error message(s)
raise SyntaxError ]
Spec4Field.flatten(): [ as in base class ]
'''
This method scans a 4-letter species code.
# - - - S p e c 4 F i e l d . s c a n F i e l d - - -
@staticmethod
def scanField(encounter, scan, fieldName):
'''Scan a 4-letter species code.
'''
#-- 1 --
# [ if the line in scan has at least SPEC4_L characters ->
# scan := scan advanced SPEC4_L
# rawCode := those characters, uppercased
# else ->
# Log() +:= error message
# raise SyntaxError ]
try:
rawCode = scan.move(SPEC4_L).upper()
except IndexError:
scan.syntax("Expecting a %d-character species code." %
SPEC4_L)
Next we check the code for validity. IBP has
specifically requested that invalid codes be preserved in
the record, so we issue a warning instead of an error.
See Section 13.1, “BaseSpeciesSet.lookupSpec4()”.
#-- 2 --
# [ if rawCode is not a 4-letter code defined in
# encounter.compiler.speciesSet ->
# Log() +:= warning message
# else -> I ]
try:
species = encounter.compiler.speciesSet.lookupSpec4 (
rawCode)
except KeyError:
scan.warning("Species code '%s' is undefined." %
rawCode)
#-- 3 --
setattr(encounter, SPEC4_ATTR,
Spec4Field(encounter, rawCode))