syntax improvements and use real class attributes

This commit is contained in:
Marc Jauvin 2018-02-09 17:14:40 -05:00
parent d2287b7a2e
commit d88a13598a
5 changed files with 49 additions and 32 deletions

View File

@ -99,6 +99,7 @@ class TabData:
Only used for QtWebKit.
pinned: Flag to pin the tab.
fullscreen: Whether the tab has a video shown fullscreen currently.
netrc_used: Whether netrc authentication was performed.
"""
keep_icon = attr.ib(False)
@ -107,6 +108,7 @@ class TabData:
override_target = attr.ib(None)
pinned = attr.ib(False)
fullscreen = attr.ib(False)
netrc_used = attr.ib(False)
class AbstractAction:
@ -762,6 +764,7 @@ class AbstractTab(QWidget):
else:
self._set_load_status(usertypes.LoadStatus.error)
self.load_finished.emit(ok)
if not self.title():
self.title_changed.emit(self.url().toDisplayString())
self.zoom.set_current()

View File

@ -265,26 +265,38 @@ def get_user_stylesheet():
def netrc_authentication(url, authenticator):
"""performs authorization using netrc."""
if 'HOME' in os.environ:
"""Perform authorization using netrc.
Args:
url: The URL the request was done for.
authenticator: QAuthenticator object used to set credentials provided.
Return:
True if netrc found credentials for the URL.
False otherwise.
"""
if 'HOME' not in os.environ:
# We'll get an OSError by netrc if 'HOME' isn't available in
# os.environ. We don't want to log that, so we prevent it
# altogether.
user, password = None, None
try:
net = netrc.netrc(config.val.content.netrc_file)
authenticators = net.authenticators(url.host())
if authenticators is not None:
(user, _account, password) = authenticators
except FileNotFoundError:
log.misc.debug("No .netrc file found")
except OSError:
log.misc.exception("Unable to read the netrc file")
except netrc.NetrcParseError:
log.misc.exception("Error when parsing the netrc file")
return False
user, password = None, None
try:
net = netrc.netrc(config.val.content.netrc_file)
authenticators = net.authenticators(url.host())
if authenticators is not None:
(user, _account, password) = authenticators
except FileNotFoundError:
log.misc.debug("No .netrc file found")
except OSError:
log.misc.exception("Unable to read the netrc file")
except netrc.NetrcParseError:
log.misc.exception("Error when parsing the netrc file")
if user is not None:
authenticator.setUser(user)
authenticator.setPassword(password)
return True
return False
if user is None:
return False
authenticator.setUser(user)
authenticator.setPassword(password)
return True

View File

@ -739,15 +739,15 @@ class WebEngineTab(browsertab.AbstractTab):
@pyqtSlot(QUrl, 'QAuthenticator*')
def _on_authentication_required(self, url, authenticator):
netrc = None
if not hasattr(self, 'netrc_used'):
setattr(self, 'netrc_used', True)
netrc = shared.netrc_authentication(url, authenticator)
if not netrc:
netrc_success = False
if not self.data.netrc_used:
self.data.netrc_used = True
netrc_success = shared.netrc_authentication(url, authenticator)
if not netrc_success:
abort_on = [self.shutting_down, self.load_started]
answer = shared.authentication_required(url, authenticator,
abort_on)
if not netrc and answer is None:
if not netrc_success and answer is None:
try:
# pylint: disable=no-member, useless-suppression
sip.assign(authenticator, QAuthenticator())
@ -783,8 +783,8 @@ class WebEngineTab(browsertab.AbstractTab):
# https://bugreports.qt.io/browse/QTBUG-61506
self.search.clear()
super()._on_load_started()
if hasattr(self, 'netrc_used'):
delattr(self, 'netrc_used')
if self.data.netrc_used:
self.data.netrc_used = False
@pyqtSlot(QWebEnginePage.RenderProcessTerminationStatus, int)
def _on_render_process_terminated(self, status, exitcode):

View File

@ -129,6 +129,7 @@ class NetworkManager(QNetworkAccessManager):
_rejected_ssl_errors: A {QUrl: [SslError]} dict of rejected errors.
_accepted_ssl_errors: A {QUrl: [SslError]} dict of accepted errors.
_private: Whether we're in private browsing mode.
netrc_used: Whether netrc authentication was performed.
Signals:
shutting_down: Emitted when the QNAM is shutting down.
@ -159,6 +160,7 @@ class NetworkManager(QNetworkAccessManager):
self.authenticationRequired.connect(self.on_authentication_required)
self.proxyAuthenticationRequired.connect(
self.on_proxy_authentication_required)
self.netrc_used = False
def _set_cookiejar(self):
"""Set the cookie jar of the NetworkManager correctly."""
@ -269,8 +271,8 @@ class NetworkManager(QNetworkAccessManager):
def on_authentication_required(self, reply, authenticator):
"""Called when a website needs authentication."""
netrc = False
if not hasattr(self, 'netrc_used'):
setattr(self, 'netrc_used', True)
if not self.netrc_used:
self.netrc_used = True
netrc = shared.netrc_authentication(reply.url(), authenticator)
if not netrc:
abort_on = self._get_abort_signals(reply)

View File

@ -707,9 +707,9 @@ class WebKitTab(browsertab.AbstractTab):
@pyqtSlot()
def _on_load_started(self):
super()._on_load_started()
obj = self.networkaccessmanager()
if hasattr(obj, 'netrc_used'):
delattr(obj, 'netrc_used')
nam = self.networkaccessmanager()
if nam.netrc_used:
nam.netrc_used = False
@pyqtSlot()
def _on_frame_load_finished(self):