]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/factory.py
Merge pull request #2641 from lonvia/reinit-tokenizer-dir
[nominatim.git] / nominatim / tokenizer / factory.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Functions for creating a tokenizer or initialising the right one for an
9 existing database.
10
11 A tokenizer is something that is bound to the lifetime of a database. It
12 can be choosen and configured before the intial import but then needs to
13 be used consistently when querying and updating the database.
14
15 This module provides the functions to create and configure a new tokenizer
16 as well as instanciating the appropriate tokenizer for updating an existing
17 database.
18
19 A tokenizer usually also includes PHP code for querying. The appropriate PHP
20 normalizer module is installed, when the tokenizer is created.
21 """
22 import logging
23 import importlib
24 from pathlib import Path
25
26 from ..errors import UsageError
27 from ..db import properties
28 from ..db.connection import connect
29
30 LOG = logging.getLogger()
31
32 def _import_tokenizer(name):
33     """ Load the tokenizer.py module from project directory.
34     """
35     src_file = Path(__file__).parent / (name + '_tokenizer.py')
36     if not src_file.is_file():
37         LOG.fatal("No tokenizer named '%s' available. "
38                   "Check the setting of NOMINATIM_TOKENIZER.", name)
39         raise UsageError('Tokenizer not found')
40
41     return importlib.import_module('nominatim.tokenizer.' + name + '_tokenizer')
42
43
44 def create_tokenizer(config, init_db=True, module_name=None):
45     """ Create a new tokenizer as defined by the given configuration.
46
47         The tokenizer data and code is copied into the 'tokenizer' directory
48         of the project directory and the tokenizer loaded from its new location.
49     """
50     if module_name is None:
51         module_name = config.TOKENIZER
52
53     # Create the directory for the tokenizer data
54     basedir = config.project_dir / 'tokenizer'
55     if not basedir.exists():
56         basedir.mkdir()
57     elif not basedir.is_dir():
58         LOG.fatal("Tokenizer directory '%s' cannot be created.", basedir)
59         raise UsageError("Tokenizer setup failed.")
60
61     # Import and initialize the tokenizer.
62     tokenizer_module = _import_tokenizer(module_name)
63
64     tokenizer = tokenizer_module.create(config.get_libpq_dsn(), basedir)
65     tokenizer.init_new_db(config, init_db=init_db)
66
67     with connect(config.get_libpq_dsn()) as conn:
68         properties.set_property(conn, 'tokenizer', module_name)
69
70     return tokenizer
71
72
73 def get_tokenizer_for_db(config):
74     """ Instantiate a tokenizer for an existing database.
75
76         The function looks up the appropriate tokenizer in the database
77         and initialises it.
78     """
79     basedir = config.project_dir / 'tokenizer'
80     if not basedir.is_dir():
81         # Directory will be repopulated by tokenizer below.
82         basedir.mkdir()
83
84     with connect(config.get_libpq_dsn()) as conn:
85         name = properties.get_property(conn, 'tokenizer')
86
87     if name is None:
88         LOG.fatal("Tokenizer was not set up properly. Database property missing.")
89         raise UsageError('Cannot initialize tokenizer.')
90
91     tokenizer_module = _import_tokenizer(name)
92
93     tokenizer = tokenizer_module.create(config.get_libpq_dsn(), basedir)
94     tokenizer.init_from_project(config)
95
96     return tokenizer