[YoutubeDL] Add negation support for string comparisons in format selection expressions (closes #18600, closes #18805)

This commit is contained in:
Sergey M․ 2019-01-20 13:48:09 +07:00
parent 379306ef55
commit 2cc779f497
No known key found for this signature in database
GPG key ID: 2C393E0F18A9236D
3 changed files with 54 additions and 4 deletions

View file

@ -1063,21 +1063,24 @@ class YoutubeDL(object):
if not m:
STR_OPERATORS = {
'=': operator.eq,
'!=': operator.ne,
'^=': lambda attr, value: attr.startswith(value),
'$=': lambda attr, value: attr.endswith(value),
'*=': lambda attr, value: value in attr,
}
str_operator_rex = re.compile(r'''(?x)
\s*(?P<key>ext|acodec|vcodec|container|protocol|format_id)
\s*(?P<op>%s)(?P<none_inclusive>\s*\?)?
\s*(?P<negation>!\s*)?(?P<op>%s)(?P<none_inclusive>\s*\?)?
\s*(?P<value>[a-zA-Z0-9._-]+)
\s*$
''' % '|'.join(map(re.escape, STR_OPERATORS.keys())))
m = str_operator_rex.search(filter_spec)
if m:
comparison_value = m.group('value')
op = STR_OPERATORS[m.group('op')]
str_op = STR_OPERATORS[m.group('op')]
if m.group('negation'):
op = lambda attr, value: not str_op
else:
op = str_op
if not m:
raise ValueError('Invalid filter specification %r' % filter_spec)