'Returns a function to filter the formats according to the filter_spec'

def _build_format_filter(self, filter_spec):

    OPERATORS = {u'<': operator.lt, u'<=': operator.le, u'>': operator.gt, u'>=': operator.ge, u'=': operator.eq, u'!=': operator.ne}
    operator_rex = re.compile((u'(?x)\\s*\n                                                (?P<key>width|height|tbr|abr|vbr|asr|filesize|fps)\n                                                \\s*(?P<op>%s)(?P<none_inclusive>\\s*\\?)?\\s*\n                                                (?P<value>[0-9.]+(?:[kKmMgGtTpPeEzZyY]i?[Bb]?)?)\n                                                $\n                                                ' % u'|'.join(map(re.escape, OPERATORS.keys()))))
    m = operator_rex.search(filter_spec)
    if m:
        try:
            comparison_value = int(m.group(u'value'))
        except ValueError:
            comparison_value = parse_filesize(m.group(u'value'))
            if (comparison_value is None):
                comparison_value = parse_filesize((m.group(u'value') + u'B'))
            if (comparison_value is None):
                raise ValueError((u'Invalid    value    %r    in    format    specification    %r' % (m.group(u'value'), filter_spec)))
        op = OPERATORS[m.group(u'op')]
    if (not m):
        STR_OPERATORS = {u'=': operator.eq, u'!=': operator.ne, u'^=': (lambda attr, value: attr.startswith(value)), u'$=': (lambda attr, value: attr.endswith(value)), u'*=': (lambda attr, value: (value in attr))}
        str_operator_rex = re.compile((u'(?x)\n                                                                \\s*(?P<key>ext|acodec|vcodec|container|protocol|format_id)\n                                                                \\s*(?P<op>%s)(?P<none_inclusive>\\s*\\?)?\n                                                                \\s*(?P<value>[a-zA-Z0-9._-]+)\n                                                                \\s*$\n                                                                ' % u'|'.join(map(re.escape, STR_OPERATORS.keys()))))
        m = str_operator_rex.search(filter_spec)
        if m:
            comparison_value = m.group(u'value')
            op = STR_OPERATORS[m.group(u'op')]
    if (not m):
        raise ValueError((u'Invalid    filter    specification    %r' % filter_spec))
    def _filter(f):
        actual_value = f.get(m.group(u'key'))
        if (actual_value is None):
            return m.group(u'none_inclusive')
        return op(actual_value, comparison_value)
    return _filter
