-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}'")
-
- pc_pattern = config['pattern'].replace('d', '[0-9]').replace('l', '[A-Z]')
-
- self.pattern = re.compile(f'(?:{country_code.upper()}[ -]?)?({pc_pattern})')
-
-
- 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()
-
- match = self.pattern.fullmatch(normalized)
-
- return match.group(1) if match else None