From a8ad7df064676b3013c84ea3011a905ea296b9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Sat, 7 Dec 2024 16:27:31 +0000 Subject: [PATCH] Use Item.field_query for queries that receive user input --- beetsplug/aura.py | 15 ++++++++------- beetsplug/bpd/__init__.py | 12 +++++++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/beetsplug/aura.py b/beetsplug/aura.py index a9b270657f..e7034c1e9e 100644 --- a/beetsplug/aura.py +++ b/beetsplug/aura.py @@ -186,7 +186,9 @@ def translate_filters(self): value = converter(value) # Add exact match query to list # Use a slow query so it works with all fields - queries.append(MatchQuery(beets_attr, value, fast=False)) + queries.append( + self.model_cls.field_query(beets_attr, value, MatchQuery) + ) # NOTE: AURA doesn't officially support multiple queries return AndQuery(queries) @@ -318,13 +320,12 @@ def all_resources(self): sort = self.translate_sorts(sort_arg) # For each sort field add a query which ensures all results # have a non-empty, non-zero value for that field. - for s in sort.sorts: - query.subqueries.append( - NotQuery( - # Match empty fields (^$) or zero fields, (^0$) - RegexpQuery(s.field, "(^$|^0$)", fast=False) - ) + query.subqueries.extend( + NotQuery( + self.model_cls.field_query(s.field, "(^$|^0$)", RegexpQuery) ) + for s in sort.sorts + ) else: sort = None # Get information from the library diff --git a/beetsplug/bpd/__init__.py b/beetsplug/bpd/__init__.py index 7b01f52c5b..9d8b4142b3 100644 --- a/beetsplug/bpd/__init__.py +++ b/beetsplug/bpd/__init__.py @@ -26,6 +26,7 @@ import time import traceback from string import Template +from typing import TYPE_CHECKING import beets import beets.ui @@ -34,6 +35,9 @@ from beets.plugins import BeetsPlugin from beets.util import bluelet +if TYPE_CHECKING: + from beets.dbcore.query import Query + PROTOCOL_VERSION = "0.16.0" BUFSIZE = 1024 @@ -1402,7 +1406,7 @@ def _metadata_query(self, query_type, kv, allow_any_query: bool = False): type "any"; if None, then an error is thrown. """ if kv: # At least one key-value pair. - queries = [] + queries: list[Query] = [] # Iterate pairwise over the arguments. it = iter(kv) for tag, value in zip(it, it): @@ -1417,7 +1421,7 @@ def _metadata_query(self, query_type, kv, allow_any_query: bool = False): raise BPDError(ERROR_UNKNOWN, "no such tagtype") else: _, key = self._tagtype_lookup(tag) - queries.append(query_type(key, value)) + queries.append(Item.field_query(key, value, query_type)) return dbcore.query.AndQuery(queries) else: # No key-value pairs. return dbcore.query.TrueQuery() @@ -1480,7 +1484,9 @@ def cmd_count(self, conn, tag, value): _, key = self._tagtype_lookup(tag) songs = 0 playtime = 0.0 - for item in self.lib.items(dbcore.query.MatchQuery(key, value)): + for item in self.lib.items( + Item.field_query(key, value, dbcore.query.MatchQuery) + ): songs += 1 playtime += item.length yield "songs: " + str(songs)