2016-07-07 11:37:29 +02:00
|
|
|
# coding: utf-8
|
2014-11-02 11:36:29 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
# this file used to help with handling various Python versions and implementations.
|
|
|
|
# we dropped support for Python <3.6 (and by this, for Jython and IronPython)
|
|
|
|
# so it's not needed anymore, but a lot of code is still referencing to these compat imports.
|
|
|
|
|
2018-01-23 15:53:01 +01:00
|
|
|
import base64
|
2014-12-12 04:01:08 +01:00
|
|
|
import ctypes
|
2014-11-02 11:23:40 +01:00
|
|
|
import getpass
|
2017-09-06 04:24:34 +02:00
|
|
|
import itertools
|
2014-11-02 11:23:40 +01:00
|
|
|
import os
|
2014-11-23 10:49:19 +01:00
|
|
|
import re
|
2015-09-05 17:40:44 +02:00
|
|
|
import shlex
|
2015-02-28 21:42:16 +01:00
|
|
|
import shutil
|
2015-01-10 19:55:36 +01:00
|
|
|
import socket
|
2016-04-23 12:28:49 +02:00
|
|
|
import struct
|
2014-11-02 11:23:40 +01:00
|
|
|
import sys
|
2015-10-25 20:04:55 +01:00
|
|
|
import xml.etree.ElementTree
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
import urllib.request as compat_urllib_request
|
|
|
|
import urllib.error as compat_urllib_error
|
|
|
|
import urllib.parse as compat_urllib_parse
|
|
|
|
from urllib.parse import urlparse as compat_urllib_parse_urlparse
|
|
|
|
import urllib.parse as compat_urlparse
|
|
|
|
import urllib.response as compat_urllib_response
|
|
|
|
import http.cookiejar as compat_cookiejar
|
|
|
|
compat_cookiejar_Cookie = compat_cookiejar.Cookie
|
|
|
|
import http.cookies as compat_cookies
|
2021-04-18 16:36:44 +02:00
|
|
|
compat_cookies_SimpleCookie = compat_cookies.SimpleCookie
|
2021-03-05 22:45:56 +01:00
|
|
|
import html.entities as compat_html_entities
|
|
|
|
compat_html_entities_html5 = compat_html_entities.html5
|
|
|
|
import http.client as compat_http_client
|
|
|
|
from urllib.error import HTTPError as compat_HTTPError
|
|
|
|
from urllib.request import urlretrieve as compat_urlretrieve
|
|
|
|
from html.parser import HTMLParser as compat_HTMLParser
|
|
|
|
|
|
|
|
# HTMLParseError has been deprecated in Python 3.3 and removed in
|
|
|
|
# Python 3.5. Introducing dummy exception for Python >3.5 for compatible
|
|
|
|
# and uniform cross-version exceptiong handling
|
|
|
|
|
|
|
|
|
|
|
|
class compat_HTMLParseError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
from subprocess import DEVNULL
|
|
|
|
compat_subprocess_get_DEVNULL = lambda: DEVNULL
|
|
|
|
|
|
|
|
import http.server as compat_http_server
|
|
|
|
compat_str = str
|
|
|
|
from urllib.parse import unquote_to_bytes as compat_urllib_parse_unquote_to_bytes
|
|
|
|
from urllib.parse import unquote as compat_urllib_parse_unquote
|
|
|
|
from urllib.parse import unquote_plus as compat_urllib_parse_unquote_plus
|
|
|
|
from urllib.parse import urlencode as compat_urllib_parse_urlencode
|
|
|
|
from urllib.request import DataHandler as compat_urllib_request_DataHandler
|
|
|
|
compat_basestring = str
|
|
|
|
compat_chr = chr
|
|
|
|
from xml.etree.ElementTree import ParseError as compat_xml_parse_error
|
2016-05-22 19:34:08 +02:00
|
|
|
etree = xml.etree.ElementTree
|
|
|
|
|
|
|
|
|
|
|
|
class _TreeBuilder(etree.TreeBuilder):
|
|
|
|
def doctype(self, name, pubid, system):
|
|
|
|
pass
|
|
|
|
|
2016-11-17 12:42:56 +01:00
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
from xml.etree.ElementTree import Element as compat_etree_Element
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
def compat_etree_fromstring(text):
|
|
|
|
return etree.XML(text, parser=etree.XMLParser(target=_TreeBuilder()))
|
2014-11-02 11:23:40 +01:00
|
|
|
|
2017-06-17 18:14:24 +02:00
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
compat_etree_register_namespace = etree.register_namespace
|
|
|
|
compat_xpath = lambda xpath: xpath
|
|
|
|
from urllib.parse import parse_qs as compat_parse_qs
|
|
|
|
compat_os_name = os.name
|
2017-06-17 18:14:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
if compat_os_name == 'nt':
|
2016-05-10 09:58:25 +02:00
|
|
|
def compat_shlex_quote(s):
|
2017-06-17 18:14:24 +02:00
|
|
|
return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"')
|
|
|
|
else:
|
2021-03-05 22:45:56 +01:00
|
|
|
from shlex import quote as compat_shlex_quote
|
2014-11-02 11:23:40 +01:00
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
compat_shlex_split = shlex.split
|
2015-09-05 17:40:44 +02:00
|
|
|
|
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
def compat_ord(c):
|
2014-11-23 20:41:03 +01:00
|
|
|
if type(c) is int:
|
|
|
|
return c
|
|
|
|
else:
|
|
|
|
return ord(c)
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
compat_getenv = os.getenv
|
|
|
|
compat_expanduser = os.path.expanduser
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
def compat_setenv(key, value, env=os.environ):
|
|
|
|
env[key] = value
|
2014-11-02 11:23:40 +01:00
|
|
|
|
|
|
|
|
2020-02-08 13:36:55 +01:00
|
|
|
if compat_os_name == 'nt' and sys.version_info < (3, 8):
|
|
|
|
# os.path.realpath on Windows does not follow symbolic links
|
|
|
|
# prior to Python 3.8 (see https://bugs.python.org/issue9949)
|
|
|
|
def compat_realpath(path):
|
|
|
|
while os.path.islink(path):
|
|
|
|
path = os.path.abspath(os.readlink(path))
|
|
|
|
return path
|
|
|
|
else:
|
|
|
|
compat_realpath = os.path.realpath
|
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
def compat_print(s):
|
|
|
|
assert isinstance(s, compat_str)
|
|
|
|
print(s)
|
2016-03-05 18:07:25 +01:00
|
|
|
|
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
compat_getpass = getpass.getpass
|
|
|
|
compat_input = input
|
|
|
|
compat_kwargs = lambda kwargs: kwargs
|
|
|
|
compat_numeric_types = (int, float, complex)
|
|
|
|
compat_integer_types = (int, )
|
|
|
|
compat_socket_create_connection = socket.create_connection
|
2018-06-17 22:22:08 +02:00
|
|
|
|
|
|
|
|
2014-11-19 18:21:58 +01:00
|
|
|
def workaround_optparse_bug9161():
|
2021-03-05 22:45:56 +01:00
|
|
|
pass
|
2014-11-19 18:21:58 +01:00
|
|
|
|
2016-04-23 12:28:49 +02:00
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
compat_get_terminal_size = shutil.get_terminal_size
|
|
|
|
compat_itertools_count = itertools.count
|
|
|
|
from tokenize import tokenize as compat_tokenize_tokenize
|
|
|
|
compat_struct_pack = struct.pack
|
|
|
|
compat_struct_unpack = struct.unpack
|
|
|
|
compat_Struct = struct.Struct
|
|
|
|
compat_zip = zip
|
|
|
|
compat_b64decode = base64.b64decode
|
2016-04-23 12:28:49 +02:00
|
|
|
|
2018-01-27 21:04:39 +01:00
|
|
|
|
2021-03-05 22:45:56 +01:00
|
|
|
def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
|
|
|
|
return ctypes.WINFUNCTYPE(*args, **kwargs)
|
2014-12-12 04:01:08 +01:00
|
|
|
|
|
|
|
|
2014-11-02 11:23:40 +01:00
|
|
|
__all__ = [
|
2017-06-11 21:35:57 +02:00
|
|
|
'compat_HTMLParseError',
|
2016-01-02 20:49:59 +01:00
|
|
|
'compat_HTMLParser',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_HTTPError',
|
2018-01-27 21:04:39 +01:00
|
|
|
'compat_Struct',
|
2018-01-23 15:53:01 +01:00
|
|
|
'compat_b64decode',
|
2015-02-01 11:36:59 +01:00
|
|
|
'compat_basestring',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_chr',
|
|
|
|
'compat_cookiejar',
|
2020-05-05 00:54:10 +02:00
|
|
|
'compat_cookiejar_Cookie',
|
2015-07-30 00:20:37 +02:00
|
|
|
'compat_cookies',
|
2021-04-18 16:36:44 +02:00
|
|
|
'compat_cookies_SimpleCookie',
|
2014-12-12 04:01:08 +01:00
|
|
|
'compat_ctypes_WINFUNCTYPE',
|
2019-03-05 19:18:52 +01:00
|
|
|
'compat_etree_Element',
|
2015-10-25 20:04:55 +01:00
|
|
|
'compat_etree_fromstring',
|
2017-02-01 20:15:25 +01:00
|
|
|
'compat_etree_register_namespace',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_expanduser',
|
2015-02-28 21:42:16 +01:00
|
|
|
'compat_get_terminal_size',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_getenv',
|
|
|
|
'compat_getpass',
|
|
|
|
'compat_html_entities',
|
2016-06-10 09:05:24 +02:00
|
|
|
'compat_html_entities_html5',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_http_client',
|
2015-01-30 02:57:37 +01:00
|
|
|
'compat_http_server',
|
2016-06-05 23:16:08 +02:00
|
|
|
'compat_input',
|
2018-06-17 22:22:08 +02:00
|
|
|
'compat_integer_types',
|
2015-07-10 12:58:12 +02:00
|
|
|
'compat_itertools_count',
|
2014-11-15 15:17:19 +01:00
|
|
|
'compat_kwargs',
|
2016-03-05 18:07:25 +01:00
|
|
|
'compat_numeric_types',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_ord',
|
2016-03-03 12:24:24 +01:00
|
|
|
'compat_os_name',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_parse_qs',
|
|
|
|
'compat_print',
|
2020-02-08 13:38:36 +01:00
|
|
|
'compat_realpath',
|
2016-05-09 17:55:03 +02:00
|
|
|
'compat_setenv',
|
2016-05-10 09:58:25 +02:00
|
|
|
'compat_shlex_quote',
|
2015-09-05 17:40:44 +02:00
|
|
|
'compat_shlex_split',
|
2015-01-10 19:55:36 +01:00
|
|
|
'compat_socket_create_connection',
|
2015-01-11 10:13:03 +01:00
|
|
|
'compat_str',
|
2016-05-03 10:50:16 +02:00
|
|
|
'compat_struct_pack',
|
|
|
|
'compat_struct_unpack',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_subprocess_get_DEVNULL',
|
2015-06-28 22:08:29 +02:00
|
|
|
'compat_tokenize_tokenize',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_urllib_error',
|
|
|
|
'compat_urllib_parse',
|
|
|
|
'compat_urllib_parse_unquote',
|
2015-07-17 19:28:34 +02:00
|
|
|
'compat_urllib_parse_unquote_plus',
|
2015-07-17 14:24:07 +02:00
|
|
|
'compat_urllib_parse_unquote_to_bytes',
|
2016-03-25 20:46:57 +01:00
|
|
|
'compat_urllib_parse_urlencode',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_urllib_parse_urlparse',
|
|
|
|
'compat_urllib_request',
|
2015-10-17 17:15:01 +02:00
|
|
|
'compat_urllib_request_DataHandler',
|
|
|
|
'compat_urllib_response',
|
2014-11-02 11:23:40 +01:00
|
|
|
'compat_urlparse',
|
|
|
|
'compat_urlretrieve',
|
|
|
|
'compat_xml_parse_error',
|
2016-03-17 21:51:38 +01:00
|
|
|
'compat_xpath',
|
2017-09-06 04:24:34 +02:00
|
|
|
'compat_zip',
|
2014-11-19 18:21:58 +01:00
|
|
|
'workaround_optparse_bug9161',
|
2014-11-02 11:23:40 +01:00
|
|
|
]
|