1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Provides custom functions over command-line arguments.
 
  10 from typing import Optional, List, Dict, Any, Sequence, Tuple
 
  13 from pathlib import Path
 
  15 from ..errors import UsageError
 
  16 from ..config import Configuration
 
  17 from ..typing import Protocol
 
  20 LOG = logging.getLogger()
 
  23 class Subcommand(Protocol):
 
  25     Interface to be implemented by classes implementing a CLI subcommand.
 
  28     def add_args(self, parser: argparse.ArgumentParser) -> None:
 
  30         Fill the given parser for the subcommand with the appropriate
 
  34     def run(self, args: 'NominatimArgs') -> int:
 
  36         Run the subcommand with the given parsed arguments.
 
  41     """ Customized namespace class for the nominatim command line tool
 
  42         to receive the command-line arguments.
 
  44     # Basic environment set by root program.
 
  50     subcommand: Optional[str]
 
  54     osm2pgsql_cache: Optional[int]
 
  57     # Arguments added to all subcommands.
 
  59     threads: Optional[int]
 
  61     # Arguments to 'add-data'
 
  66     relation: Optional[int]
 
  67     tiger_data: Optional[str]
 
  70     # Arguments to 'admin'
 
  76     analyse_indexing: bool
 
  79     place_id: Optional[int]
 
  81     # Arguments to 'import'
 
  83     continue_at: Optional[str]
 
  90     prepare_database: bool
 
  92     # Arguments to 'index'
 
  98     # Arguments to 'export'
 
 101     output_all_postcodes: bool
 
 102     language: Optional[str]
 
 103     restrict_to_country: Optional[str]
 
 105     # Arguments to 'convert'
 
 108     # Arguments to 'refresh'
 
 115     secondary_importance: bool
 
 119     enable_debug_statements: bool
 
 120     data_object: Sequence[Tuple[str, int]]
 
 121     data_area: Sequence[Tuple[str, int]]
 
 123     # Arguments to 'replication'
 
 125     update_functions: bool
 
 126     check_for_updates: bool
 
 131     # Arguments to 'serve'
 
 135     # Arguments to 'special-phrases
 
 136     import_from_wiki: bool
 
 137     import_from_csv: Optional[str]
 
 140     # Arguments to all query functions
 
 147     polygon_output: Optional[str]
 
 148     polygon_threshold: Optional[float]
 
 150     # Arguments to 'search'
 
 152     amenity: Optional[str]
 
 153     street: Optional[str]
 
 155     county: Optional[str]
 
 157     country: Optional[str]
 
 158     postalcode: Optional[str]
 
 159     countrycodes: Optional[str]
 
 160     exclude_place_ids: Optional[str]
 
 162     viewbox: Optional[str]
 
 166     # Arguments to 'reverse'
 
 170     layers: Optional[Sequence[str]]
 
 172     # Arguments to 'lookup'
 
 175     # Arguments to 'details'
 
 176     object_class: Optional[str]
 
 180     polygon_geojson: bool
 
 181     group_hierarchy: bool
 
 183     def osm2pgsql_options(self, default_cache: int,
 
 184                           default_threads: int) -> Dict[str, Any]:
 
 185         """ Return the standard osm2pgsql options that can be derived
 
 186             from the command line arguments. The resulting dict can be
 
 187             further customized and then used in `run_osm2pgsql()`.
 
 189         return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY or self.config.lib_dir.osm2pgsql,
 
 190                     osm2pgsql_cache=self.osm2pgsql_cache or default_cache,
 
 191                     osm2pgsql_style=self.config.get_import_style_file(),
 
 192                     osm2pgsql_style_path=self.config.lib_dir.lua,
 
 193                     threads=self.threads or default_threads,
 
 194                     dsn=self.config.get_libpq_dsn(),
 
 195                     flatnode_file=str(self.config.get_path('FLATNODE_FILE') or ''),
 
 196                     tablespaces=dict(slim_data=self.config.TABLESPACE_OSM_DATA,
 
 197                                      slim_index=self.config.TABLESPACE_OSM_INDEX,
 
 198                                      main_data=self.config.TABLESPACE_PLACE_DATA,
 
 199                                      main_index=self.config.TABLESPACE_PLACE_INDEX
 
 203     def get_osm_file_list(self) -> Optional[List[Path]]:
 
 204         """ Return the --osm-file argument as a list of Paths or None
 
 205             if no argument was given. The function also checks if the files
 
 206             exist and raises a UsageError if one cannot be found.
 
 208         if not self.osm_file:
 
 211         files = [Path(f) for f in self.osm_file]
 
 213             if not fname.is_file():
 
 214                 LOG.fatal("OSM file '%s' does not exist.", fname)
 
 215                 raise UsageError('Cannot access file.')