File: py3compat.py

package info (click to toggle)
pypandoc 1.15%2Bds0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 380 kB
  • sloc: python: 1,299; sh: 12; makefile: 10
file content (77 lines) | stat: -rw-r--r-- 1,916 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-

from __future__ import with_statement

import locale
import sys

# compat code from IPython py3compat.py and encoding.py, which is licensed under the terms of the
# Modified BSD License (also known as New or Revised or 3-Clause BSD)
_DEFAULT_ENCODING = None
try:
    # There are reports of getpreferredencoding raising errors
    # in some cases, which may well be fixed, but let's be conservative here.
    _DEFAULT_ENCODING = locale.getpreferredencoding()
except Exception:
    pass

_DEFAULT_ENCODING = _DEFAULT_ENCODING or sys.getdefaultencoding()


def _decode(s, encoding=None):
    encoding = encoding or _DEFAULT_ENCODING
    return s.decode(encoding)


def _encode(u, encoding=None):
    encoding = encoding or _DEFAULT_ENCODING
    return u.encode(encoding)


def cast_unicode(s, encoding=None):
    if isinstance(s, bytes):
        return _decode(s, encoding)
    return s


def cast_bytes(s, encoding=None):
    # bytes == str on py2.7 -> always encode on py2
    if not isinstance(s, bytes):
        return _encode(s, encoding)
    return s


if sys.version_info[0] >= 3:
    PY3 = True

    string_types = (str,)
    unicode_type = str

    # from http://stackoverflow.com/questions/11687478/convert-a-filename-to-a-file-url
    from urllib.parse import urljoin, urlparse
    from urllib.request import pathname2url, url2pathname


    def path2url(path):  # noqa: E303
        return urljoin('file:', pathname2url(path))


    def url2path(url):  # noqa: E303
        return url2pathname(urlparse(url).path)

else:
    PY3 = False

    string_types = (str, unicode)  # noqa: F821
    unicode_type = unicode  # noqa: F821

    from urlparse import urljoin, urlparse
    import urllib


    def path2url(path):  # noqa: E303
        return urljoin('file:', urllib.pathname2url(path))


    def url2path(url):  # noqa: E303
        return urllib.url2pathname(urlparse(url).path)