You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.4 KiB

4 years ago
  1. import operator
  2. import sys
  3. try:
  4. from collections import MutableMapping, Sequence # noqa
  5. except ImportError:
  6. from collections.abc import MutableMapping, Sequence # noqa
  7. PY3 = sys.version_info[0] >= 3
  8. if PY3:
  9. zip = zip
  10. from functools import lru_cache
  11. from io import StringIO
  12. from urllib.parse import (
  13. unquote, urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit
  14. )
  15. from urllib.request import urlopen
  16. str_types = str,
  17. int_types = int,
  18. iteritems = operator.methodcaller("items")
  19. else:
  20. from itertools import izip as zip # noqa
  21. from StringIO import StringIO
  22. from urlparse import (
  23. urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
  24. )
  25. from urllib import unquote # noqa
  26. from urllib2 import urlopen # noqa
  27. str_types = basestring
  28. int_types = int, long
  29. iteritems = operator.methodcaller("iteritems")
  30. from functools32 import lru_cache
  31. # On python < 3.3 fragments are not handled properly with unknown schemes
  32. def urlsplit(url):
  33. scheme, netloc, path, query, fragment = _urlsplit(url)
  34. if "#" in path:
  35. path, fragment = path.split("#", 1)
  36. return SplitResult(scheme, netloc, path, query, fragment)
  37. def urldefrag(url):
  38. if "#" in url:
  39. s, n, p, q, frag = urlsplit(url)
  40. defrag = urlunsplit((s, n, p, q, ''))
  41. else:
  42. defrag = url
  43. frag = ''
  44. return defrag, frag
  45. # flake8: noqa