def _fetch_inventory(self, url): cache_dir = config.cache_dir() cache_dir = os.path.join(cache_dir, "help", type(self).__qualname__) try: os.makedirs(cache_dir) except OSError: pass url = QUrl(self.inventory) if not url.isLocalFile(): # fetch and cache the inventory file. manager = QNetworkAccessManager(self) cache = QNetworkDiskCache() cache.setCacheDirectory(cache_dir) manager.setCache(cache) req = QNetworkRequest(url) # Follow redirects (for example http -> https) # If redirects were not followed, the documentation would not be found try: req.setAttribute(QNetworkRequest.FollowRedirectsAttribute, 1) # from Qt 5.6 req.setAttribute(QNetworkRequest.RedirectPolicyAttribute, # from Qt 5.9 QNetworkRequest.NoLessSafeRedirectPolicy) except AttributeError: # if ran with earlier Qt pass self._reply = manager.get(req) manager.finished.connect(self._on_finished) else: with open(url.toLocalFile(), "rb") as f: self._load_inventory(f)
def get(self, tile): future = Future() url = QUrl(tile.url) request = QNetworkRequest(url) # Modified by Jean 2020/05/21 to support tianditu.gov.cn # request.setRawHeader(b"User-Agent", b"OWMap/1.0") request.setRawHeader(b"User-Agent", b"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0") request.setAttribute( QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache ) request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True) request.setMaximumRedirectsAllowed(5) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply future._tile = tile @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() def on_reply_ready(reply, future): # type: (QNetworkReply, Future) -> None # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None with closing(reply): if not future.set_running_or_notify_cancel(): return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard http and # urllib exceptions. future.set_exception(Exception(reply.errorString())) return try: image = Image.open(BytesIO(reply.readAll())) except Exception as e: future.set_exception(e) else: tile.disc_cache = reply.attribute( QNetworkRequest.SourceIsFromCacheAttribute) future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def on_reply_ready(reply, future): # type: (QNetworkReply, Future) -> None nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if _log.level <= logging.DEBUG: s = io.StringIO() print("\n", reply.url(), file=s) if reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute): print(" (served from cache)", file=s) for name, val in reply.rawHeaderPairs(): print(bytes(name).decode("latin-1"), ":", bytes(val).decode("latin-1"), file=s) _log.debug(s.getvalue()) if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image)
def _fetch_inventory(self, url: QUrl) -> None: if not url.isLocalFile(): # fetch and cache the inventory file. self._manager = manager = self._networkAccessManagerInstance() req = QNetworkRequest(url) req.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache) req.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True) req.setAttribute(QNetworkRequest.RedirectPolicyAttribute, QNetworkRequest.NoLessSafeRedirectPolicy) req.setMaximumRedirectsAllowed(5) self._reply = manager.get(req) self._reply.finished.connect(self._on_finished) else: with open(url.toLocalFile(), "rb") as f: self._load_inventory(f)
def on_reply_ready(reply, future): nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image)
def _fetch_inventory(self, url): cache_dir = config.cache_dir() cache_dir = os.path.join(cache_dir, "help", type(self).__qualname__) try: os.makedirs(cache_dir, exist_ok=True) except OSError: pass url = QUrl(self.inventory) if not url.isLocalFile(): # fetch and cache the inventory file. manager = QNetworkAccessManager(self) cache = QNetworkDiskCache() cache.setCacheDirectory(cache_dir) manager.setCache(cache) req = QNetworkRequest(url) # Follow redirects (for example http -> https) # If redirects were not followed, the documentation would not be found try: req.setAttribute(QNetworkRequest.FollowRedirectsAttribute, 1) # from Qt 5.6 req.setAttribute( QNetworkRequest.RedirectPolicyAttribute, # from Qt 5.9 QNetworkRequest.NoLessSafeRedirectPolicy) except AttributeError: # if ran with earlier Qt pass self._reply = manager.get(req) manager.finished.connect(self._on_finished) else: with open(url.toLocalFile(), "rb") as f: self._load_inventory(f)
def _fetch_inventory(self, url): cache_dir = config.cache_dir() cache_dir = os.path.join(cache_dir, "help", type(self).__qualname__) try: os.makedirs(cache_dir) except OSError: pass url = QUrl(self.inventory) if not url.isLocalFile(): # fetch and cache the inventory file. manager = QNetworkAccessManager(self) cache = QNetworkDiskCache() cache.setCacheDirectory(cache_dir) manager.setCache(cache) req = QNetworkRequest(url) self._reply = manager.get(req) manager.finished.connect(self._on_finished) else: self._load_inventory(open(str(url.toLocalFile()), "rb"))
def get(self, url): future = Future() url = QUrl(url) request = QNetworkRequest(url) request.setRawHeader(b"User-Agent", b"OWImageViewer/1.0") request.setAttribute( QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache ) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() n_redir = 0 def on_reply_ready(reply, future): nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def get(self, url): future = Future() url = QUrl(url) request = QNetworkRequest(url) request.setRawHeader(b"User-Agent", b"OWImageViewer/1.0") request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() n_redir = 0 def on_reply_ready(reply, future): nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def get(self, url): future = Future() url = QUrl(url) request = QNetworkRequest(url) request.setRawHeader(b"User-Agent", b"OWImageViewer/1.0") request.setAttribute( QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache ) if QT_VERSION >= 0x50600: request.setAttribute( QNetworkRequest.FollowRedirectsAttribute, True ) request.setMaximumRedirectsAllowed(5) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() n_redir = 0 def on_reply_ready(reply, future): # type: (QNetworkReply, Future) -> None nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if _log.level <= logging.DEBUG: s = io.StringIO() print("\n", reply.url(), file=s) if reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute): print(" (served from cache)", file=s) for name, val in reply.rawHeaderPairs(): print(bytes(name).decode("latin-1"), ":", bytes(val).decode("latin-1"), file=s) _log.debug(s.getvalue()) if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def get(self, tile): future = Future() url = QUrl(tile.url) request = QNetworkRequest(url) # Modified by Jean 2020/05/21 to support tianditu.gov.cn # request.setRawHeader(b"User-Agent", b"OWMap/1.0") # request.setRawHeader(b"User-Agent", b"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0") request.setRawHeader( b'User-Agent', b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36' ) # Updated by Jean @ 2022/1/6 request.setRawHeader( b'Accept', b'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8' ) request.setRawHeader(b'Accept-Encoding', b'gzip, deflate, br') request.setRawHeader(b'Accept-Language', b'zh-CN,zh;q=0.9') request.setRawHeader( b'sec-ch-ua', b'" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"' ) request.setRawHeader(b'sec-ch-ua-mobile', b'?0') request.setRawHeader(b'Upgrade-Insecure-Requests', b'1') request.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache) request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True) request.setMaximumRedirectsAllowed(5) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply future._tile = tile @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() def on_reply_ready(reply, future): # type: (QNetworkReply, Future) -> None # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None with closing(reply): if not future.set_running_or_notify_cancel(): return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard http and # urllib exceptions. future.set_exception(Exception(reply.errorString())) return try: image = Image.open(BytesIO(reply.readAll())) except Exception as e: future.set_exception(e) else: tile.disc_cache = reply.attribute( QNetworkRequest.SourceIsFromCacheAttribute) future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future