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.')
 
  30         group.add_argument('--no-replace', action='store_true',
 
  31                            help='Keep the old phrases and only add the new ones.')
 
  35         if args.import_from_wiki:
 
  36             ImportSpecialPhrases.start_import(args, SPWikiLoader(args.config))
 
  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.')
 
  43             ImportSpecialPhrases.start_import(args, SPCsvLoader(args.import_from_csv))
 
  48     def start_import(args, loader):
 
  50             Create the SPImporter object containing the right
 
  51             sp loader and then start the import of special phrases.
 
  53         from ..tokenizer import factory as tokenizer_factory
 
  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:
 
  59                 args.config, args.phplib_dir, db_connection, loader
 
  60             ).import_phrases(tokenizer, should_replace)