3 import xml
.etree
.ElementTree
7 from yt_dlp
.utils
import (
14 from yt_dlp
.utils
.traversal
import (
27 {'index': 0, 'url': 'https://www.example.com/0'},
28 {'index': 1, 'url': 'https://www.example.com/1'},
39 def test_traversal_base(self
):
40 assert traverse_obj(_TEST_DATA
, ('str',)) == 'str', \
42 assert traverse_obj(_TEST_DATA
, ['str']) == 'str', \
44 assert traverse_obj(_TEST_DATA
, (value
for value
in ('str',))) == 'str', \
46 assert traverse_obj(_TEST_DATA
, 'str') == 'str', \
47 'single items should be treated as a path'
48 assert traverse_obj(_TEST_DATA
, 100) == 100, \
50 assert traverse_obj(_TEST_DATA
, 1.2) == 1.2, \
52 assert traverse_obj(_TEST_DATA
, None) == _TEST_DATA
, \
53 '`None` should not perform any modification'
55 def test_traversal_ellipsis(self
):
56 assert traverse_obj(_TEST_DATA
, ...) == [x
for x
in _TEST_DATA
.values() if x
not in (None, {})], \
57 '`...` should give all non discarded values'
58 assert traverse_obj(_TEST_DATA
, ('urls', 0, ...)) == list(_TEST_DATA
['urls'][0].values()), \
59 '`...` selection for dicts should select all values'
60 assert traverse_obj(_TEST_DATA
, (..., ..., 'url')) == ['https://www.example.com/0', 'https://www.example.com/1'], \
61 'nested `...` queries should work'
62 assert traverse_obj(_TEST_DATA
, (..., ..., 'index')) == list(range(4)), \
63 '`...` query result should be flattened'
64 assert traverse_obj(iter(range(4)), ...) == list(range(4)), \
65 '`...` should accept iterables'
67 def test_traversal_function(self
):
68 filter_func
= lambda x
, y
: x
== 'urls' and isinstance(y
, list)
69 assert traverse_obj(_TEST_DATA
, filter_func
) == [_TEST_DATA
['urls']], \
70 'function as query key should perform a filter based on (key, value)'
71 assert traverse_obj(_TEST_DATA
, lambda _
, x
: isinstance(x
[0], str)) == ['str'], \
72 'exceptions in the query function should be catched'
73 assert traverse_obj(iter(range(4)), lambda _
, x
: x
% 2 == 0) == [0, 2], \
74 'function key should accept iterables'
75 # Wrong function signature should raise (debug mode)
76 with pytest
.raises(Exception):
77 traverse_obj(_TEST_DATA
, lambda a
: ...)
78 with pytest
.raises(Exception):
79 traverse_obj(_TEST_DATA
, lambda a
, b
, c
: ...)
81 def test_traversal_set(self
):
82 # transformation/type, like `expected_type`
83 assert traverse_obj(_TEST_DATA
, (..., {str.upper
})) == ['STR'], \
84 'Function in set should be a transformation'
85 assert traverse_obj(_TEST_DATA
, (..., {str}
)) == ['str'], \
86 'Type in set should be a type filter'
87 assert traverse_obj(_TEST_DATA
, (..., {str, int})) == [100, 'str'], \
88 'Multiple types in set should be a type filter'
89 assert traverse_obj(_TEST_DATA
, {dict}
) == _TEST_DATA
, \
90 'A single set should be wrapped into a path'
91 assert traverse_obj(_TEST_DATA
, (..., {str.upper
})) == ['STR'], \
92 'Transformation function should not raise'
93 expected
= [x
for x
in map(str_or_none
, _TEST_DATA
.values()) if x
is not None]
94 assert traverse_obj(_TEST_DATA
, (..., {str_or_none}
)) == expected
, \
95 'Function in set should be a transformation'
96 assert traverse_obj(_TEST_DATA
, ('fail', {lambda _
: 'const'})) == 'const', \
97 'Function in set should always be called'
98 # Sets with length < 1 or > 1 not including only types should raise
99 with pytest
.raises(Exception):
100 traverse_obj(_TEST_DATA
, set())
101 with pytest
.raises(Exception):
102 traverse_obj(_TEST_DATA
, {str.upper
, str})
104 def test_traversal_slice(self
):
105 _SLICE_DATA
= [0, 1, 2, 3, 4]
107 assert traverse_obj(_TEST_DATA
, ('dict', slice(1))) is None, \
108 'slice on a dictionary should not throw'
109 assert traverse_obj(_SLICE_DATA
, slice(1)) == _SLICE_DATA
[:1], \
110 'slice key should apply slice to sequence'
111 assert traverse_obj(_SLICE_DATA
, slice(1, 2)) == _SLICE_DATA
[1:2], \
112 'slice key should apply slice to sequence'
113 assert traverse_obj(_SLICE_DATA
, slice(1, 4, 2)) == _SLICE_DATA
[1:4:2], \
114 'slice key should apply slice to sequence'
116 def test_traversal_alternatives(self
):
117 assert traverse_obj(_TEST_DATA
, 'fail', 'str') == 'str', \
118 'multiple `paths` should be treated as alternative paths'
119 assert traverse_obj(_TEST_DATA
, 'str', 100) == 'str', \
120 'alternatives should exit early'
121 assert traverse_obj(_TEST_DATA
, 'fail', 'fail') is None, \
122 'alternatives should return `default` if exhausted'
123 assert traverse_obj(_TEST_DATA
, (..., 'fail'), 100) == 100, \
124 'alternatives should track their own branching return'
125 assert traverse_obj(_TEST_DATA
, ('dict', ...), ('data', ...)) == list(_TEST_DATA
['data']), \
126 'alternatives on empty objects should search further'
128 def test_traversal_branching_nesting(self
):
129 assert traverse_obj(_TEST_DATA
, ('urls', (3, 0), 'url')) == ['https://www.example.com/0'], \
130 'tuple as key should be treated as branches'
131 assert traverse_obj(_TEST_DATA
, ('urls', [3, 0], 'url')) == ['https://www.example.com/0'], \
132 'list as key should be treated as branches'
133 assert traverse_obj(_TEST_DATA
, ('urls', ((1, 'fail'), (0, 'url')))) == ['https://www.example.com/0'], \
134 'double nesting in path should be treated as paths'
135 assert traverse_obj(['0', [1, 2]], [(0, 1), 0]) == [1], \
136 'do not fail early on branching'
137 expected
= ['https://www.example.com/0', 'https://www.example.com/1']
138 assert traverse_obj(_TEST_DATA
, ('urls', ((0, ('fail', 'url')), (1, 'url')))) == expected
, \
139 'tripple nesting in path should be treated as branches'
140 assert traverse_obj(_TEST_DATA
, ('urls', ('fail', (..., 'url')))) == expected
, \
141 'ellipsis as branch path start gets flattened'
143 def test_traversal_dict(self
):
144 assert traverse_obj(_TEST_DATA
, {0: 100, 1: 1.2}) == {0: 100, 1: 1.2}, \
145 'dict key should result in a dict with the same keys'
146 expected
= {0: 'https://www.example.com/0'}
147 assert traverse_obj(_TEST_DATA
, {0: ('urls', 0, 'url')}) == expected
, \
148 'dict key should allow paths'
149 expected
= {0: ['https://www.example.com/0']}
150 assert traverse_obj(_TEST_DATA
, {0: ('urls', (3, 0), 'url')}) == expected
, \
151 'tuple in dict path should be treated as branches'
152 assert traverse_obj(_TEST_DATA
, {0: ('urls', ((1, 'fail'), (0, 'url')))}) == expected
, \
153 'double nesting in dict path should be treated as paths'
154 expected
= {0: ['https://www.example.com/1', 'https://www.example.com/0']}
155 assert traverse_obj(_TEST_DATA
, {0: ('urls', ((1, ('fail', 'url')), (0, 'url')))}) == expected
, \
156 'tripple nesting in dict path should be treated as branches'
157 assert traverse_obj(_TEST_DATA
, {0: 'fail'}) == {}, \
158 'remove `None` values when top level dict key fails'
159 assert traverse_obj(_TEST_DATA
, {0: 'fail'}, default
=...) == {0: ...}, \
160 'use `default` if key fails and `default`'
161 assert traverse_obj(_TEST_DATA
, {0: 'dict'}) == {}, \
162 'remove empty values when dict key'
163 assert traverse_obj(_TEST_DATA
, {0: 'dict'}, default
=...) == {0: ...}, \
164 'use `default` when dict key and `default`'
165 assert traverse_obj(_TEST_DATA
, {0: {0: 'fail'}}) == {}, \
166 'remove empty values when nested dict key fails'
167 assert traverse_obj(None, {0: 'fail'}) == {}, \
168 'default to dict if pruned'
169 assert traverse_obj(None, {0: 'fail'}, default
=...) == {0: ...}, \
170 'default to dict if pruned and default is given'
171 assert traverse_obj(_TEST_DATA
, {0: {0: 'fail'}}, default
=...) == {0: {0: ...}}, \
172 'use nested `default` when nested dict key fails and `default`'
173 assert traverse_obj(_TEST_DATA
, {0: ('dict', ...)}) == {}, \
174 'remove key if branch in dict key not successful'
176 def test_traversal_default(self
):
177 _DEFAULT_DATA
= {'None': None, 'int': 0, 'list': []}
179 assert traverse_obj(_DEFAULT_DATA
, 'fail') is None, \
180 'default value should be `None`'
181 assert traverse_obj(_DEFAULT_DATA
, 'fail', 'fail', default
=...) == ..., \
182 'chained fails should result in default'
183 assert traverse_obj(_DEFAULT_DATA
, 'None', 'int') == 0, \
184 'should not short cirquit on `None`'
185 assert traverse_obj(_DEFAULT_DATA
, 'fail', default
=1) == 1, \
186 'invalid dict key should result in `default`'
187 assert traverse_obj(_DEFAULT_DATA
, 'None', default
=1) == 1, \
188 '`None` is a deliberate sentinel and should become `default`'
189 assert traverse_obj(_DEFAULT_DATA
, ('list', 10)) is None, \
190 '`IndexError` should result in `default`'
191 assert traverse_obj(_DEFAULT_DATA
, (..., 'fail'), default
=1) == 1, \
192 'if branched but not successful return `default` if defined, not `[]`'
193 assert traverse_obj(_DEFAULT_DATA
, (..., 'fail'), default
=None) is None, \
194 'if branched but not successful return `default` even if `default` is `None`'
195 assert traverse_obj(_DEFAULT_DATA
, (..., 'fail')) == [], \
196 'if branched but not successful return `[]`, not `default`'
197 assert traverse_obj(_DEFAULT_DATA
, ('list', ...)) == [], \
198 'if branched but object is empty return `[]`, not `default`'
199 assert traverse_obj(None, ...) == [], \
200 'if branched but object is `None` return `[]`, not `default`'
201 assert traverse_obj({0: None}, (0, ...)) == [], \
202 'if branched but state is `None` return `[]`, not `default`'
204 @pytest.mark
.parametrize('path', [
207 100 * ('fail',) + (...,),
208 (...,) + 100 * ('fail',),
210 def test_traversal_branching(self
, path
):
211 assert traverse_obj({}, path
) == [], \
212 'if branched but state is `None`, return `[]` (not `default`)'
213 assert traverse_obj({}, 'fail', path
) == [], \
214 'if branching in last alternative and previous did not match, return `[]` (not `default`)'
215 assert traverse_obj({0: 'x'}, 0, path
) == 'x', \
216 'if branching in last alternative and previous did match, return single value'
217 assert traverse_obj({0: 'x'}, path
, 0) == 'x', \
218 'if branching in first alternative and non-branching path does match, return single value'
219 assert traverse_obj({}, path
, 'fail') is None, \
220 'if branching in first alternative and non-branching path does not match, return `default`'
222 def test_traversal_expected_type(self
):
223 _EXPECTED_TYPE_DATA
= {'str': 'str', 'int': 0}
225 assert traverse_obj(_EXPECTED_TYPE_DATA
, 'str', expected_type
=str) == 'str', \
226 'accept matching `expected_type` type'
227 assert traverse_obj(_EXPECTED_TYPE_DATA
, 'str', expected_type
=int) is None, \
228 'reject non matching `expected_type` type'
229 assert traverse_obj(_EXPECTED_TYPE_DATA
, 'int', expected_type
=lambda x
: str(x
)) == '0', \
230 'transform type using type function'
231 assert traverse_obj(_EXPECTED_TYPE_DATA
, 'str', expected_type
=lambda _
: 1 / 0) is None, \
232 'wrap expected_type fuction in try_call'
233 assert traverse_obj(_EXPECTED_TYPE_DATA
, ..., expected_type
=str) == ['str'], \
234 'eliminate items that expected_type fails on'
235 assert traverse_obj(_TEST_DATA
, {0: 100, 1: 1.2}, expected_type
=int) == {0: 100}, \
236 'type as expected_type should filter dict values'
237 assert traverse_obj(_TEST_DATA
, {0: 100, 1: 1.2, 2: 'None'}, expected_type
=str_or_none
) == {0: '100', 1: '1.2'}, \
238 'function as expected_type should transform dict values'
239 assert traverse_obj(_TEST_DATA
, ({0: 1.2}, 0, {int_or_none}
), expected_type
=int) == 1, \
240 'expected_type should not filter non final dict values'
241 assert traverse_obj(_TEST_DATA
, {0: {0: 100, 1: 'str'}}, expected_type
=int) == {0: {0: 100}}, \
242 'expected_type should transform deep dict values'
243 assert traverse_obj(_TEST_DATA
, [({0: '...'}, {0: '...'})], expected_type
=type(...)) == [{0: ...}, {0: ...}], \
244 'expected_type should transform branched dict values'
245 assert traverse_obj({1: {3: 4}}, [(1, 2), 3], expected_type
=int) == [4], \
246 'expected_type regression for type matching in tuple branching'
247 assert traverse_obj(_TEST_DATA
, ['data', ...], expected_type
=int) == [], \
248 'expected_type regression for type matching in dict result'
250 def test_traversal_get_all(self
):
251 _GET_ALL_DATA
= {'key': [0, 1, 2]}
253 assert traverse_obj(_GET_ALL_DATA
, ('key', ...), get_all
=False) == 0, \
254 'if not `get_all`, return only first matching value'
255 assert traverse_obj(_GET_ALL_DATA
, ..., get_all
=False) == [0, 1, 2], \
256 'do not overflatten if not `get_all`'
258 def test_traversal_casesense(self
):
263 0: {'KeY': 'value2'},
267 assert traverse_obj(_CASESENSE_DATA
, 'key') is None, \
268 'dict keys should be case sensitive unless `casesense`'
269 assert traverse_obj(_CASESENSE_DATA
, 'keY', casesense
=False) == 'value0', \
270 'allow non matching key case if `casesense`'
271 assert traverse_obj(_CASESENSE_DATA
, [0, ('keY',)], casesense
=False) == ['value1'], \
272 'allow non matching key case in branch if `casesense`'
273 assert traverse_obj(_CASESENSE_DATA
, [0, ([0, 'keY'],)], casesense
=False) == ['value2'], \
274 'allow non matching key case in branch path if `casesense`'
276 def test_traversal_traverse_string(self
):
277 _TRAVERSE_STRING_DATA
= {'str': 'str', 1.2: 1.2}
279 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', 0)) is None, \
280 'do not traverse into string if not `traverse_string`'
281 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', 0), traverse_string
=True) == 's', \
282 'traverse into string if `traverse_string`'
283 assert traverse_obj(_TRAVERSE_STRING_DATA
, (1.2, 1), traverse_string
=True) == '.', \
284 'traverse into converted data if `traverse_string`'
285 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', ...), traverse_string
=True) == 'str', \
286 '`...` should result in string (same value) if `traverse_string`'
287 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', slice(0, None, 2)), traverse_string
=True) == 'sr', \
288 '`slice` should result in string if `traverse_string`'
289 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', lambda i
, v
: i
or v
== 's'), traverse_string
=True) == 'str', \
290 'function should result in string if `traverse_string`'
291 assert traverse_obj(_TRAVERSE_STRING_DATA
, ('str', (0, 2)), traverse_string
=True) == ['s', 'r'], \
292 'branching should result in list if `traverse_string`'
293 assert traverse_obj({}, (0, ...), traverse_string
=True) == [], \
294 'branching should result in list if `traverse_string`'
295 assert traverse_obj({}, (0, lambda x
, y
: True), traverse_string
=True) == [], \
296 'branching should result in list if `traverse_string`'
297 assert traverse_obj({}, (0, slice(1)), traverse_string
=True) == [], \
298 'branching should result in list if `traverse_string`'
300 def test_traversal_re(self
):
301 mobj
= re
.fullmatch(r
'0(12)(?P<group>3)(4)?', '0123')
302 assert traverse_obj(mobj
, ...) == [x
for x
in mobj
.groups() if x
is not None], \
303 '`...` on a `re.Match` should give its `groups()`'
304 assert traverse_obj(mobj
, lambda k
, _
: k
in (0, 2)) == ['0123', '3'], \
305 'function on a `re.Match` should give groupno, value starting at 0'
306 assert traverse_obj(mobj
, 'group') == '3', \
307 'str key on a `re.Match` should give group with that name'
308 assert traverse_obj(mobj
, 2) == '3', \
309 'int key on a `re.Match` should give group with that name'
310 assert traverse_obj(mobj
, 'gRoUp', casesense
=False) == '3', \
311 'str key on a `re.Match` should respect casesense'
312 assert traverse_obj(mobj
, 'fail') is None, \
313 'failing str key on a `re.Match` should return `default`'
314 assert traverse_obj(mobj
, 'gRoUpS', casesense
=False) is None, \
315 'failing str key on a `re.Match` should return `default`'
316 assert traverse_obj(mobj
, 8) is None, \
317 'failing int key on a `re.Match` should return `default`'
318 assert traverse_obj(mobj
, lambda k
, _
: k
in (0, 'group')) == ['0123', '3'], \
319 'function on a `re.Match` should give group name as well'
321 def test_traversal_xml_etree(self
):
322 etree
= xml
.etree
.ElementTree
.fromstring('''<?xml version="1.0"?>
324 <country name="Liechtenstein">
327 <gdppc>141100</gdppc>
328 <neighbor name="Austria" direction="E"/>
329 <neighbor name="Switzerland" direction="W"/>
331 <country name="Singapore">
335 <neighbor name="Malaysia" direction="N"/>
337 <country name="Panama">
341 <neighbor name="Costa Rica" direction="W"/>
342 <neighbor name="Colombia" direction="E"/>
345 assert traverse_obj(etree
, '') == etree
, \
346 'empty str key should return the element itself'
347 assert traverse_obj(etree
, 'country') == list(etree
), \
348 'str key should lead all children with that tag name'
349 assert traverse_obj(etree
, ...) == list(etree
), \
350 '`...` as key should return all children'
351 assert traverse_obj(etree
, lambda _
, x
: x
[0].text
== '4') == [etree
[1]], \
352 'function as key should get element as value'
353 assert traverse_obj(etree
, lambda i
, _
: i
== 1) == [etree
[1]], \
354 'function as key should get index as key'
355 assert traverse_obj(etree
, 0) == etree
[0], \
356 'int key should return the nth child'
357 expected
= ['Austria', 'Switzerland', 'Malaysia', 'Costa Rica', 'Colombia']
358 assert traverse_obj(etree
, './/neighbor/@name') == expected
, \
359 '`@<attribute>` at end of path should give that attribute'
360 assert traverse_obj(etree
, '//neighbor/@fail') == [None, None, None, None, None], \
361 '`@<nonexistant>` at end of path should give `None`'
362 assert traverse_obj(etree
, ('//neighbor/@', 2)) == {'name': 'Malaysia', 'direction': 'N'}, \
363 '`@` should give the full attribute dict'
364 assert traverse_obj(etree
, '//year/text()') == ['2008', '2011', '2011'], \
365 '`text()` at end of path should give the inner text'
366 assert traverse_obj(etree
, '//*[@direction]/@direction') == ['E', 'W', 'N', 'W', 'E'], \
367 'full Python xpath features should be supported'
368 assert traverse_obj(etree
, (0, '@name')) == 'Liechtenstein', \
369 'special transformations should act on current element'
370 assert traverse_obj(etree
, ('country', 0, ..., 'text()', {int_or_none}
)) == [1, 2008, 141100], \
371 'special transformations should act on current element'
373 def test_traversal_unbranching(self
):
374 assert traverse_obj(_TEST_DATA
, [(100, 1.2), all
]) == [100, 1.2], \
375 '`all` should give all results as list'
376 assert traverse_obj(_TEST_DATA
, [(100, 1.2), any
]) == 100, \
377 '`any` should give the first result'
378 assert traverse_obj(_TEST_DATA
, [100, all
]) == [100], \
379 '`all` should give list if non branching'
380 assert traverse_obj(_TEST_DATA
, [100, any
]) == 100, \
381 '`any` should give single item if non branching'
382 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 100), all
]) == [100], \
383 '`all` should filter `None` and empty dict'
384 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 100), any
]) == 100, \
385 '`any` should filter `None` and empty dict'
386 assert traverse_obj(_TEST_DATA
, [{
387 'all': [('dict', 'None', 100, 1.2), all
],
388 'any': [('dict', 'None', 100, 1.2), any
],
389 }]) == {'all': [100, 1.2], 'any': 100}, \
390 '`all`/`any` should apply to each dict path separately'
391 assert traverse_obj(_TEST_DATA
, [{
392 'all': [('dict', 'None', 100, 1.2), all
],
393 'any': [('dict', 'None', 100, 1.2), any
],
394 }], get_all
=False) == {'all': [100, 1.2], 'any': 100}, \
395 '`all`/`any` should apply to dict regardless of `get_all`'
396 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 100, 1.2), all
, {float}
]) is None, \
397 '`all` should reset branching status'
398 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 100, 1.2), any
, {float}
]) is None, \
399 '`any` should reset branching status'
400 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 100, 1.2), all
, ..., {float}
]) == [1.2], \
401 '`all` should allow further branching'
402 assert traverse_obj(_TEST_DATA
, [('dict', 'None', 'urls', 'data'), any
, ..., 'index']) == [0, 1], \
403 '`any` should allow further branching'
405 def test_traversal_morsel(self
):
417 morsel
= http
.cookies
.Morsel()
418 morsel
.set('item_key', 'item_value', 'coded_value')
419 morsel
.update(values
)
420 values
['key'] = 'item_key'
421 values
['value'] = 'item_value'
423 for key
, value
in values
.items():
424 assert traverse_obj(morsel
, key
) == value
, \
425 'Morsel should provide access to all values'
426 assert traverse_obj(morsel
, ...) == list(values
.values()), \
427 '`...` should yield all values'
428 assert traverse_obj(morsel
, lambda k
, v
: True) == list(values
.values()), \
429 'function key should yield all values'
430 assert traverse_obj(morsel
, [(None,), any
]) == morsel
, \
431 'Morsel should not be implicitly changed to dict on usage'
433 def test_traversal_filter(self
):
434 data
= [None, False, True, 0, 1, 0.0, 1.1, '', 'str', {}, {0: 0}, [], [1]]
436 assert traverse_obj(data
, [..., filter]) == [True, 1, 1.1, 'str', {0: 0}, [1]], \
437 '`filter` should filter falsy values'
440 class TestTraversalHelpers
:
441 def test_traversal_require(self
):
442 with pytest
.raises(ExtractorError
):
443 traverse_obj(_TEST_DATA
, ['None', {require('value')}])
444 assert traverse_obj(_TEST_DATA
, ['str', {require('value')}]) == 'str', \
445 '`require` should pass through non `None` values'
447 def test_subs_list_to_dict(self
):
448 assert traverse_obj([
449 {'name': 'de', 'url': 'https://example.com/subs/de.vtt'},
450 {'name': 'en', 'url': 'https://example.com/subs/en1.ass'},
451 {'name': 'en', 'url': 'https://example.com/subs/en2.ass'},
455 }, all
, {subs_list_to_dict}
]) == {
456 'de': [{'url': 'https://example.com/subs/de.vtt'}],
458 {'url': 'https://example.com/subs/en1.ass'},
459 {'url': 'https://example.com/subs/en2.ass'},
461 }, 'function should build subtitle dict from list of subtitles'
462 assert traverse_obj([
463 {'name': 'de', 'url': 'https://example.com/subs/de.ass'},
465 {'name': 'en', 'content': 'content'},
466 {'url': 'https://example.com/subs/en'},
471 }, all
, {subs_list_to_dict}
]) == {
472 'de': [{'url': 'https://example.com/subs/de.ass'}],
473 'en': [{'data': 'content'}],
474 }, 'subs with mandatory items missing should be filtered'
475 assert traverse_obj([
476 {'url': 'https://example.com/subs/de.ass', 'name': 'de'},
477 {'url': 'https://example.com/subs/en', 'name': 'en'},
480 'ext': ['url', {lambda x
: determine_ext(x
, default_ext
=None)}],
482 }, all
, {subs_list_to_dict(ext
='ext')}]) == {
483 'de': [{'url': 'https://example.com/subs/de.ass', 'ext': 'ass'}],
484 'en': [{'url': 'https://example.com/subs/en', 'ext': 'ext'}],
485 }, '`ext` should set default ext but leave existing value untouched'
486 assert traverse_obj([
487 {'name': 'en', 'url': 'https://example.com/subs/en2', 'prio': True},
488 {'name': 'en', 'url': 'https://example.com/subs/en1', 'prio': False},
491 'quality': ['prio', {int}
],
493 }, all
, {subs_list_to_dict(ext
='ext')}]) == {'en': [
494 {'url': 'https://example.com/subs/en1', 'ext': 'ext'},
495 {'url': 'https://example.com/subs/en2', 'ext': 'ext'},
496 ]}, '`quality` key should sort subtitle list accordingly'
500 def test_dict_get(self
):
508 d
= {**FALSE_VALUES
, 'a': 42}
509 assert dict_get(d
, 'a') == 42
510 assert dict_get(d
, 'b') is None
511 assert dict_get(d
, 'b', 42) == 42
512 assert dict_get(d
, ('a',)) == 42
513 assert dict_get(d
, ('b', 'a')) == 42
514 assert dict_get(d
, ('b', 'c', 'a', 'd')) == 42
515 assert dict_get(d
, ('b', 'c')) is None
516 assert dict_get(d
, ('b', 'c'), 42) == 42
517 for key
, false_value
in FALSE_VALUES
.items():
518 assert dict_get(d
, ('b', 'c', key
)) is None
519 assert dict_get(d
, ('b', 'c', key
), skip_false_values
=False) == false_value