summaryrefslogtreecommitdiff
path: root/qutebrowser/utils/urlutils.py
diff options
context:
space:
mode:
authorarza <arza@arza.us>2019-10-12 19:31:18 +0300
committerGitHub <noreply@github.com>2019-10-12 19:31:18 +0300
commit3be04ad8f30a05ed4154d01ada364fcf088d82ba (patch)
treed2e6ef09435dc534dd8d13cf96ff1c170e9eeabe /qutebrowser/utils/urlutils.py
parent8b96b81c9ebe397dd29103833c4d4061e0120606 (diff)
parent926bcdbbe17be0b8d64c0d288a5b63bbb85667c1 (diff)
downloadqutebrowser-3be04ad8f30a05ed4154d01ada364fcf088d82ba.tar.gz
qutebrowser-3be04ad8f30a05ed4154d01ada364fcf088d82ba.zip
Merge branch 'master' into auto_search
Diffstat (limited to 'qutebrowser/utils/urlutils.py')
-rw-r--r--qutebrowser/utils/urlutils.py81
1 files changed, 45 insertions, 36 deletions
diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py
index ad565d27b..0243ed33c 100644
--- a/qutebrowser/utils/urlutils.py
+++ b/qutebrowser/utils/urlutils.py
@@ -25,6 +25,7 @@ import os.path
import ipaddress
import posixpath
import urllib.parse
+import typing
from PyQt5.QtCore import QUrl, QUrlQuery
from PyQt5.QtNetwork import QHostInfo, QHostAddress, QNetworkProxy
@@ -58,7 +59,7 @@ class InvalidUrlError(Exception):
"""Error raised if a function got an invalid URL."""
- def __init__(self, url):
+ def __init__(self, url: QUrl) -> None:
if url.isValid():
raise ValueError("Got valid URL {}!".format(url.toDisplayString()))
self.url = url
@@ -66,7 +67,7 @@ class InvalidUrlError(Exception):
super().__init__(self.msg)
-def _parse_search_term(s):
+def _parse_search_term(s: str) -> typing.Tuple[typing.Optional[str], str]:
"""Get a search engine name and search term from a string.
Args:
@@ -79,7 +80,7 @@ def _parse_search_term(s):
split = s.split(maxsplit=1)
if len(split) == 2:
- engine = split[0]
+ engine = split[0] # type: typing.Optional[str]
try:
config.val.url.searchengines[engine]
except KeyError:
@@ -97,7 +98,7 @@ def _parse_search_term(s):
return (engine, term)
-def _get_search_url(txt):
+def _get_search_url(txt: str) -> QUrl:
"""Get a search engine URL for a text.
Args:
@@ -117,14 +118,14 @@ def _get_search_url(txt):
if config.val.url.open_base_url and term in config.val.url.searchengines:
url = qurl_from_user_input(config.val.url.searchengines[term])
- url.setPath(None)
- url.setFragment(None)
- url.setQuery(None)
+ url.setPath(None) # type: ignore
+ url.setFragment(None) # type: ignore
+ url.setQuery(None) # type: ignore
qtutils.ensure_valid(url)
return url
-def _is_url_naive(urlstr):
+def _is_url_naive(urlstr: str) -> bool:
"""Naive check if given URL is really a URL.
Args:
@@ -152,7 +153,7 @@ def _is_url_naive(urlstr):
return bool(re.search(tld, host) and not re.search(forbidden, host))
-def _is_url_dns(urlstr):
+def _is_url_dns(urlstr: str) -> bool:
"""Check if a URL is really a URL via DNS.
Args:
@@ -180,8 +181,11 @@ def _is_url_dns(urlstr):
return not info.error()
-def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True,
- force_search=False):
+def fuzzy_url(urlstr: str,
+ cwd: str = None,
+ relative: bool = False,
+ do_search: bool = True,
+ force_search: bool = False) -> QUrl:
"""Get a QUrl based on a user input which is URL or search term.
Args:
@@ -220,7 +224,7 @@ def fuzzy_url(urlstr, cwd=None, relative=False, do_search=True,
return url
-def _has_explicit_scheme(url):
+def _has_explicit_scheme(url: QUrl) -> bool:
"""Check if a url has an explicit scheme given.
Args:
@@ -230,12 +234,12 @@ def _has_explicit_scheme(url):
# after the scheme delimiter. Since we don't know of any URIs
# using this and want to support e.g. searching for scoped C++
# symbols, we treat this as not a URI anyways.
- return (url.isValid() and url.scheme() and
+ return bool(url.isValid() and url.scheme() and
(url.host() or url.path()) and
not url.path().startswith(':'))
-def is_special_url(url):
+def is_special_url(url: QUrl) -> bool:
"""Return True if url is an about:... or other special URL.
Args:
@@ -247,7 +251,7 @@ def is_special_url(url):
return url.scheme() in special_schemes
-def is_url(urlstr):
+def is_url(urlstr: str) -> bool:
"""Check if url seems to be a valid URL.
Args:
@@ -304,7 +308,7 @@ def is_url(urlstr):
return url
-def qurl_from_user_input(urlstr):
+def qurl_from_user_input(urlstr: str) -> QUrl:
"""Get a QUrl based on a user input. Additionally handles IPv6 addresses.
QUrl.fromUserInput handles something like '::1' as a file URL instead of an
@@ -340,12 +344,12 @@ def qurl_from_user_input(urlstr):
return QUrl('http://[{}]{}'.format(ipstr, rest))
-def ensure_valid(url):
+def ensure_valid(url: QUrl) -> None:
if not url.isValid():
raise InvalidUrlError(url)
-def invalid_url_error(url, action):
+def invalid_url_error(url: QUrl, action: str) -> None:
"""Display an error message for a URL.
Args:
@@ -359,7 +363,7 @@ def invalid_url_error(url, action):
message.error(errstring)
-def raise_cmdexc_if_invalid(url):
+def raise_cmdexc_if_invalid(url: QUrl) -> None:
"""Check if the given QUrl is invalid, and if so, raise a CommandError."""
try:
ensure_valid(url)
@@ -367,7 +371,10 @@ def raise_cmdexc_if_invalid(url):
raise cmdutils.CommandError(str(e))
-def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False):
+def get_path_if_valid(pathstr: str,
+ cwd: str = None,
+ relative: bool = False,
+ check_exists: bool = False) -> typing.Optional[str]:
"""Check if path is a valid path.
Args:
@@ -385,7 +392,7 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False):
expanded = os.path.expanduser(pathstr)
if os.path.isabs(expanded):
- path = expanded
+ path = expanded # type: typing.Optional[str]
elif relative and cwd:
path = os.path.join(cwd, expanded)
elif relative:
@@ -412,7 +419,7 @@ def get_path_if_valid(pathstr, cwd=None, relative=False, check_exists=False):
return path
-def filename_from_url(url):
+def filename_from_url(url: QUrl) -> typing.Optional[str]:
"""Get a suitable filename from a URL.
Args:
@@ -432,7 +439,7 @@ def filename_from_url(url):
return None
-def host_tuple(url):
+def host_tuple(url: QUrl) -> typing.Tuple[str, str, int]:
"""Get a (scheme, host, port) tuple from a QUrl.
This is suitable to identify a connection, e.g. for SSL errors.
@@ -457,7 +464,7 @@ def host_tuple(url):
return scheme, host, port
-def get_errstring(url, base="Invalid URL"):
+def get_errstring(url: QUrl, base: str = "Invalid URL") -> str:
"""Get an error string for a URL.
Args:
@@ -474,7 +481,7 @@ def get_errstring(url, base="Invalid URL"):
return base
-def same_domain(url1, url2):
+def same_domain(url1: QUrl, url2: QUrl) -> bool:
"""Check if url1 and url2 belong to the same website.
This will use a "public suffix list" to determine what a "top level domain"
@@ -491,7 +498,7 @@ def same_domain(url1, url2):
suffix1 = url1.topLevelDomain()
suffix2 = url2.topLevelDomain()
- if suffix1 == '':
+ if not suffix1:
return url1.host() == url2.host()
if suffix1 != suffix2:
@@ -502,7 +509,7 @@ def same_domain(url1, url2):
return domain1 == domain2
-def encoded_url(url):
+def encoded_url(url: QUrl) -> str:
"""Return the fully encoded url as string.
Args:
@@ -511,16 +518,17 @@ def encoded_url(url):
return bytes(url.toEncoded()).decode('ascii')
-def file_url(path):
+def file_url(path: str) -> QUrl:
"""Return a file:// url (as string) to the given local path.
Arguments:
path: The absolute path to the local file
"""
- return QUrl.fromLocalFile(path).toString(QUrl.FullyEncoded)
+ url = QUrl.fromLocalFile(path)
+ return url.toString(QUrl.FullyEncoded) # type: ignore
-def data_url(mimetype, data):
+def data_url(mimetype: str, data: bytes) -> QUrl:
"""Get a data: QUrl for the given data."""
b64 = base64.b64encode(data).decode('ascii')
url = QUrl('data:{};base64,{}'.format(mimetype, b64))
@@ -528,7 +536,7 @@ def data_url(mimetype, data):
return url
-def safe_display_string(qurl):
+def safe_display_string(qurl: QUrl) -> str:
"""Get a IDN-homograph phishing safe form of the given QUrl.
If we're dealing with a Punycode-encoded URL, this prepends the hostname in
@@ -539,19 +547,20 @@ def safe_display_string(qurl):
"""
ensure_valid(qurl)
- host = qurl.host(QUrl.FullyEncoded)
+ host = qurl.host(QUrl.FullyEncoded) # type: ignore
if '..' in host: # pragma: no cover
# WORKAROUND for https://bugreports.qt.io/browse/QTBUG-60364
return '(unparseable URL!) {}'.format(qurl.toDisplayString())
for part in host.split('.'):
- if part.startswith('xn--') and host != qurl.host(QUrl.FullyDecoded):
+ url_host = qurl.host(QUrl.FullyDecoded) # type: ignore
+ if part.startswith('xn--') and host != url_host:
return '({}) {}'.format(host, qurl.toDisplayString())
return qurl.toDisplayString()
-def query_string(qurl):
+def query_string(qurl: QUrl) -> str:
"""Get a query string for the given URL.
This is a WORKAROUND for:
@@ -567,11 +576,11 @@ class InvalidProxyTypeError(Exception):
"""Error raised when proxy_from_url gets an unknown proxy type."""
- def __init__(self, typ):
+ def __init__(self, typ: str) -> None:
super().__init__("Invalid proxy type {}!".format(typ))
-def proxy_from_url(url):
+def proxy_from_url(url: QUrl) -> QNetworkProxy:
"""Create a QNetworkProxy from QUrl and a proxy type.
Args: