def ensure_it_is_a_number(string, german): """Ensures string can be parsed to a positive or negative number.""" trimmed = string.strip() if len(trimmed) == 0: trimmed = '0' no_space = trimmed.replace(' ', '') if ',' in no_space: replaced = no_space.replace(',', '.') else: replaced = no_space illegal_char_index = __index_of_first_illegal_char_number_only(replaced) if illegal_char_index != -1: if german: raise exceptions.ParseException(INVALID_SYMBOL_ERROR_DE + " '" + replaced[illegal_char_index] + "'") else: raise exceptions.ParseException(INVALID_SYMBOL_ERROR_EN + " '" + replaced[illegal_char_index] + "'") if len(replaced) > 10: replaced = replaced[:11] if not __is_positive(replaced): final = replaced.replace('-', '0', 1) return float(final) * -1 return float(replaced)
def ensure_it_is_a_valid_mgrs_square(string, german): """Ensures string consists of 2 letters""" trimmed = string.strip() no_space = trimmed.replace(' ', '').upper() if len(no_space) > 2: if german: raise exceptions.ParseException(INPUT_TO_LONG_DE) else: raise exceptions.ParseException(INPUT_TO_LONG_EN) for i in range(0, len(no_space)): if i == 0: if no_space[i].isalpha(): continue else: if german: raise exceptions.ParseException( SQUARE_HAS_TO_START_WITH_DE) else: raise exceptions.ParseException( SQUARE_HAS_TO_START_WITH_EN) elif i == 1: if no_space[i].isalpha(): continue else: if german: raise exceptions.ParseException( SQUARE_HAS_TO_BE_TWO_LETTERS_DE) else: raise exceptions.ParseException( SQUARE_HAS_TO_BE_TWO_LETTERS_EN) else: if no_space[i].isalpha() or no_space[i].isdigit(): char_int = ord(no_space[i].lower()) if char_int < ord('a') or char_int > ord('z'): if german: raise exceptions.ParseException( INVALID_SYMBOL_ERROR_DE + no_space[i]) else: raise exceptions.ParseException( INVALID_SYMBOL_ERROR_EN + no_space[i]) if len(no_space) != 2: if german: raise exceptions.ParseException(INPUT_NOT_COMPLETE_DE) else: raise exceptions.ParseException(INPUT_NOT_COMPLETE_EN) return no_space
def ensure_it_is_a_positive_integer(string, german): """Ensures string can be parsed to a positive integer.""" trimmed = string.strip() if len(trimmed) == 0: trimmed = '0' no_space = trimmed.replace(' ', '') if ',' in no_space or '.' in no_space: if german: raise exceptions.ParseException(VALUE_HAS_TO_BE_INTEGER_DE) else: raise exceptions.ParseException(VALUE_HAS_TO_BE_INTEGER_EN) illegal_char_index = __index_of_first_illegal_char_positive_number_only( no_space) if illegal_char_index != -1: if german: raise exceptions.ParseException(INVALID_SYMBOL_ERROR_DE + " '" + no_space[illegal_char_index] + "'") else: raise exceptions.ParseException(INVALID_SYMBOL_ERROR_EN + " '" + no_space[illegal_char_index] + "'") return int(no_space)
def ensure_mgrs_northing_in_range(northing, german): if not 0 <= northing < 100000: if german: raise exceptions.ParseException(MGRS_NORTHING_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(MGRS_NORTHING_OUT_OF_RANGE_EN)
def ensure_mgrs_easting_in_range(easting, german): if not 0 <= easting < 100000: if german: raise exceptions.ParseException(MGRS_EASTING_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(MGRS_EASTING_OUT_OF_RANGE_EN)
def ensure_zone_in_range(zone, german): if not 1 <= zone <= 60: if german: raise exceptions.ParseException(ZONE_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(ZONE_OUT_OF_RANGE_EN)
def ensure_seconds_in_range(seconds, german): if seconds < 0 or seconds >= 60: if german: raise exceptions.ParseException(SECONDS_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(SECONDS_OUT_OF_RANGE_EN)
def ensure_minutes_in_range(minutes, german): if minutes < 0 or minutes >= 60: if german: raise exceptions.ParseException(MINUTES_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(MINUTES_OUT_OF_RANGE_EN)
def ensure_latitude_in_range(latitude, german): if latitude < -90 or latitude > 90: if german: raise exceptions.ParseException(LATITUDE_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(LATITUDE_OUT_OF_RANGE_EN)
def ensure_longitude_in_range(longitude, german): if longitude < -180 or longitude > 180: if german: raise exceptions.ParseException(LONGITUDE_OUT_OF_RANGE_DE) else: raise exceptions.ParseException(LONGITUDE_OUT_OF_RANGE_EN)
def ensure_it_is_a_valid_mgrs_zone(string, german): """Ensures string consists of 2 digits and a letter""" trimmed = string.strip() no_space = trimmed.replace(' ', '').upper() if len(no_space) > 3: if german: raise exceptions.ParseException(INPUT_TO_LONG_DE) else: raise exceptions.ParseException(INPUT_TO_LONG_EN) for i in range(0, len(no_space)): if i == 0: if no_space[i].isdigit(): continue else: if german: raise exceptions.ParseException( ZONE_HAS_TO_START_WITH_NUMBER_DE) else: raise exceptions.ParseException( ZONE_HAS_TO_START_WITH_NUMBER_EN) elif i == 1: if no_space[i].isdigit(): continue else: if german: raise exceptions.ParseException( ZONE_HAS_TO_CONTAIN_TWO_NUMBERS_DE) else: raise exceptions.ParseException( ZONE_HAS_TO_CONTAIN_TWO_NUMBERS_EN) else: if no_space[i].isalpha(): char_int = ord(no_space[i].lower()) if char_int < ord('a') or char_int > ord('z'): if german: raise exceptions.ParseException( INVALID_SYMBOL_ERROR_DE + " '" + no_space[i] + "'") else: raise exceptions.ParseException( INVALID_SYMBOL_ERROR_EN + " '" + no_space[i] + "'") lat_zone = ord(no_space[2]) zone_temp = int(no_space[:2]) if lat_zone < 67 or lat_zone > 88 or lat_zone == 73 or lat_zone == 79: if german: raise exceptions.ParseException( INVALID_LAT_GRID_ZONE_DE.format(chr(lat_zone))) else: raise exceptions.ParseException( INVALID_LAT_GRID_ZONE_EN.format(chr(lat_zone))) if lat_zone == ord('X') and (zone_temp == 32 or zone_temp == 34 or zone_temp == 36): if german: raise exceptions.ParseException( INVALID_MGRS_SQUARE_LETTRES_DE.format( str(zone_temp), chr(lat_zone))) else: raise exceptions.ParseException( INVALID_MGRS_SQUARE_LETTRES_EN.format( str(zone_temp), chr(lat_zone))) else: if german: raise exceptions.ParseException( ZONE_HAS_TO_CONTAIN_TWO_NUMBERS_AND_A_LETTER_DE) else: raise exceptions.ParseException( ZONE_HAS_TO_CONTAIN_TWO_NUMBERS_AND_A_LETTER_EN) if len(no_space) != 3: if german: raise exceptions.ParseException(INPUT_NOT_COMPLETE_DE) else: raise exceptions.ParseException(INPUT_NOT_COMPLETE_EN) return no_space