-import re
-
-from nominatim.errors import UsageError
-from nominatim.tools import country_info
-
-class _PostcodeMatcher:
- """ Matches and formats a postcode according to the format definition.
- """
- def __init__(self, country_code, config):
- if 'pattern' not in config:
- raise UsageError("Field 'pattern' required for 'postcode' "
- f"for country '{country_code}'")
-
- self.pattern = re.compile(config['pattern'].replace('d', '[0-9]')
- .replace('l', '[A-Z]'))
-
-
- def normalize(self, postcode):
- """ Return the normalized version of the postcode. If the given postcode
- does not correspond to the usage-pattern, return null.
- """
- normalized = postcode.strip().upper()
-
- return normalized if self.pattern.fullmatch(normalized) else None