2022-02-04 14:37:02 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2022-04-24 18:28:18 +02:00
|
|
|
import warnings
|
2022-02-04 14:37:02 +01:00
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
|
|
|
|
from ._deprecated import * # noqa: F401, F403
|
2022-04-24 18:28:18 +02:00
|
|
|
from .compat_utils import passthrough_module
|
|
|
|
|
|
|
|
# XXX: Implement this the same way as other DeprecationWarnings without circular import
|
2022-07-17 15:15:43 +02:00
|
|
|
passthrough_module(__name__, '._legacy', callback=lambda attr: warnings.warn(
|
2022-07-18 02:20:54 +02:00
|
|
|
DeprecationWarning(f'{__name__}.{attr} is deprecated'), stacklevel=3))
|
2022-02-04 14:37:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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 exception handling
|
|
|
|
class compat_HTMLParseError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class _TreeBuilder(etree.TreeBuilder):
|
|
|
|
def doctype(self, name, pubid, system):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def compat_etree_fromstring(text):
|
|
|
|
return etree.XML(text, parser=etree.XMLParser(target=_TreeBuilder()))
|
|
|
|
|
|
|
|
|
|
|
|
compat_os_name = os._name if os.name == 'java' else os.name
|
|
|
|
|
|
|
|
|
|
|
|
if compat_os_name == 'nt':
|
|
|
|
def compat_shlex_quote(s):
|
2022-07-18 02:20:54 +02:00
|
|
|
import re
|
2022-02-04 14:37:02 +01:00
|
|
|
return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"')
|
|
|
|
else:
|
|
|
|
from shlex import quote as compat_shlex_quote # noqa: F401
|
|
|
|
|
|
|
|
|
|
|
|
def compat_ord(c):
|
|
|
|
return c if isinstance(c, int) else ord(c)
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
2022-06-21 13:32:56 +02:00
|
|
|
return os.path.realpath(path)
|
2022-02-04 14:37:02 +01:00
|
|
|
else:
|
|
|
|
compat_realpath = os.path.realpath
|
|
|
|
|
|
|
|
|
|
|
|
# Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
|
|
|
|
# See https://github.com/yt-dlp/yt-dlp/issues/792
|
|
|
|
# https://docs.python.org/3/library/os.path.html#os.path.expanduser
|
|
|
|
if compat_os_name in ('nt', 'ce'):
|
|
|
|
def compat_expanduser(path):
|
|
|
|
HOME = os.environ.get('HOME')
|
|
|
|
if not HOME:
|
|
|
|
return os.path.expanduser(path)
|
|
|
|
elif not path.startswith('~'):
|
|
|
|
return path
|
|
|
|
i = path.replace('\\', '/', 1).find('/') # ~user
|
|
|
|
if i < 0:
|
|
|
|
i = len(path)
|
|
|
|
userhome = os.path.join(os.path.dirname(HOME), path[1:i]) if i > 1 else HOME
|
|
|
|
return userhome + path[i:]
|
|
|
|
else:
|
|
|
|
compat_expanduser = os.path.expanduser
|
2022-07-17 15:15:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# NB: Add modules that are imported dynamically here so that PyInstaller can find them
|
|
|
|
# See https://github.com/pyinstaller/pyinstaller-hooks-contrib/issues/438
|
|
|
|
if False:
|
|
|
|
from . import _legacy # noqa: F401
|