def test_invalid_url_error(urlutils_message_mock, url, valid, has_err_string): """Test invalid_url_error(). Args: url: The URL to check. valid: Whether the QUrl is valid (isValid() == True). has_err_string: Whether the QUrl is expected to have errorString set. """ qurl = QUrl(url) assert qurl.isValid() == valid if valid: with pytest.raises(ValueError): urlutils.invalid_url_error(0, qurl, '') assert not urlutils_message_mock.messages else: assert bool(qurl.errorString()) == has_err_string urlutils.invalid_url_error(0, qurl, 'frozzle') msg = urlutils_message_mock.getmsg(urlutils_message_mock.Level.error) if has_err_string: expected_text = ("Trying to frozzle with invalid URL - " + qurl.errorString()) else: expected_text = "Trying to frozzle with invalid URL" assert msg.text == expected_text
def get(self, url, page=None, fileobj=None, filename=None): """Start a download with a link URL. Args: url: The URL to get, as QUrl page: The QWebPage to get the download from. fileobj: The file object to write the answer to. filename: A path to write the data to. Return: The created DownloadItem. """ if fileobj is not None and filename is not None: raise TypeError("Only one of fileobj/filename may be given!") if not url.isValid(): urlutils.invalid_url_error(self._win_id, url, "start download") return req = QNetworkRequest(url) # WORKAROUND for Qt corrupting data loaded from cache: # https://bugreports.qt-project.org/browse/QTBUG-42757 req.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.AlwaysNetwork) if page is None: nam = self._networkmanager else: nam = page.networkAccessManager() reply = nam.get(req) return self.fetch(reply, fileobj, filename)
def test_invalid_url_error(message_mock, caplog, url, valid, has_err_string): """Test invalid_url_error(). Args: url: The URL to check. valid: Whether the QUrl is valid (isValid() == True). has_err_string: Whether the QUrl is expected to have errorString set. """ qurl = QUrl(url) assert qurl.isValid() == valid if valid: with pytest.raises(ValueError): urlutils.invalid_url_error(qurl, '') assert not message_mock.messages else: assert bool(qurl.errorString()) == has_err_string with caplog.at_level(logging.ERROR): urlutils.invalid_url_error(qurl, 'frozzle') msg = message_mock.getmsg(usertypes.MessageLevel.error) if has_err_string: expected_text = ("Trying to frozzle with invalid URL - " + qurl.errorString()) else: expected_text = "Trying to frozzle with invalid URL" assert msg.text == expected_text
def _remove_tab(self, tab): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. """ idx = self.indexOf(tab) if idx == -1: raise ValueError("tab {} is not contained in TabbedWidget!".format( tab)) if tab is self._now_focused: self._now_focused = None if tab is objreg.get('last-focused-tab', None, scope='window', window=self._win_id): objreg.delete('last-focused-tab', scope='window', window=self._win_id) if tab.cur_url.isValid(): history_data = qtutils.serialize(tab.history()) entry = UndoEntry(tab.cur_url, history_data) self._undo_stack.append(entry) elif tab.cur_url.isEmpty(): # There are some good reasons why an URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/The-Compiler/qutebrowser/issues/163 pass else: # We display a warnings for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(self._win_id, tab.cur_url, "saving tab") tab.shutdown() self.removeTab(idx) tab.deleteLater()
def get(self, url, page=None, fileobj=None, filename=None, auto_remove=False): """Start a download with a link URL. Args: url: The URL to get, as QUrl page: The QWebPage to get the download from. fileobj: The file object to write the answer to. filename: A path to write the data to. auto_remove: Whether to remove the download even if ui -> remove-finished-downloads is set to false. Return: If the download could start immediately, (fileobj/filename given), the created DownloadItem. If not, None. """ if fileobj is not None and filename is not None: raise TypeError("Only one of fileobj/filename may be given!") if not url.isValid(): urlutils.invalid_url_error(self._win_id, url, "start download") return req = QNetworkRequest(url) return self.get_request(req, page, fileobj, filename, auto_remove)
def _remove_tab(self, tab): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. """ idx = self.indexOf(tab) if idx == -1: raise ValueError( "tab {} is not contained in TabbedWidget!".format(tab)) if tab is self._now_focused: self._now_focused = None if tab is objreg.get( 'last-focused-tab', None, scope='window', window=self._win_id): objreg.delete('last-focused-tab', scope='window', window=self._win_id) if tab.cur_url.isValid(): history_data = qtutils.serialize(tab.history()) entry = UndoEntry(tab.cur_url, history_data) self._undo_stack.append(entry) elif tab.cur_url.isEmpty(): # There are some good reasons why an URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/The-Compiler/qutebrowser/issues/163 pass else: # We display a warnings for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(self._win_id, tab.cur_url, "saving tab") tab.shutdown() self.removeTab(idx) tab.deleteLater()
def test_invalid_url_error(urlutils_message_mock, url, valid, has_err_string): """Test invalid_url_error(). Args: url: The URL to check. valid: Whether the QUrl is valid (isValid() == True). has_err_string: Whether the QUrl is expected to have errorString set. """ qurl = QUrl(url) assert qurl.isValid() == valid if valid: with pytest.raises(ValueError): urlutils.invalid_url_error(0, qurl, '') assert not urlutils_message_mock.messages else: assert bool(qurl.errorString()) == has_err_string urlutils.invalid_url_error(0, qurl, 'frozzle') msg = urlutils_message_mock.getmsg() assert msg.win_id == 0 assert not msg.immediate if has_err_string: expected_text = ("Trying to frozzle with invalid URL - " + qurl.errorString()) else: expected_text = "Trying to frozzle with invalid URL" assert msg.text == expected_text
def _remove_tab(self, tab, *, add_undo=True, new_undo=True, crashed=False): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. add_undo: Whether the tab close can be undone. new_undo: Whether the undo entry should be a new item in the stack. crashed: Whether we're closing a tab with crashed renderer process. """ idx = self.widget.indexOf(tab) if idx == -1: if crashed: return raise TabDeletedError("tab {} is not contained in " "TabbedWidget!".format(tab)) if tab is self._now_focused: self._now_focused = None tab.pending_removal = True if tab.url().isEmpty(): # There are some good reasons why a URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/qutebrowser/qutebrowser/issues/163 pass elif not tab.url().isValid(): # We display a warning for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(tab.url(), "saving tab") elif add_undo: try: history_data = tab.history.private_api.serialize() except browsertab.WebTabError: pass # special URL else: entry = _UndoEntry(url=tab.url(), history=history_data, index=idx, pinned=tab.data.pinned) if new_undo or not self.undo_stack: self.undo_stack.append([entry]) else: self.undo_stack[-1].append(entry) tab.private_api.shutdown() self.widget.removeTab(idx) if not crashed: # WORKAROUND for a segfault when we delete the crashed tab. # see https://bugreports.qt.io/browse/QTBUG-58698 if not qtutils.version_check('5.12'): # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-58982 # Seems to affect Qt 5.7-5.11 as well. tab.layout().unwrap() tab.deleteLater()
def _remove_tab(self, tab, *, add_undo=True, new_undo=True, crashed=False): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. add_undo: Whether the tab close can be undone. new_undo: Whether the undo entry should be a new item in the stack. crashed: Whether we're closing a tab with crashed renderer process. """ idx = self.widget.indexOf(tab) if idx == -1: if crashed: return raise TabDeletedError("tab {} is not contained in " "TabbedWidget!".format(tab)) if tab is self._now_focused: self._now_focused = None if tab is objreg.get('last-focused-tab', None, scope='window', window=self._win_id): objreg.delete('last-focused-tab', scope='window', window=self._win_id) if tab.url().isEmpty(): # There are some good reasons why a URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/qutebrowser/qutebrowser/issues/163 pass elif not tab.url().isValid(): # We display a warning for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(tab.url(), "saving tab") elif add_undo: try: history_data = tab.history.serialize() except browsertab.WebTabError: pass # special URL else: entry = UndoEntry(tab.url(), history_data, idx, tab.data.pinned) if new_undo or not self._undo_stack: self._undo_stack.append([entry]) else: self._undo_stack[-1].append(entry) tab.shutdown() self.widget.removeTab(idx) if not crashed: # WORKAROUND for a segfault when we delete the crashed tab. # see https://bugreports.qt.io/browse/QTBUG-58698 tab.layout().unwrap() tab.deleteLater()
def get(self, url, page): """Start a download with a link URL. Args: url: The URL to get, as QUrl page: The QWebPage to get the download from. """ if not url.isValid(): urlutils.invalid_url_error('last-focused', url, "start download") return req = QNetworkRequest(url) reply = page.networkAccessManager().get(req) self.fetch(reply)
def get(self, url, page): """Start a download with a link URL. Args: url: The URL to get, as QUrl page: The QWebPage to get the download from. """ if not url.isValid(): urlutils.invalid_url_error(self._win_id, url, "start download") return req = QNetworkRequest(url) reply = page.networkAccessManager().get(req) self.fetch(reply)
def prompt_save(self, win_id, url): """Prompt for a new quickmark name to be added and add it. Args: win_id: The current window ID. url: The quickmark url as a QUrl. """ if not url.isValid(): urlutils.invalid_url_error(win_id, url, "save quickmark") return urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) message.ask_async( win_id, "Add quickmark:", usertypes.PromptMode.text, functools.partial(self.quickmark_add, win_id, urlstr))
def _remove_tab(self, tab, *, add_undo=True, crashed=False): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. add_undo: Whether the tab close can be undone. crashed: Whether we're closing a tab with crashed renderer process. """ idx = self.indexOf(tab) if idx == -1: if crashed: return raise TabDeletedError("tab {} is not contained in " "TabbedWidget!".format(tab)) if tab is self._now_focused: self._now_focused = None if tab is objreg.get( 'last-focused-tab', None, scope='window', window=self._win_id): objreg.delete('last-focused-tab', scope='window', window=self._win_id) if tab.url().isEmpty(): # There are some good reasons why a URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/qutebrowser/qutebrowser/issues/163 pass elif not tab.url().isValid(): # We display a warning for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(tab.url(), "saving tab") elif add_undo: try: history_data = tab.history.serialize() except browsertab.WebTabError: pass # special URL else: entry = UndoEntry(tab.url(), history_data, idx, tab.data.pinned) self._undo_stack.append(entry) tab.shutdown() self.removeTab(idx) if not crashed: # WORKAROUND for a segfault when we delete the crashed tab. # see https://bugreports.qt.io/browse/QTBUG-58698 tab.layout().unwrap() tab.deleteLater()
def get(self, url, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(self._win_id, url, "start download") return req = QNetworkRequest(url) return self.get_request(req, **kwargs)
def prompt_save(self, url): """Prompt for a new quickmark name to be added and add it. Args: url: The quickmark url as a QUrl. """ if not url.isValid(): urlutils.invalid_url_error(url, "save quickmark") return urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) message.ask_async( "Add quickmark:", usertypes.PromptMode.text, functools.partial(self.quickmark_add, urlstr), text="Please enter a quickmark name for<br/><b>{}</b>".format( html.escape(url.toDisplayString())), url=urlstr)
def get(self, url, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(url, "start download") return req = QNetworkRequest(url) return self.get_request(req, **kwargs)
def get(self, url, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl **kwargs: passed to get_request(). Return: If the download could start immediately, (fileobj/filename given), the created DownloadItem. If not, None. """ if not url.isValid(): urlutils.invalid_url_error(self._win_id, url, "start download") return req = QNetworkRequest(url) return self.get_request(req, **kwargs)
def get(self, url, *, user_agent=None, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl user_agent: The UA to set for the request, or None. **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(url, "start download") return None req = QNetworkRequest(url) if user_agent is not None: req.setHeader(QNetworkRequest.UserAgentHeader, user_agent) return self.get_request(req, **kwargs)
def get(self, url, *, user_agent=None, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl user_agent: The UA to set for the request, or None. **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(url, "start download") return req = QNetworkRequest(url) if user_agent is not None: req.setHeader(QNetworkRequest.UserAgentHeader, user_agent) return self.get_request(req, **kwargs)
def get(self, url, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(url, "start download") return None req = QNetworkRequest(url) user_agent = websettings.user_agent(url) req.setHeader(QNetworkRequest.UserAgentHeader, user_agent) return self.get_request(req, **kwargs)
def get(self, url, cache=True, **kwargs): """Start a download with a link URL. Args: url: The URL to get, as QUrl cache: If set to False, don't cache the response. **kwargs: passed to get_request(). Return: The created DownloadItem. """ if not url.isValid(): urlutils.invalid_url_error(url, "start download") return None req = QNetworkRequest(url) user_agent = websettings.user_agent(url) req.setHeader(QNetworkRequest.UserAgentHeader, user_agent) if not cache: req.setAttribute(QNetworkRequest.CacheSaveControlAttribute, False) return self.get_request(req, **kwargs)
def _remove_tab(self, tab, *, add_undo=True, new_undo=True, crashed=False): """Remove a tab from the tab list and delete it properly. Args: tab: The QWebView to be closed. add_undo: Whether the tab close can be undone. new_undo: Whether the undo entry should be a new item in the stack. crashed: Whether we're closing a tab with crashed renderer process. """ idx = self.indexOf(tab) if idx == -1: if crashed: return raise TabDeletedError("tab {} is not contained in " "TabbedWidget!".format(tab)) if tab is self._now_focused: self._now_focused = None if tab is objreg.get( 'last-focused-tab', None, scope='window', window=self._win_id): objreg.delete('last-focused-tab', scope='window', window=self._win_id) if tab.url().isEmpty(): # There are some good reasons why a URL could be empty # (target="_blank" with a download, see [1]), so we silently ignore # this. # [1] https://github.com/qutebrowser/qutebrowser/issues/163 pass elif not tab.url().isValid(): # We display a warning for URLs which are not empty but invalid - # but we don't return here because we want the tab to close either # way. urlutils.invalid_url_error(tab.url(), "saving tab") elif add_undo: try: history_data = tab.history.serialize() except browsertab.WebTabError: pass # special URL else: entry = UndoEntry(tab.url(), history_data, idx, tab.data.pinned) if new_undo or not self._undo_stack: self._undo_stack.append([entry]) else: self._undo_stack[-1].append(entry) cur_node = tab.node node_parent = cur_node.parent if node_parent: node_siblings = list(node_parent.children) node_children = cur_node.children if node_children: next_node = node_children[0] # prvni node se stane parentem pro ostatní děti for n in node_children[1:]: n.parent = next_node # swap nodes node_idx = node_siblings.index(cur_node) node_siblings[node_idx] = next_node node_parent.children = tuple(node_siblings) cur_node.children = tuple() cur_node.parent = None else: self.print_tree_tab_structure("!!! ERROR !!! Tab ID %s\n" % tab.tab_id) tab.shutdown() self.removeTab(idx) if not crashed: # WORKAROUND for a segfault when we delete the crashed tab. # see https://bugreports.qt.io/browse/QTBUG-58698 tab.layout().unwrap() tab.deleteLater()