"""
Nominatim configuration accessor.
"""
-from typing import Dict, Any, List, Mapping, Optional, Union
+from typing import Dict, Any, List, Mapping, Optional
import logging
import os
from pathlib import Path
from dotenv import dotenv_values
+from nominatim.typing import StrPath
from nominatim.errors import UsageError
-PathOrStr = Union[str, os.PathLike[str]]
-
LOG = logging.getLogger()
CONFIG_CACHE : Dict[str, Any] = {}
self.lib_dir = _LibDirs()
- def set_libdirs(self, **kwargs: PathOrStr) -> None:
+ def set_libdirs(self, **kwargs: StrPath) -> None:
""" Set paths to library functions and data.
"""
for key, value in kwargs.items():
return env
- def load_sub_configuration(self, filename: PathOrStr,
+ def load_sub_configuration(self, filename: StrPath,
config: Optional[str] = None) -> Any:
""" Load additional configuration from a file. `filename` is the name
of the configuration file. The file is first searched in the
return result
- def find_config_file(self, filename: PathOrStr,
+ def find_config_file(self, filename: StrPath,
config: Optional[str] = None) -> Path:
""" Resolve the location of a configuration file given a filename and
an optional configuration option with the file name.
"""
Specialised connection and cursor functions.
"""
-from typing import Union, List, Optional, Any, Callable, ContextManager, Mapping, cast, TypeVar, overload, Tuple, Sequence
+from typing import List, Optional, Any, Callable, ContextManager, Mapping, cast, overload, Tuple
import contextlib
import logging
import os
import psycopg2.extras
from psycopg2 import sql as pysql
+from nominatim.typing import Query, T_cursor
from nominatim.errors import UsageError
-Query = Union[str, bytes, pysql.Composable]
-T = TypeVar('T', bound=psycopg2.extensions.cursor)
-
LOG = logging.getLogger()
class _Cursor(psycopg2.extras.DictCursor):
super().execute(query, args)
- def execute_values(self, sql: Query, argslist: List[Any], template: Optional[str] = None) -> None:
+ def execute_values(self, sql: Query, argslist: List[Any],
+ template: Optional[str] = None) -> None:
""" Wrapper for the psycopg2 convenience function to execute
SQL for a list of values.
"""
...
@overload
- def cursor(self, cursor_factory: Callable[..., T]) -> T:
+ def cursor(self, cursor_factory: Callable[..., T_cursor]) -> T_cursor:
...
def cursor(self, cursor_factory = _Cursor, **kwargs): # type: ignore
--- /dev/null
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# This file is part of Nominatim. (https://nominatim.org)
+#
+# Copyright (C) 2022 by the Nominatim developer community.
+# For a full list of authors see the git log.
+"""
+Type definitions for typing annotations.
+
+Complex type definitions are moved here, to keep the source files readable.
+"""
+from typing import Union, TypeVar, TYPE_CHECKING
+
+# Generics varaible names do not confirm to naming styles, ignore globally here.
+# pylint: disable=invalid-name
+
+if TYPE_CHECKING:
+ import psycopg2.sql
+ import psycopg2.extensions
+ import os
+
+StrPath = Union[str, 'os.PathLike[str]']
+
+# psycopg2-related types
+
+Query = Union[str, bytes, 'psycopg2.sql.Composable']
+T_cursor = TypeVar('T_cursor', bound='psycopg2.extensions.cursor')