Move NetworkManager to own file

This commit is contained in:
Florian Bruhin 2014-02-20 22:23:38 +01:00
parent 7f25e1e3f9
commit 65ae1882f0
3 changed files with 61 additions and 40 deletions

View File

@ -0,0 +1 @@
"""Modules related to network operations."""

View File

@ -0,0 +1,58 @@
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Our own QNetworkAccessManager."""
from PyQt5.QtNetwork import QNetworkAccessManager
class NetworkManager(QNetworkAccessManager):
"""Our own QNetworkAccessManager.
Attributes:
_requests: Pending requests.
"""
def __init__(self, parent=None):
self._requests = {}
super().__init__(parent)
def abort_requests(self):
"""Abort all running requests."""
for request in self._requests.values():
request.abort()
def createRequest(self, op, req, outgoing_data):
"""Return a new QNetworkReply object.
Extend QNetworkAccessManager::createRequest to save requests in
self._requests.
Args:
op: Operation op
req: const QNetworkRequest & req
outgoing_data: QIODevice * outgoingData
Return:
A QNetworkReply.
"""
reply = super().createRequest(op, req, outgoing_data)
self._requests[id(reply)] = reply
reply.destroyed.connect(lambda obj: self._requests.pop(id(obj)))
return reply

View File

@ -30,7 +30,7 @@ from PyQt5.QtWidgets import QApplication, QShortcut, QSizePolicy
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QEvent
from PyQt5.QtGui import QClipboard
from PyQt5.QtPrintSupport import QPrintPreviewDialog
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply
from PyQt5.QtNetwork import QNetworkReply
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
@ -38,6 +38,7 @@ import qutebrowser.utils.about as about
import qutebrowser.utils.url as urlutils
import qutebrowser.utils.config as config
from qutebrowser.widgets.tabbar import TabWidget
from qutebrowser.network.networkmanager import NetworkManager
from qutebrowser.utils.signals import SignalCache, dbg_signal
from qutebrowser.utils.misc import read_file
from qutebrowser.utils.types import NeighborList
@ -940,42 +941,3 @@ class BrowserPage(QWebPage):
except KeyError:
return super().extension(ext, opt, out)
return handler(opt, out)
class NetworkManager(QNetworkAccessManager):
"""Our own QNetworkAccessManager.
Attributes:
_requests: Pending requests.
"""
def __init__(self, parent=None):
self._requests = {}
super().__init__(parent)
def abort_requests(self):
"""Abort all running requests."""
for request in self._requests.values():
request.abort()
def createRequest(self, op, req, outgoing_data):
"""Return a new QNetworkReply object.
Extend QNetworkAccessManager::createRequest to save requests in
self._requests.
Args:
op: Operation op
req: const QNetworkRequest & req
outgoing_data: QIODevice * outgoingData
Return:
A QNetworkReply.
"""
reply = super().createRequest(op, req, outgoing_data)
self._requests[id(reply)] = reply
reply.destroyed.connect(lambda obj: self._requests.pop(id(obj)))
return reply