1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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.errors import UsageError
16 from nominatim.config import Configuration
17 from nominatim.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.
49 subcommand: Optional[str]
53 osm2pgsql_cache: Optional[int]
56 # Arguments added to all subcommands.
58 threads: Optional[int]
60 # Arguments to 'add-data'
65 relation: Optional[int]
66 tiger_data: Optional[str]
69 # Arguments to 'admin'
74 analyse_indexing: bool
77 place_id: Optional[int]
79 # Arguments to 'import'
81 continue_at: Optional[str]
89 # Arguments to 'index'
95 # Arguments to 'export'
98 output_all_postcodes: bool
99 language: Optional[str]
100 restrict_to_country: Optional[str]
101 restrict_to_osm_node: Optional[int]
102 restrict_to_osm_way: Optional[int]
103 restrict_to_osm_relation: Optional[int]
105 # Arguments to 'refresh'
112 secondary_importance: bool
116 enable_debug_statements: bool
117 data_object: Sequence[Tuple[str, int]]
118 data_area: Sequence[Tuple[str, int]]
120 # Arguments to 'replication'
122 update_functions: bool
123 check_for_updates: bool
128 # Arguments to 'serve'
131 # Arguments to 'special-phrases
132 import_from_wiki: bool
133 import_from_csv: Optional[str]
136 # Arguments to all query functions
142 polygon_output: Optional[str]
143 polygon_threshold: Optional[float]
145 # Arguments to 'search'
147 street: Optional[str]
149 county: Optional[str]
151 country: Optional[str]
152 postalcode: Optional[str]
153 countrycodes: Optional[str]
154 exclude_place_ids: Optional[str]
156 viewbox: Optional[str]
160 # Arguments to 'reverse'
165 # Arguments to 'lookup'
168 # Arguments to 'details'
169 object_class: Optional[str]
172 def osm2pgsql_options(self, default_cache: int,
173 default_threads: int) -> Dict[str, Any]:
174 """ Return the standard osm2pgsql options that can be derived
175 from the command line arguments. The resulting dict can be
176 further customized and then used in `run_osm2pgsql()`.
178 return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY or self.config.lib_dir.osm2pgsql,
179 osm2pgsql_cache=self.osm2pgsql_cache or default_cache,
180 osm2pgsql_style=self.config.get_import_style_file(),
181 osm2pgsql_style_path=self.config.config_dir,
182 threads=self.threads or default_threads,
183 dsn=self.config.get_libpq_dsn(),
184 forward_dependencies=self.config.get_bool('UPDATE_FORWARD_DEPENDENCIES'),
185 flatnode_file=str(self.config.get_path('FLATNODE_FILE') or ''),
186 tablespaces=dict(slim_data=self.config.TABLESPACE_OSM_DATA,
187 slim_index=self.config.TABLESPACE_OSM_INDEX,
188 main_data=self.config.TABLESPACE_PLACE_DATA,
189 main_index=self.config.TABLESPACE_PLACE_INDEX
194 def get_osm_file_list(self) -> Optional[List[Path]]:
195 """ Return the --osm-file argument as a list of Paths or None
196 if no argument was given. The function also checks if the files
197 exist and raises a UsageError if one cannot be found.
199 if not self.osm_file:
202 files = [Path(f) for f in self.osm_file]
204 if not fname.is_file():
205 LOG.fatal("OSM file '%s' does not exist.", fname)
206 raise UsageError('Cannot access file.')