def ValidateLanguageCode(lang, column_name=None, problems=None):
    """
  Validates a non-required language code value using the pybcp47 module:
    - if invalid adds InvalidValue error (if problems accumulator is provided)
    - distinguishes between 'not well-formed' and 'not valid' and adds error
      reasons accordingly
    - an empty language code is regarded as valid! Otherwise we might end up
      with many duplicate errors because of the required field checks.
    - returns true if the language is valid, false if not well-formed or
      invalid.
  """
    if util.IsEmpty(lang):
        return True
    bcp47_obj = parser.ParseLanguage(str(lang.lower()))
    if not bcp47_obj.wellformed:
        if problems:
            problems.InvalidValue(column_name,
                                  lang,
                                  'language code "%s" is not well-formed' %
                                  lang,
                                  type=problems_class.TYPE_ERROR)
        return False
    if not bcp47_obj.valid:
        if problems:
            problems.InvalidValue(
                column_name,
                lang,
                'language code "%s" is not valid, parses as: %s' %
                (lang, bcp47_obj),
                type=problems_class.TYPE_WARNING)
        return False
    return True
Example #2
0
 def ValidateVehicleType(self, problems):
     self.vehicle_type = util.ValidateAndReturnIntValue(
         self.vehicle_type,
         self._gtfs_factory.Route._ROUTE_TYPE_IDS,
         None,
         True,
         "vehicle_type",
         problems,
     )
     # Entrances must not have a vehicle type, in general google transit does not
     # read vehicle types from stops with a parent station.
     if self.vehicle_type:
         if self.location_type == 2:
             problems.InvalidValue(
                 "vehicle_type",
                 self.location_type,
                 reason="an entrance must not have a vehicle type",
             )
         elif not util.IsEmpty(self.parent_station):
             problems.InvalidValue(
                 "vehicle_type",
                 self.location_type,
                 reason="Google Transit does not read vehicle types for stops "
                 "having a parent station",
                 type=problems_module.TYPE_WARNING,
             )
Example #3
0
 def ValidateStopLocationType(self, problems):
   self.location_type = util.ValidateAndReturnIntValue(
       self.location_type, [0, 1, 2], 0, True, 'location_type', problems)
   # Entrances must have a parent_station.
   if self.location_type == 2 and util.IsEmpty(self.parent_station):
     problems.InvalidValue('location_type', self.location_type,
         reason='an entrance must have a parent_station')
Example #4
0
 def ValidateCo2PerKm(self, problems):
     if not util.IsEmpty(self.co2_per_km):
         try:
             self.co2_per_km = float(self.co2_per_km)
         except ValueError:
             problems.InvalidValue('co2_per_km', self.co2_per_km)
Example #5
0
 def ValidateCurrencyType(self, problems):
   if util.IsEmpty(self.currency_type):
     problems.MissingValue("currency_type")
   elif self.currency_type not in util.ISO4217.codes:
     problems.InvalidValue("currency_type", self.currency_type)
Example #6
0
 def ValidateFareId(self, problems):
   if util.IsEmpty(self.fare_id):
     problems.MissingValue("fare_id")