]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/compat.py
debian/control: Update list of supported sites.
[youtubedl] / youtube_dl / compat.py
1 from __future__ import unicode_literals
2
3 import getpass
4 import optparse
5 import os
6 import re
7 import socket
8 import subprocess
9 import sys
10
11
12 try:
13 import urllib.request as compat_urllib_request
14 except ImportError: # Python 2
15 import urllib2 as compat_urllib_request
16
17 try:
18 import urllib.error as compat_urllib_error
19 except ImportError: # Python 2
20 import urllib2 as compat_urllib_error
21
22 try:
23 import urllib.parse as compat_urllib_parse
24 except ImportError: # Python 2
25 import urllib as compat_urllib_parse
26
27 try:
28 from urllib.parse import urlparse as compat_urllib_parse_urlparse
29 except ImportError: # Python 2
30 from urlparse import urlparse as compat_urllib_parse_urlparse
31
32 try:
33 import urllib.parse as compat_urlparse
34 except ImportError: # Python 2
35 import urlparse as compat_urlparse
36
37 try:
38 import http.cookiejar as compat_cookiejar
39 except ImportError: # Python 2
40 import cookielib as compat_cookiejar
41
42 try:
43 import html.entities as compat_html_entities
44 except ImportError: # Python 2
45 import htmlentitydefs as compat_html_entities
46
47 try:
48 import html.parser as compat_html_parser
49 except ImportError: # Python 2
50 import HTMLParser as compat_html_parser
51
52 try:
53 import http.client as compat_http_client
54 except ImportError: # Python 2
55 import httplib as compat_http_client
56
57 try:
58 from urllib.error import HTTPError as compat_HTTPError
59 except ImportError: # Python 2
60 from urllib2 import HTTPError as compat_HTTPError
61
62 try:
63 from urllib.request import urlretrieve as compat_urlretrieve
64 except ImportError: # Python 2
65 from urllib import urlretrieve as compat_urlretrieve
66
67
68 try:
69 from subprocess import DEVNULL
70 compat_subprocess_get_DEVNULL = lambda: DEVNULL
71 except ImportError:
72 compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w')
73
74 try:
75 import http.server as compat_http_server
76 except ImportError:
77 import BaseHTTPServer as compat_http_server
78
79 try:
80 from urllib.parse import unquote as compat_urllib_parse_unquote
81 except ImportError:
82 def compat_urllib_parse_unquote(string, encoding='utf-8', errors='replace'):
83 if string == '':
84 return string
85 res = string.split('%')
86 if len(res) == 1:
87 return string
88 if encoding is None:
89 encoding = 'utf-8'
90 if errors is None:
91 errors = 'replace'
92 # pct_sequence: contiguous sequence of percent-encoded bytes, decoded
93 pct_sequence = b''
94 string = res[0]
95 for item in res[1:]:
96 try:
97 if not item:
98 raise ValueError
99 pct_sequence += item[:2].decode('hex')
100 rest = item[2:]
101 if not rest:
102 # This segment was just a single percent-encoded character.
103 # May be part of a sequence of code units, so delay decoding.
104 # (Stored in pct_sequence).
105 continue
106 except ValueError:
107 rest = '%' + item
108 # Encountered non-percent-encoded characters. Flush the current
109 # pct_sequence.
110 string += pct_sequence.decode(encoding, errors) + rest
111 pct_sequence = b''
112 if pct_sequence:
113 # Flush the final pct_sequence
114 string += pct_sequence.decode(encoding, errors)
115 return string
116
117 try:
118 compat_str = unicode # Python 2
119 except NameError:
120 compat_str = str
121
122 try:
123 compat_basestring = basestring # Python 2
124 except NameError:
125 compat_basestring = str
126
127 try:
128 compat_chr = unichr # Python 2
129 except NameError:
130 compat_chr = chr
131
132 try:
133 from xml.etree.ElementTree import ParseError as compat_xml_parse_error
134 except ImportError: # Python 2.6
135 from xml.parsers.expat import ExpatError as compat_xml_parse_error
136
137
138 try:
139 from urllib.parse import parse_qs as compat_parse_qs
140 except ImportError: # Python 2
141 # HACK: The following is the correct parse_qs implementation from cpython 3's stdlib.
142 # Python 2's version is apparently totally broken
143
144 def _parse_qsl(qs, keep_blank_values=False, strict_parsing=False,
145 encoding='utf-8', errors='replace'):
146 qs, _coerce_result = qs, compat_str
147 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
148 r = []
149 for name_value in pairs:
150 if not name_value and not strict_parsing:
151 continue
152 nv = name_value.split('=', 1)
153 if len(nv) != 2:
154 if strict_parsing:
155 raise ValueError("bad query field: %r" % (name_value,))
156 # Handle case of a control-name with no equal sign
157 if keep_blank_values:
158 nv.append('')
159 else:
160 continue
161 if len(nv[1]) or keep_blank_values:
162 name = nv[0].replace('+', ' ')
163 name = compat_urllib_parse_unquote(
164 name, encoding=encoding, errors=errors)
165 name = _coerce_result(name)
166 value = nv[1].replace('+', ' ')
167 value = compat_urllib_parse_unquote(
168 value, encoding=encoding, errors=errors)
169 value = _coerce_result(value)
170 r.append((name, value))
171 return r
172
173 def compat_parse_qs(qs, keep_blank_values=False, strict_parsing=False,
174 encoding='utf-8', errors='replace'):
175 parsed_result = {}
176 pairs = _parse_qsl(qs, keep_blank_values, strict_parsing,
177 encoding=encoding, errors=errors)
178 for name, value in pairs:
179 if name in parsed_result:
180 parsed_result[name].append(value)
181 else:
182 parsed_result[name] = [value]
183 return parsed_result
184
185 try:
186 from shlex import quote as shlex_quote
187 except ImportError: # Python < 3.3
188 def shlex_quote(s):
189 if re.match(r'^[-_\w./]+$', s):
190 return s
191 else:
192 return "'" + s.replace("'", "'\"'\"'") + "'"
193
194
195 def compat_ord(c):
196 if type(c) is int:
197 return c
198 else:
199 return ord(c)
200
201
202 if sys.version_info >= (3, 0):
203 compat_getenv = os.getenv
204 compat_expanduser = os.path.expanduser
205 else:
206 # Environment variables should be decoded with filesystem encoding.
207 # Otherwise it will fail if any non-ASCII characters present (see #3854 #3217 #2918)
208
209 def compat_getenv(key, default=None):
210 from .utils import get_filesystem_encoding
211 env = os.getenv(key, default)
212 if env:
213 env = env.decode(get_filesystem_encoding())
214 return env
215
216 # HACK: The default implementations of os.path.expanduser from cpython do not decode
217 # environment variables with filesystem encoding. We will work around this by
218 # providing adjusted implementations.
219 # The following are os.path.expanduser implementations from cpython 2.7.8 stdlib
220 # for different platforms with correct environment variables decoding.
221
222 if os.name == 'posix':
223 def compat_expanduser(path):
224 """Expand ~ and ~user constructions. If user or $HOME is unknown,
225 do nothing."""
226 if not path.startswith('~'):
227 return path
228 i = path.find('/', 1)
229 if i < 0:
230 i = len(path)
231 if i == 1:
232 if 'HOME' not in os.environ:
233 import pwd
234 userhome = pwd.getpwuid(os.getuid()).pw_dir
235 else:
236 userhome = compat_getenv('HOME')
237 else:
238 import pwd
239 try:
240 pwent = pwd.getpwnam(path[1:i])
241 except KeyError:
242 return path
243 userhome = pwent.pw_dir
244 userhome = userhome.rstrip('/')
245 return (userhome + path[i:]) or '/'
246 elif os.name == 'nt' or os.name == 'ce':
247 def compat_expanduser(path):
248 """Expand ~ and ~user constructs.
249
250 If user or $HOME is unknown, do nothing."""
251 if path[:1] != '~':
252 return path
253 i, n = 1, len(path)
254 while i < n and path[i] not in '/\\':
255 i = i + 1
256
257 if 'HOME' in os.environ:
258 userhome = compat_getenv('HOME')
259 elif 'USERPROFILE' in os.environ:
260 userhome = compat_getenv('USERPROFILE')
261 elif 'HOMEPATH' not in os.environ:
262 return path
263 else:
264 try:
265 drive = compat_getenv('HOMEDRIVE')
266 except KeyError:
267 drive = ''
268 userhome = os.path.join(drive, compat_getenv('HOMEPATH'))
269
270 if i != 1: # ~user
271 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
272
273 return userhome + path[i:]
274 else:
275 compat_expanduser = os.path.expanduser
276
277
278 if sys.version_info < (3, 0):
279 def compat_print(s):
280 from .utils import preferredencoding
281 print(s.encode(preferredencoding(), 'xmlcharrefreplace'))
282 else:
283 def compat_print(s):
284 assert isinstance(s, compat_str)
285 print(s)
286
287
288 try:
289 subprocess_check_output = subprocess.check_output
290 except AttributeError:
291 def subprocess_check_output(*args, **kwargs):
292 assert 'input' not in kwargs
293 p = subprocess.Popen(*args, stdout=subprocess.PIPE, **kwargs)
294 output, _ = p.communicate()
295 ret = p.poll()
296 if ret:
297 raise subprocess.CalledProcessError(ret, p.args, output=output)
298 return output
299
300 if sys.version_info < (3, 0) and sys.platform == 'win32':
301 def compat_getpass(prompt, *args, **kwargs):
302 if isinstance(prompt, compat_str):
303 from .utils import preferredencoding
304 prompt = prompt.encode(preferredencoding())
305 return getpass.getpass(prompt, *args, **kwargs)
306 else:
307 compat_getpass = getpass.getpass
308
309 # Old 2.6 and 2.7 releases require kwargs to be bytes
310 try:
311 def _testfunc(x):
312 pass
313 _testfunc(**{'x': 0})
314 except TypeError:
315 def compat_kwargs(kwargs):
316 return dict((bytes(k), v) for k, v in kwargs.items())
317 else:
318 compat_kwargs = lambda kwargs: kwargs
319
320
321 if sys.version_info < (2, 7):
322 def compat_socket_create_connection(address, timeout, source_address=None):
323 host, port = address
324 err = None
325 for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
326 af, socktype, proto, canonname, sa = res
327 sock = None
328 try:
329 sock = socket.socket(af, socktype, proto)
330 sock.settimeout(timeout)
331 if source_address:
332 sock.bind(source_address)
333 sock.connect(sa)
334 return sock
335 except socket.error as _:
336 err = _
337 if sock is not None:
338 sock.close()
339 if err is not None:
340 raise err
341 else:
342 raise socket.error("getaddrinfo returns an empty list")
343 else:
344 compat_socket_create_connection = socket.create_connection
345
346
347 # Fix https://github.com/rg3/youtube-dl/issues/4223
348 # See http://bugs.python.org/issue9161 for what is broken
349 def workaround_optparse_bug9161():
350 op = optparse.OptionParser()
351 og = optparse.OptionGroup(op, 'foo')
352 try:
353 og.add_option('-t')
354 except TypeError:
355 real_add_option = optparse.OptionGroup.add_option
356
357 def _compat_add_option(self, *args, **kwargs):
358 enc = lambda v: (
359 v.encode('ascii', 'replace') if isinstance(v, compat_str)
360 else v)
361 bargs = [enc(a) for a in args]
362 bkwargs = dict(
363 (k, enc(v)) for k, v in kwargs.items())
364 return real_add_option(self, *bargs, **bkwargs)
365 optparse.OptionGroup.add_option = _compat_add_option
366
367
368 __all__ = [
369 'compat_HTTPError',
370 'compat_basestring',
371 'compat_chr',
372 'compat_cookiejar',
373 'compat_expanduser',
374 'compat_getenv',
375 'compat_getpass',
376 'compat_html_entities',
377 'compat_html_parser',
378 'compat_http_client',
379 'compat_http_server',
380 'compat_kwargs',
381 'compat_ord',
382 'compat_parse_qs',
383 'compat_print',
384 'compat_socket_create_connection',
385 'compat_str',
386 'compat_subprocess_get_DEVNULL',
387 'compat_urllib_error',
388 'compat_urllib_parse',
389 'compat_urllib_parse_unquote',
390 'compat_urllib_parse_urlparse',
391 'compat_urllib_request',
392 'compat_urlparse',
393 'compat_urlretrieve',
394 'compat_xml_parse_error',
395 'shlex_quote',
396 'subprocess_check_output',
397 'workaround_optparse_bug9161',
398 ]