1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the 'convert' subcommand.
10 from typing import Set, Any, Union, Optional, Sequence
13 from pathlib import Path
15 from nominatim.clicmd.args import NominatimArgs
16 from nominatim.errors import UsageError
18 # Do not repeat documentation of subcommand classes.
19 # pylint: disable=C0111
20 # Using non-top-level imports to avoid eventually unused imports.
21 # pylint: disable=E0012,C0415
23 class WithAction(argparse.Action):
24 """ Special action that saves a list of flags, given on the command-line
25 as `--with-foo` or `--without-foo`.
27 def __init__(self, option_strings: Sequence[str], dest: Any,
28 default: bool = True, **kwargs: Any) -> None:
30 raise ValueError("nargs not allowed.")
31 if option_strings is None:
32 raise ValueError("Positional parameter not allowed.")
34 self.dest_set = kwargs.pop('dest_set')
35 full_option_strings = []
36 for opt in option_strings:
37 if not opt.startswith('--'):
38 raise ValueError("short-form options not allowed")
40 self.dest_set.add(opt[2:])
41 full_option_strings.append(f"--with-{opt[2:]}")
42 full_option_strings.append(f"--without-{opt[2:]}")
44 super().__init__(full_option_strings, argparse.SUPPRESS, nargs=0, **kwargs)
47 def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace,
48 values: Union[str, Sequence[Any], None],
49 option_string: Optional[str] = None) -> None:
51 if option_string.startswith('--with-'):
52 self.dest_set.add(option_string[7:])
53 if option_string.startswith('--without-'):
54 self.dest_set.discard(option_string[10:])
58 """ Convert an existing database into a different format. (EXPERIMENTAL)
60 Dump a read-only version of the database in a different format.
61 At the moment only a SQLite database suitable for reverse lookup
65 def __init__(self) -> None:
66 self.options: Set[str] = set()
68 def add_args(self, parser: argparse.ArgumentParser) -> None:
69 parser.add_argument('--format', default='sqlite',
71 help='Format of the output database (must be sqlite currently)')
72 parser.add_argument('--output', '-o', required=True, type=Path,
73 help='File to write the database to.')
74 group = parser.add_argument_group('Switches to define database layout'
75 '(currently no effect)')
76 group.add_argument('--reverse', action=WithAction, dest_set=self.options, default=True,
77 help='Enable/disable support for reverse and lookup API'
78 ' (default: enabled)')
79 group.add_argument('--search', action=WithAction, dest_set=self.options, default=True,
80 help='Enable/disable support for search API (default: disabled)')
81 group.add_argument('--details', action=WithAction, dest_set=self.options, default=True,
82 help='Enable/disable support for details API (default: enabled)')
85 def run(self, args: NominatimArgs) -> int:
86 if args.output.exists():
87 raise UsageError(f"File '{args.output}' already exists. Refusing to overwrite.")
89 if args.format == 'sqlite':
90 from ..tools import convert_sqlite
92 asyncio.run(convert_sqlite.convert(args.project_dir, args.output, self.options))