示例#1
0
def incdec(url, count, inc_or_dec):
    """Helper method for :navigate when `where' is increment/decrement.

    Args:
        url: The current url.
        count: How much to increment or decrement by.
        inc_or_dec: Either 'increment' or 'decrement'.
    """
    urlutils.ensure_valid(url)
    segments: Optional[Set[str]] = (set(config.val.url.incdec_segments))

    if segments is None:
        segments = {'path', 'query'}

    # Make a copy of the QUrl so we don't modify the original
    url = QUrl(url)
    # We're searching the last number so we walk the url segments backwards
    for segment, getter, setter in reversed(_URL_SEGMENTS):
        if segment not in segments:
            continue

        # Get the last number in a string not preceded by regex '%' or '%.'
        match = re.fullmatch(r'(.*\D|^)(?<!%)(?<!%.)(0*)(\d+)(.*)',
                             getter(url))
        if not match:
            continue

        setter(url, _get_incdec_value(match, inc_or_dec, count))
        qtutils.ensure_valid(url)

        return url

    raise Error("No number found in URL!")
示例#2
0
    def _resource_url(self, path: str) -> str:
        """Load qutebrowser resource files.

        Arguments:
            path: The relative path to the resource.
        """
        assert not posixpath.isabs(path), path
        url = QUrl('qute://resource')
        url.setPath('/' + path)
        urlutils.ensure_valid(url)
        urlstr = url.toString(QUrl.FullyEncoded)  # type: ignore[arg-type]
        return urlstr
示例#3
0
def path_up(url, count):
    """Helper method for :navigate when `where' is up.

    Args:
        url: The current url.
        count: The number of levels to go up in the url.
    """
    urlutils.ensure_valid(url)
    url = url.adjusted(QUrl.RemoveFragment | QUrl.RemoveQuery)
    path = url.path()
    if not path or path == '/':
        raise Error("Can't go up!")
    for _i in range(0, min(count, path.count('/'))):
        path = posixpath.join(path, posixpath.pardir)
    path = posixpath.normpath(path)
    url.setPath(path)
    return url
示例#4
0
def strip(url, count):
    """Strip fragment/query from a URL."""
    if count != 1:
        raise Error("Count is not supported when stripping URL components")
    urlutils.ensure_valid(url)
    return url.adjusted(QUrl.RemoveFragment | QUrl.RemoveQuery)