+
+
+def _make_replication_server(url: str, timeout: int) -> ContextManager[ReplicationServer]:
+ """ Returns a ReplicationServer in form of a context manager.
+
+ Creates a light wrapper around older versions of pyosmium that did
+ not support the context manager interface.
+ """
+ if hasattr(ReplicationServer, '__enter__'):
+ # Patches the open_url function for pyosmium >= 3.2
+ # where the socket timeout is no longer respected.
+ def patched_open_url(self: ReplicationServer, url: urlrequest.Request) -> Any:
+ """ Download a resource from the given URL and return a byte sequence
+ of the content.
+ """
+ get_params = {
+ 'headers': {"User-Agent" : f"Nominatim (pyosmium/{pyo_version.pyosmium_release})"},
+ 'timeout': timeout or None,
+ 'stream': True
+ }
+
+ if self.session is not None:
+ return self.session.get(url.get_full_url(), **get_params)
+
+ @contextmanager
+ def _get_url_with_session() -> Iterator[requests.Response]:
+ with requests.Session() as session:
+ request = session.get(url.get_full_url(), **get_params) # type: ignore
+ yield request
+
+ return _get_url_with_session()
+
+ repl = ReplicationServer(url)
+ repl.open_url = types.MethodType(patched_open_url, repl)
+
+ return cast(ContextManager[ReplicationServer], repl)
+
+ @contextmanager
+ def get_cm() -> Generator[ReplicationServer, None, None]:
+ yield ReplicationServer(url)
+
+ return get_cm()