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

View File

@ -265,11 +265,21 @@ def get_user_stylesheet():
def netrc_authentication(url, authenticator): def netrc_authentication(url, authenticator):
"""performs authorization using netrc.""" """Perform authorization using netrc.
if 'HOME' in os.environ:
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 # 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 # os.environ. We don't want to log that, so we prevent it
# altogether. # altogether.
return False
user, password = None, None user, password = None, None
try: try:
net = netrc.netrc(config.val.content.netrc_file) net = netrc.netrc(config.val.content.netrc_file)
@ -283,8 +293,10 @@ def netrc_authentication(url, authenticator):
except netrc.NetrcParseError: except netrc.NetrcParseError:
log.misc.exception("Error when parsing the netrc file") log.misc.exception("Error when parsing the netrc file")
if user is not None: if user is None:
return False
authenticator.setUser(user) authenticator.setUser(user)
authenticator.setPassword(password) authenticator.setPassword(password)
return True return True
return False

View File

@ -739,15 +739,15 @@ class WebEngineTab(browsertab.AbstractTab):
@pyqtSlot(QUrl, 'QAuthenticator*') @pyqtSlot(QUrl, 'QAuthenticator*')
def _on_authentication_required(self, url, authenticator): def _on_authentication_required(self, url, authenticator):
netrc = None netrc_success = False
if not hasattr(self, 'netrc_used'): if not self.data.netrc_used:
setattr(self, 'netrc_used', True) self.data.netrc_used = True
netrc = shared.netrc_authentication(url, authenticator) netrc_success = shared.netrc_authentication(url, authenticator)
if not netrc: if not netrc_success:
abort_on = [self.shutting_down, self.load_started] abort_on = [self.shutting_down, self.load_started]
answer = shared.authentication_required(url, authenticator, answer = shared.authentication_required(url, authenticator,
abort_on) abort_on)
if not netrc and answer is None: if not netrc_success and answer is None:
try: try:
# pylint: disable=no-member, useless-suppression # pylint: disable=no-member, useless-suppression
sip.assign(authenticator, QAuthenticator()) sip.assign(authenticator, QAuthenticator())
@ -783,8 +783,8 @@ class WebEngineTab(browsertab.AbstractTab):
# https://bugreports.qt.io/browse/QTBUG-61506 # https://bugreports.qt.io/browse/QTBUG-61506
self.search.clear() self.search.clear()
super()._on_load_started() super()._on_load_started()
if hasattr(self, 'netrc_used'): if self.data.netrc_used:
delattr(self, 'netrc_used') self.data.netrc_used = False
@pyqtSlot(QWebEnginePage.RenderProcessTerminationStatus, int) @pyqtSlot(QWebEnginePage.RenderProcessTerminationStatus, int)
def _on_render_process_terminated(self, status, exitcode): 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. _rejected_ssl_errors: A {QUrl: [SslError]} dict of rejected errors.
_accepted_ssl_errors: A {QUrl: [SslError]} dict of accepted errors. _accepted_ssl_errors: A {QUrl: [SslError]} dict of accepted errors.
_private: Whether we're in private browsing mode. _private: Whether we're in private browsing mode.
netrc_used: Whether netrc authentication was performed.
Signals: Signals:
shutting_down: Emitted when the QNAM is shutting down. shutting_down: Emitted when the QNAM is shutting down.
@ -159,6 +160,7 @@ class NetworkManager(QNetworkAccessManager):
self.authenticationRequired.connect(self.on_authentication_required) self.authenticationRequired.connect(self.on_authentication_required)
self.proxyAuthenticationRequired.connect( self.proxyAuthenticationRequired.connect(
self.on_proxy_authentication_required) self.on_proxy_authentication_required)
self.netrc_used = False
def _set_cookiejar(self): def _set_cookiejar(self):
"""Set the cookie jar of the NetworkManager correctly.""" """Set the cookie jar of the NetworkManager correctly."""
@ -269,8 +271,8 @@ class NetworkManager(QNetworkAccessManager):
def on_authentication_required(self, reply, authenticator): def on_authentication_required(self, reply, authenticator):
"""Called when a website needs authentication.""" """Called when a website needs authentication."""
netrc = False netrc = False
if not hasattr(self, 'netrc_used'): if not self.netrc_used:
setattr(self, 'netrc_used', True) self.netrc_used = True
netrc = shared.netrc_authentication(reply.url(), authenticator) netrc = shared.netrc_authentication(reply.url(), authenticator)
if not netrc: if not netrc:
abort_on = self._get_abort_signals(reply) abort_on = self._get_abort_signals(reply)

View File

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