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 nominatim_core.errors import UsageError
16 from nominatim_core.config import Configuration
17 from nominatim_core.typing import Protocol
19 LOG = logging.getLogger()
21 class Subcommand(Protocol):
23 Interface to be implemented by classes implementing a CLI subcommand.
26 def add_args(self, parser: argparse.ArgumentParser) -> None:
28 Fill the given parser for the subcommand with the appropriate
32 def run(self, args: 'NominatimArgs') -> int:
34 Run the subcommand with the given parsed arguments.
39 """ Customized namespace class for the nominatim command line tool
40 to receive the command-line arguments.
42 # Basic environment set by root program.
48 subcommand: Optional[str]
52 osm2pgsql_cache: Optional[int]
55 # Arguments added to all subcommands.
57 threads: Optional[int]
59 # Arguments to 'add-data'
64 relation: Optional[int]
65 tiger_data: Optional[str]
68 # Arguments to 'admin'
74 analyse_indexing: bool
77 place_id: Optional[int]
79 # Arguments to 'import'
81 continue_at: Optional[str]
88 prepare_database: bool
90 # Arguments to 'index'
96 # Arguments to 'export'
99 output_all_postcodes: bool
100 language: Optional[str]
101 restrict_to_country: Optional[str]
103 # Arguments to 'convert'
106 # Arguments to 'refresh'
113 secondary_importance: bool
117 enable_debug_statements: bool
118 data_object: Sequence[Tuple[str, int]]
119 data_area: Sequence[Tuple[str, int]]
121 # Arguments to 'replication'
123 update_functions: bool
124 check_for_updates: bool
129 # Arguments to 'serve'
133 # Arguments to 'special-phrases
134 import_from_wiki: bool
135 import_from_csv: Optional[str]
138 # Arguments to all query functions
144 polygon_output: Optional[str]
145 polygon_threshold: Optional[float]
147 # Arguments to 'search'
149 amenity: Optional[str]
150 street: Optional[str]
152 county: Optional[str]
154 country: Optional[str]
155 postalcode: Optional[str]
156 countrycodes: Optional[str]
157 exclude_place_ids: Optional[str]
159 viewbox: Optional[str]
163 # Arguments to 'reverse'
167 layers: Optional[Sequence[str]]
169 # Arguments to 'lookup'
172 # Arguments to 'details'
173 object_class: Optional[str]
177 polygon_geojson: bool
178 group_hierarchy: bool
181 def osm2pgsql_options(self, default_cache: int,
182 default_threads: int) -> Dict[str, Any]:
183 """ Return the standard osm2pgsql options that can be derived
184 from the command line arguments. The resulting dict can be
185 further customized and then used in `run_osm2pgsql()`.
187 return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY or self.config.lib_dir.osm2pgsql,
188 osm2pgsql_cache=self.osm2pgsql_cache or default_cache,
189 osm2pgsql_style=self.config.get_import_style_file(),
190 osm2pgsql_style_path=self.config.config_dir,
191 threads=self.threads or default_threads,
192 dsn=self.config.get_libpq_dsn(),
193 flatnode_file=str(self.config.get_path('FLATNODE_FILE') or ''),
194 tablespaces=dict(slim_data=self.config.TABLESPACE_OSM_DATA,
195 slim_index=self.config.TABLESPACE_OSM_INDEX,
196 main_data=self.config.TABLESPACE_PLACE_DATA,
197 main_index=self.config.TABLESPACE_PLACE_INDEX
202 def get_osm_file_list(self) -> Optional[List[Path]]:
203 """ Return the --osm-file argument as a list of Paths or None
204 if no argument was given. The function also checks if the files
205 exist and raises a UsageError if one cannot be found.
207 if not self.osm_file:
210 files = [Path(f) for f in self.osm_file]
212 if not fname.is_file():
213 LOG.fatal("OSM file '%s' does not exist.", fname)
214 raise UsageError('Cannot access file.')