]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_db/clicmd/args.py
Store entrance fields as columns on table
[nominatim.git] / src / nominatim_db / clicmd / args.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Provides custom functions over command-line arguments.
9 """
10 from typing import Optional, List, Dict, Any, Sequence, Tuple
11 import argparse
12 import logging
13 from pathlib import Path
14
15 from ..errors import UsageError
16 from ..config import Configuration
17 from ..typing import Protocol
18
19
20 LOG = logging.getLogger()
21
22
23 class Subcommand(Protocol):
24     """
25     Interface to be implemented by classes implementing a CLI subcommand.
26     """
27
28     def add_args(self, parser: argparse.ArgumentParser) -> None:
29         """
30         Fill the given parser for the subcommand with the appropriate
31         parameters.
32         """
33
34     def run(self, args: 'NominatimArgs') -> int:
35         """
36         Run the subcommand with the given parsed arguments.
37         """
38
39
40 class NominatimArgs:
41     """ Customized namespace class for the nominatim command line tool
42         to receive the command-line arguments.
43     """
44     # Basic environment set by root program.
45     config: Configuration
46     project_dir: Path
47
48     # Global switches
49     version: bool
50     subcommand: Optional[str]
51     command: Subcommand
52
53     # Shared parameters
54     osm2pgsql_cache: Optional[int]
55     socket_timeout: int
56
57     # Arguments added to all subcommands.
58     verbose: int
59     threads: Optional[int]
60
61     # Arguments to 'add-data'
62     file: Optional[str]
63     diff: Optional[str]
64     node: Optional[int]
65     way: Optional[int]
66     relation: Optional[int]
67     tiger_data: Optional[str]
68     use_main_api: bool
69
70     # Arguments to 'admin'
71     warm: bool
72     check_database: bool
73     migrate: bool
74     collect_os_info: bool
75     clean_deleted: str
76     analyse_indexing: bool
77     target: Optional[str]
78     osm_id: Optional[str]
79     place_id: Optional[int]
80
81     # Arguments to 'import'
82     osm_file: List[str]
83     continue_at: Optional[str]
84     reverse_only: bool
85     no_partitions: bool
86     no_updates: bool
87     offline: bool
88     ignore_errors: bool
89     index_noanalyse: bool
90     prepare_database: bool
91
92     # Arguments to 'index'
93     boundaries_only: bool
94     no_boundaries: bool
95     minrank: int
96     maxrank: int
97
98     # Arguments to 'export'
99     output_type: str
100     output_format: str
101     output_all_postcodes: bool
102     language: Optional[str]
103     restrict_to_country: Optional[str]
104
105     # Arguments to 'convert'
106     output: Path
107
108     # Arguments to 'refresh'
109     postcodes: bool
110     word_tokens: bool
111     word_counts: bool
112     address_levels: bool
113     functions: bool
114     wiki_data: bool
115     secondary_importance: bool
116     importance: bool
117     website: bool
118     diffs: bool
119     enable_debug_statements: bool
120     data_object: Sequence[Tuple[str, int]]
121     data_area: Sequence[Tuple[str, int]]
122
123     # Arguments to 'replication'
124     init: bool
125     update_functions: bool
126     check_for_updates: bool
127     once: bool
128     catch_up: bool
129     do_index: bool
130
131     # Arguments to 'serve'
132     server: str
133     engine: str
134
135     # Arguments to 'special-phrases
136     import_from_wiki: bool
137     import_from_csv: Optional[str]
138     no_replace: bool
139     min: int
140
141     # Arguments to all query functions
142     format: str
143     list_formats: bool
144     addressdetails: bool
145     entrances: bool
146     extratags: bool
147     namedetails: bool
148     lang: Optional[str]
149     polygon_output: Optional[str]
150     polygon_threshold: Optional[float]
151
152     # Arguments to 'search'
153     query: Optional[str]
154     amenity: Optional[str]
155     street: Optional[str]
156     city: Optional[str]
157     county: Optional[str]
158     state: Optional[str]
159     country: Optional[str]
160     postalcode: Optional[str]
161     countrycodes: Optional[str]
162     exclude_place_ids: Optional[str]
163     limit: int
164     viewbox: Optional[str]
165     bounded: bool
166     dedupe: bool
167
168     # Arguments to 'reverse'
169     lat: float
170     lon: float
171     zoom: Optional[int]
172     layers: Optional[Sequence[str]]
173
174     # Arguments to 'lookup'
175     ids: Sequence[str]
176
177     # Arguments to 'details'
178     object_class: Optional[str]
179     linkedplaces: bool
180     hierarchy: bool
181     keywords: bool
182     polygon_geojson: bool
183     group_hierarchy: bool
184
185     def osm2pgsql_options(self, default_cache: int,
186                           default_threads: int) -> Dict[str, Any]:
187         """ Return the standard osm2pgsql options that can be derived
188             from the command line arguments. The resulting dict can be
189             further customized and then used in `run_osm2pgsql()`.
190         """
191         return dict(osm2pgsql=self.config.OSM2PGSQL_BINARY,
192                     osm2pgsql_cache=self.osm2pgsql_cache or default_cache,
193                     osm2pgsql_style=self.config.get_import_style_file(),
194                     osm2pgsql_style_path=self.config.lib_dir.lua,
195                     threads=self.threads or default_threads,
196                     dsn=self.config.get_libpq_dsn(),
197                     flatnode_file=str(self.config.get_path('FLATNODE_FILE') or ''),
198                     tablespaces=dict(slim_data=self.config.TABLESPACE_OSM_DATA,
199                                      slim_index=self.config.TABLESPACE_OSM_INDEX,
200                                      main_data=self.config.TABLESPACE_PLACE_DATA,
201                                      main_index=self.config.TABLESPACE_PLACE_INDEX
202                                      )
203                     )
204
205     def get_osm_file_list(self) -> Optional[List[Path]]:
206         """ Return the --osm-file argument as a list of Paths or None
207             if no argument was given. The function also checks if the files
208             exist and raises a UsageError if one cannot be found.
209         """
210         if not self.osm_file:
211             return None
212
213         files = [Path(f) for f in self.osm_file]
214         for fname in files:
215             if not fname.is_file():
216                 LOG.fatal("OSM file '%s' does not exist.", fname)
217                 raise UsageError('Cannot access file.')
218
219         return files