]>
Commit | Line | Data |
---|---|---|
f4aac741 | 1 | import errno |
112da0a0 | 2 | import io |
44a5f171 | 3 | import hashlib |
112da0a0 PH |
4 | import json |
5 | import os.path | |
00fcc17a FV |
6 | import re |
7 | import types | |
dd508b7c | 8 | import sys |
112da0a0 | 9 | |
fc2c063e | 10 | import youtube_dl.extractor |
44a5f171 | 11 | from youtube_dl import YoutubeDL |
dd508b7c | 12 | from youtube_dl.utils import preferredencoding |
112da0a0 | 13 | |
112da0a0 | 14 | |
44a5f171 PH |
15 | def global_setup(): |
16 | youtube_dl._setup_opener(timeout=10) | |
17 | ||
18 | ||
19 | def get_params(override=None): | |
20 | PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), | |
21 | "parameters.json") | |
22 | with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: | |
23 | parameters = json.load(pf) | |
24 | if override: | |
25 | parameters.update(override) | |
26 | return parameters | |
112da0a0 | 27 | |
f4aac741 PH |
28 | |
29 | def try_rm(filename): | |
30 | """ Remove a file if it exists """ | |
31 | try: | |
32 | os.remove(filename) | |
33 | except OSError as ose: | |
34 | if ose.errno != errno.ENOENT: | |
35 | raise | |
36 | ||
37 | ||
dd508b7c FV |
38 | def report_warning(message): |
39 | ''' | |
40 | Print the message to stderr, it will be prefixed with 'WARNING:' | |
41 | If stderr is a tty file the 'WARNING:' will be colored | |
42 | ''' | |
43 | if sys.stderr.isatty() and os.name != 'nt': | |
44 | _msg_header = u'\033[0;33mWARNING:\033[0m' | |
45 | else: | |
46 | _msg_header = u'WARNING:' | |
47 | output = u'%s %s\n' % (_msg_header, message) | |
48 | if 'b' in getattr(sys.stderr, 'mode', '') or sys.version_info[0] < 3: | |
49 | output = output.encode(preferredencoding()) | |
50 | sys.stderr.write(output) | |
51 | ||
52 | ||
112da0a0 | 53 | class FakeYDL(YoutubeDL): |
f4d96df0 | 54 | def __init__(self, override=None): |
112da0a0 PH |
55 | # Different instances of the downloader can't share the same dictionary |
56 | # some test set the "sublang" parameter, which would break the md5 checks. | |
f4d96df0 | 57 | params = get_params(override=override) |
f4aac741 PH |
58 | super(FakeYDL, self).__init__(params) |
59 | self.result = [] | |
60 | ||
61 | def to_screen(self, s, skip_eol=None): | |
112da0a0 | 62 | print(s) |
f4aac741 | 63 | |
112da0a0 PH |
64 | def trouble(self, s, tb=None): |
65 | raise Exception(s) | |
f4aac741 | 66 | |
112da0a0 | 67 | def download(self, x): |
fc2c063e | 68 | self.result.append(x) |
f4aac741 | 69 | |
00fcc17a FV |
70 | def expect_warning(self, regex): |
71 | # Silence an expected warning matching a regex | |
72 | old_report_warning = self.report_warning | |
73 | def report_warning(self, message): | |
74 | if re.match(regex, message): return | |
75 | old_report_warning(message) | |
76 | self.report_warning = types.MethodType(report_warning, self) | |
fc2c063e PH |
77 | |
78 | def get_testcases(): | |
79 | for ie in youtube_dl.extractor.gen_extractors(): | |
80 | t = getattr(ie, '_TEST', None) | |
81 | if t: | |
82 | t['name'] = type(ie).__name__[:-len('IE')] | |
83 | yield t | |
84 | for t in getattr(ie, '_TESTS', []): | |
85 | t['name'] = type(ie).__name__[:-len('IE')] | |
86 | yield t | |
44a5f171 PH |
87 | |
88 | ||
89 | md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest() |