]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/special_phrases.py
Fix old paths for `phpcs` when using `make test`
[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         group.add_argument('--no-replace', action='store_true',
31                            help='Keep the old phrases and only add the new ones.')
32
33     @staticmethod
34     def run(args):
35         if args.import_from_wiki:
36             ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
37
38         if args.import_from_csv:
39             if not Path(args.import_from_csv).is_file():
40                 LOG.fatal("CSV file '%s' does not exist.", args.import_from_csv)
41                 raise UsageError('Cannot access file.')
42
43             ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
44
45         return 0
46
47     @staticmethod
48     def start_import(args, loader):
49         """
50             Create the SPImporter object containing the right
51             sp loader and then start the import of special phrases.
52         """
53         from ..tokenizer import factory as tokenizer_factory
54
55         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
56         should_replace = not args.no_replace
57         with connect(args.config.get_libpq_dsn()) as db_connection:
58             SPImporter(
59                 args.config, args.phplib_dir, db_connection, loader
60             ).import_phrases(tokenizer, should_replace)