summaryrefslogtreecommitdiff
path: root/qutebrowser/utils/urlutils.py
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2019-09-11 17:00:39 +0200
committerFlorian Bruhin <me@the-compiler.org>2019-09-11 17:13:25 +0200
commitb76036405f938821f9f2221c6540899bab411a17 (patch)
treef1bc08011d62eda169818ffb8ab5fccd01f4ebb2 /qutebrowser/utils/urlutils.py
parentb088e7adfbd76922d05253000d02201d95bab3e1 (diff)
downloadqutebrowser-b76036405f938821f9f2221c6540899bab411a17.tar.gz
qutebrowser-b76036405f938821f9f2221c6540899bab411a17.zip
Move urlutils.incdec_number to navigate.py
Diffstat (limited to 'qutebrowser/utils/urlutils.py')
-rw-r--r--qutebrowser/utils/urlutils.py116
1 files changed, 0 insertions, 116 deletions
diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py
index e639bd34e..54c2ecc3c 100644
--- a/qutebrowser/utils/urlutils.py
+++ b/qutebrowser/utils/urlutils.py
@@ -511,122 +511,6 @@ def encoded_url(url):
return bytes(url.toEncoded()).decode('ascii')
-class IncDecError(Exception):
-
- """Exception raised by incdec_number on problems.
-
- Attributes:
- msg: The error message.
- url: The QUrl which caused the error.
- """
-
- def __init__(self, msg, url):
- super().__init__(msg)
- self.url = url
- self.msg = msg
-
- def __str__(self):
- return '{}: {}'.format(self.msg, self.url.toString())
-
-
-def _get_incdec_value(match, incdec, url, count):
- """Get an incremented/decremented URL based on a URL match."""
- pre, zeroes, number, post = match.groups()
- # This should always succeed because we match \d+
- val = int(number)
- if incdec == 'decrement':
- if val < count:
- raise IncDecError("Can't decrement {} by {}!".format(val, count),
- url)
- val -= count
- elif incdec == 'increment':
- val += count
- else:
- raise ValueError("Invalid value {} for incdec!".format(incdec))
- if zeroes:
- if len(number) < len(str(val)):
- zeroes = zeroes[1:]
- elif len(number) > len(str(val)):
- zeroes += '0'
-
- return ''.join([pre, zeroes, str(val), post])
-
-
-# Order of the segments in a URL.
-# Each list entry is a tuple of (path name (string), getter, setter).
-# Note that the getters must not use FullyDecoded decoded mode to prevent loss
-# of information. (host and path use FullyDecoded by default)
-_URL_SEGMENTS = [
- ('host',
- lambda url: url.host(QUrl.FullyEncoded),
- lambda url, host: url.setHost(host, QUrl.StrictMode)),
-
- ('port',
- lambda url: str(url.port()) if url.port() > 0 else '',
- lambda url, x: url.setPort(int(x))),
-
- ('path',
- lambda url: url.path(QUrl.FullyEncoded),
- lambda url, path: url.setPath(path, QUrl.StrictMode)),
-
- ('query',
- lambda url: url.query(QUrl.FullyEncoded),
- lambda url, query: url.setQuery(query, QUrl.StrictMode)),
-
- ('anchor',
- lambda url: url.fragment(QUrl.FullyEncoded),
- lambda url, fragment: url.setFragment(fragment, QUrl.StrictMode)),
-]
-
-
-def incdec_number(url, incdec, count=1, segments=None):
- """Find a number in the url and increment or decrement it.
-
- Args:
- url: The current url
- incdec: Either 'increment' or 'decrement'
- count: The number to increment or decrement by
- segments: A set of URL segments to search. Valid segments are:
- 'host', 'port', 'path', 'query', 'anchor'.
- Default: {'path', 'query'}
-
- Return:
- The new url with the number incremented/decremented.
-
- Raises IncDecError if the url contains no number.
- """
- if not url.isValid():
- raise InvalidUrlError(url)
-
- if segments is None:
- segments = {'path', 'query'}
- valid_segments = {'host', 'port', 'path', 'query', 'anchor'}
- if segments - valid_segments:
- extra_elements = segments - valid_segments
- raise IncDecError("Invalid segments: {}".format(
- ', '.join(extra_elements)), url)
-
- # 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, incdec, url, count))
- qtutils.ensure_valid(url)
-
- return url
-
- raise IncDecError("No number found in URL!", url)
-
-
def file_url(path):
"""Return a file:// url (as string) to the given local path.