]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/compat/__init__.py
[ie/loom] Add extractors (#8686)
[yt-dlp.git] / yt_dlp / compat / __init__.py
index a0cd621104a555a9ab8caa346ee39f4c6ba8ff37..5ad5c70ecf314d8805a3ac1d3b40b38eacb33c2e 100644 (file)
@@ -1,25 +1,17 @@
-import contextlib
 import os
-import subprocess
 import sys
-import warnings
 import xml.etree.ElementTree as etree
 
-from . import re
-from ._deprecated import *  # noqa: F401, F403
 from .compat_utils import passthrough_module
 
-
-# XXX: Implement this the same way as other DeprecationWarnings without circular import
-passthrough_module(__name__, '._legacy', callback=lambda attr: warnings.warn(
-    DeprecationWarning(f'{__name__}.{attr} is deprecated'), stacklevel=2))
+passthrough_module(__name__, '._deprecated')
 del passthrough_module
 
 
 # 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):
+class compat_HTMLParseError(ValueError):
     pass
 
 
@@ -37,7 +29,8 @@ def compat_etree_fromstring(text):
 
 if compat_os_name == 'nt':
     def compat_shlex_quote(s):
-        return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"')
+        import re
+        return s if re.match(r'^[-_\w./]+$', s) else s.replace('"', '""').join('""')
 else:
     from shlex import quote as compat_shlex_quote  # noqa: F401
 
@@ -52,7 +45,7 @@ def compat_ord(c):
     def compat_realpath(path):
         while os.path.islink(path):
             path = os.path.abspath(os.readlink(path))
-        return path
+        return os.path.realpath(path)
 else:
     compat_realpath = os.path.realpath
 
@@ -76,15 +69,11 @@ def compat_expanduser(path):
     compat_expanduser = os.path.expanduser
 
 
-WINDOWS_VT_MODE = False if compat_os_name == 'nt' else None
-
-
-def windows_enable_vt_mode():  # TODO: Do this the proper way https://bugs.python.org/issue30075
-    if compat_os_name != 'nt':
-        return
-    global WINDOWS_VT_MODE
-    startupinfo = subprocess.STARTUPINFO()
-    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
-    with contextlib.suppress(Exception):
-        subprocess.Popen('', shell=True, startupinfo=startupinfo).wait()
-        WINDOWS_VT_MODE = True
+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)