summaryrefslogtreecommitdiff
path: root/jsonschema/compat.py
blob: 93492f9b4df358186d3cd7f3adf23260b7a3f2bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Python 2/3 compatibility helpers.

Note: This module is *not* public API.
"""
import contextlib
import operator
import sys


try:
    from collections.abc import MutableMapping, Sequence  # noqa
except ImportError:
    from collections import MutableMapping, Sequence  # noqa

PY3 = sys.version_info[0] >= 3

if PY3:
    zip = zip
    from functools import lru_cache
    from io import StringIO as NativeIO
    from urllib.parse import (
        unquote, urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit
    )
    from urllib.request import pathname2url, urlopen
    str_types = str,
    int_types = int,
    iteritems = operator.methodcaller("items")
else:
    from itertools import izip as zip  # noqa
    from io import BytesIO as NativeIO
    from urlparse import (
        urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
    )
    from urllib import pathname2url, unquote  # noqa
    import urllib2  # noqa
    def urlopen(*args, **kwargs):
        return contextlib.closing(urllib2.urlopen(*args, **kwargs))

    str_types = basestring
    int_types = int, long
    iteritems = operator.methodcaller("iteritems")

    from functools32 import lru_cache


# 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 = ''
    return defrag, frag


# flake8: noqa