1# Copyright 2016 Ryan Dellenbaugh
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8from quodlibet import _
9from quodlibet.plugins.query import QueryPlugin, QueryPluginError
10from quodlibet.query._parser import QueryParser
11
12
13class ConditionalQuery(QueryPlugin):
14    PLUGIN_ID = "conditional_query"
15    PLUGIN_NAME = _("Conditional Query")
16    PLUGIN_DESC = _("Chooses the query to match based on a condition query. "
17                  "Syntax is '@(if: condition, then, else)'.")
18    key = 'if'
19
20    def search(self, data, body):
21        if body[0].search(data):
22            return body[1].search(data)
23        else:
24            return body[2].search(data)
25
26    def parse_body(self, body):
27        if body is None:
28            raise QueryPluginError
29        parser = QueryParser(body)
30        queries = parser.match_list(parser.Query)
31        if len(queries) != 3:
32            raise QueryPluginError
33        return queries
34