do not hide errors when importing tokenizer

Explicitly check for the tokenizer source file to check that
the name is correct. We can't use the import error for that
because it hides other import errors like a missing
library.

Fixes #2327.
This commit is contained in:
Sarah Hoffmann
2021-05-18 16:28:21 +02:00
parent 54b06d7abc
commit b2722650d4
2 changed files with 8 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ normalizer module is installed, when the tokenizer is created.
"""
import logging
import importlib
from pathlib import Path
from ..errors import UsageError
from ..db import properties
@@ -25,12 +26,13 @@ LOG = logging.getLogger()
def _import_tokenizer(name):
""" Load the tokenizer.py module from project directory.
"""
try:
return importlib.import_module('nominatim.tokenizer.' + name + '_tokenizer')
except ModuleNotFoundError as exp:
src_file = Path(__file__).parent / (name + '_tokenizer.py')
if not src_file.is_file():
LOG.fatal("No tokenizer named '%s' available. "
"Check the setting of NOMINATIM_TOKENIZER.", name)
raise UsageError('Tokenizer not found') from exp
raise UsageError('Tokenizer not found')
return importlib.import_module('nominatim.tokenizer.' + name + '_tokenizer')
def create_tokenizer(config, init_db=True, module_name=None):