]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/special_phrases.py
66fb41ed4557fedbf972d58b84139c3f450744d7
[nominatim.git] / nominatim / clicmd / special_phrases.py
1 """
2     Implementation of the 'special-phrases' command.
3 """
4 import logging
5 from pathlib import Path
6 from nominatim.errors import UsageError
7 from nominatim.db.connection import connect
8 from nominatim.tools.special_phrases.sp_importer import SPImporter
9 from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
10 from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
11
12 LOG = logging.getLogger()
13
14 # Do not repeat documentation of subcommand classes.
15 # pylint: disable=C0111
16 # Using non-top-level imports to avoid eventually unused imports.
17 # pylint: disable=E0012,C0415
18
19 class ImportSpecialPhrases:
20     """\
21     Import special phrases.
22     """
23     @staticmethod
24     def add_args(parser):
25         group = parser.add_argument_group('Input arguments')
26         group.add_argument('--import-from-wiki', action='store_true',
27                            help='Import special phrases from the OSM wiki to the database.')
28         group.add_argument('--import-from-csv', metavar='FILE',
29                            help='Import special phrases from a CSV file.')
30
31     @staticmethod
32     def run(args):
33         if args.import_from_wiki:
34             ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
35
36         if args.import_from_csv:
37             if not Path(args.import_from_csv).is_file():
38                 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
39                 raise UsageError('Cannot access file.')
40
41             ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
42
43         return 0
44
45     @staticmethod
46     def start_import(args, loader):
47         """
48             Create the SPImporter object containing the right
49             sp loader and then start the import of special phrases.
50         """
51         from ..tokenizer import factory as tokenizer_factory
52
53         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
54         with connect(args.config.get_libpq_dsn()) as db_connection:
55             SPImporter(
56                 args.config, args.phplib_dir, db_connection, loader
57             ).import_phrases(tokenizer)