X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/d1e7b85cc5871a79d808634586d4425466bb2333..2fe8b98d557cf68a15763a762fb77d3a9522af98:/src/nominatim_api/core.py diff --git a/src/nominatim_api/core.py b/src/nominatim_api/core.py index 6c4c37d7..ff0db39f 100644 --- a/src/nominatim_api/core.py +++ b/src/nominatim_api/core.py @@ -7,7 +7,8 @@ """ Implementation of classes for API access via libraries. """ -from typing import Mapping, Optional, Any, AsyncIterator, Dict, Sequence, List, Tuple, cast +from typing import Mapping, Optional, Any, AsyncIterator, Dict, Sequence, List,\ + Union, Tuple, cast import asyncio import sys import contextlib @@ -38,8 +39,10 @@ class NominatimAPIAsync: #pylint: disable=too-many-instance-attributes This class shares most of the functions with its synchronous version. There are some additional functions or parameters, which are documented below. + + This class should usually be used as a context manager in 'with' context. """ - def __init__(self, project_dir: Path, + def __init__(self, project_dir: Optional[Union[str, Path]] = None, environ: Optional[Mapping[str, str]] = None, loop: Optional[asyncio.AbstractEventLoop] = None) -> None: """ Initiate a new frontend object with synchronous API functions. @@ -166,6 +169,14 @@ class NominatimAPIAsync: #pylint: disable=too-many-instance-attributes await self._engine.dispose() + async def __aenter__(self) -> 'NominatimAPIAsync': + return self + + + async def __aexit__(self, *_: Any) -> None: + await self.close() + + @contextlib.asynccontextmanager async def begin(self) -> AsyncIterator[SearchConnection]: """ Create a new connection with automatic transaction handling. @@ -351,9 +362,11 @@ class NominatimAPI: """ This class provides a thin synchronous wrapper around the asynchronous Nominatim functions. It creates its own event loop and runs each synchronous function call to completion using that loop. + + This class should usually be used as a context manager in 'with' context. """ - def __init__(self, project_dir: Path, + def __init__(self, project_dir: Optional[Union[str, Path]] = None, environ: Optional[Mapping[str, str]] = None) -> None: """ Initiate a new frontend object with synchronous API functions. @@ -376,8 +389,17 @@ class NominatimAPI: This function also closes the asynchronous worker loop making the NominatimAPI object unusable. """ - self._loop.run_until_complete(self._async_api.close()) - self._loop.close() + if not self._loop.is_closed(): + self._loop.run_until_complete(self._async_api.close()) + self._loop.close() + + + def __enter__(self) -> 'NominatimAPI': + return self + + + def __exit__(self, *_: Any) -> None: + self.close() @property