2022-02-04 14:37:02 +01:00
|
|
|
import os
|
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
|
2022-04-24 18:28:18 +02:00
|
|
|
from .compat_utils import passthrough_module
|
|
|
|
|
2023-07-22 14:26:53 +02:00
|
|
|
passthrough_module(__name__, '._deprecated')
|
|
|
|
del passthrough_module
|
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
|
2022-11-06 19:30:59 +01:00
|
|
|
class compat_HTMLParseError(ValueError):
|
2022-02-04 14:37:02 +01:00
|
|
|
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()))
|
|
|
|
|
|
|
|
|
|
|
|
def compat_ord(c):
|
|
|
|
return c if isinstance(c, int) else ord(c)
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2024-11-17 00:24:11 +01:00
|
|
|
if os.name in ('nt', 'ce'):
|
2022-02-04 14:37:02 +01:00
|
|
|
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
|
2023-07-15 12:25:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def urllib_req_to_req(urllib_request):
|
|
|
|
"""Convert urllib Request to a networking Request"""
|
|
|
|
from ..networking import Request
|
|
|
|
from ..utils.networking import HTTPHeaderDict
|
|
|
|
return Request(
|
|
|
|
urllib_request.get_full_url(), data=urllib_request.data, method=urllib_request.get_method(),
|
|
|
|
headers=HTTPHeaderDict(urllib_request.headers, urllib_request.unredirected_hdrs),
|
|
|
|
extensions={'timeout': urllib_request.timeout} if hasattr(urllib_request, 'timeout') else None)
|