]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/helper.py
b1f421ac58331bad23328502f42a0e1316df853d
10 import youtube_dl
.extractor
11 from youtube_dl
import YoutubeDL
12 from youtube_dl
.utils
import preferredencoding
15 def get_params(override
=None):
16 PARAMETERS_FILE
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)),
18 with io
.open(PARAMETERS_FILE
, encoding
='utf-8') as pf
:
19 parameters
= json
.load(pf
)
21 parameters
.update(override
)
26 """ Remove a file if it exists """
29 except OSError as ose
:
30 if ose
.errno
!= errno
.ENOENT
:
34 def report_warning(message
):
36 Print the message to stderr, it will be prefixed with 'WARNING:'
37 If stderr is a tty file the 'WARNING:' will be colored
39 if sys
.stderr
.isatty() and os
.name
!= 'nt':
40 _msg_header
= u
'\033[0;33mWARNING:\033[0m'
42 _msg_header
= u
'WARNING:'
43 output
= u
'%s %s\n' % (_msg_header
, message
)
44 if 'b' in getattr(sys
.stderr
, 'mode', '') or sys
.version_info
[0] < 3:
45 output
= output
.encode(preferredencoding())
46 sys
.stderr
.write(output
)
49 class FakeYDL(YoutubeDL
):
50 def __init__(self
, override
=None):
51 # Different instances of the downloader can't share the same dictionary
52 # some test set the "sublang" parameter, which would break the md5 checks.
53 params
= get_params(override
=override
)
54 super(FakeYDL
, self
).__init
__(params
)
57 def to_screen(self
, s
, skip_eol
=None):
60 def trouble(self
, s
, tb
=None):
63 def download(self
, x
):
66 def expect_warning(self
, regex
):
67 # Silence an expected warning matching a regex
68 old_report_warning
= self
.report_warning
69 def report_warning(self
, message
):
70 if re
.match(regex
, message
): return
71 old_report_warning(message
)
72 self
.report_warning
= types
.MethodType(report_warning
, self
)
75 for ie
in youtube_dl
.extractor
.gen_extractors():
76 t
= getattr(ie
, '_TEST', None)
78 t
['name'] = type(ie
).__name
__[:-len('IE')]
80 for t
in getattr(ie
, '_TESTS', []):
81 t
['name'] = type(ie
).__name
__[:-len('IE')]
85 md5
= lambda s
: hashlib
.md5(s
.encode('utf-8')).hexdigest()