debian-python-jsonschema/jsonschema/compat.py

58 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
import operator
import sys
try:
2013-05-11 22:17:49 +02:00
from collections import MutableMapping, Sequence # noqa
except ImportError:
2013-05-11 22:17:49 +02:00
from collections.abc import MutableMapping, Sequence # noqa
PY3 = sys.version_info[0] >= 3
if PY3:
zip = zip
2015-03-04 21:57:31 +01:00
from functools import lru_cache
from io import StringIO
from urllib.parse import (
unquote, urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit
)
from urllib.request import urlopen
2013-05-13 03:05:49 +02:00
str_types = str,
int_types = int,
iteritems = operator.methodcaller("items")
else:
2013-05-11 22:17:49 +02:00
from itertools import izip as zip # noqa
2015-03-04 21:57:31 +01:00
from repoze.lru import lru_cache
from StringIO import StringIO
from urlparse import (
urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
)
2013-05-11 22:17:49 +02:00
from urllib import unquote # noqa
from urllib2 import urlopen # noqa
2013-05-13 03:05:49 +02:00
str_types = basestring
int_types = int, long
iteritems = operator.methodcaller("iteritems")
2013-05-13 05:40:56 +02:00
# On python < 3.3 fragments are not handled properly with unknown schemes
def urlsplit(url):
scheme, netloc, path, query, fragment = _urlsplit(url)
if "#" in path:
path, fragment = path.split("#", 1)
return SplitResult(scheme, netloc, path, query, fragment)
def urldefrag(url):
if "#" in url:
s, n, p, q, frag = urlsplit(url)
defrag = urlunsplit((s, n, p, q, ''))
else:
defrag = url
frag = ''
2015-03-03 22:09:42 +01:00
return defrag, frag
2013-05-13 05:40:56 +02:00
# flake8: noqa