2 Implementation of the 'special-phrases' command.
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
12 LOG = logging.getLogger()
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
19 class ImportSpecialPhrases:
21 Import special phrases.
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.')
33 if args.import_from_wiki:
34 ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
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.')
41 ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
46 def start_import(args, loader):
48 Create the SPImporter object containing the right
49 sp loader and then start the import of special phrases.
51 from ..tokenizer import factory as tokenizer_factory
53 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
54 with connect(args.config.get_libpq_dsn()) as db_connection:
56 args.config, args.phplib_dir, db_connection, loader
57 ).import_phrases(tokenizer)