Skip to content

Commit 2a36fb7

Browse files
committed
Add documentation
1 parent 8261f72 commit 2a36fb7

6 files changed

Lines changed: 39 additions & 9 deletions

File tree

beets/dbcore/queryparse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
from beets.util import cached_classproperty
1717

1818
from . import query, sort
19+
from .query import Query # noqa: TC001 required for docs type resolution
20+
from .sort import Sort # noqa: TC001 required for docs type resolution
1921

2022
if TYPE_CHECKING:
2123
from collections.abc import Iterable, Sequence
2224

2325
from beets.library.models import LibModel
2426

25-
from .query import Query
26-
from .sort import Sort
27-
2827

2928
PARSE_QUERY_PART_REGEX = re.compile(
3029
# Non-capturing optional segment for the keyword.

beets/library/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def default_sort(cls) -> Sort:
8686
def parse_query(
8787
cls, query: str | Sequence[str] | None = None
8888
) -> ModelQuery:
89+
"""Parse human-readable search and sort syntax for this model."""
8990
return ModelQuery.parse(cls, query)
9091

9192
@property

docs/_templates/autosummary/namedtuple.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.. currentmodule:: {{ module }}
44

55
.. autonamedtuple:: {{ objname }}
6+
:members:

docs/api/database.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ Queries
4343
Query
4444
FieldQuery
4545
AndQuery
46+
47+
.. currentmodule:: beets.dbcore.queryparse
48+
49+
.. autosummary::
50+
:toctree: generated/
51+
52+
ModelQuery

docs/changelog.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ Bug fixes
6565
ordered before present ones when sorting ascending and after them when
6666
descending. :bug:`3461`
6767

68-
..
69-
For plugin developers
70-
~~~~~~~~~~~~~~~~~~~~~
68+
For plugin developers
69+
~~~~~~~~~~~~~~~~~~~~~
70+
71+
- Query parsing is now available through ``Item.parse_query`` and
72+
``Album.parse_query``, which return a
73+
:class:`~beets.dbcore.queryparse.ModelQuery` containing the query and sort
74+
order. The ``parse_query_string``, ``parse_query_parts``,
75+
``query_from_strings``, ``sort_from_strings``, and ``parse_sorted_query``
76+
helpers are deprecated.
7177

7278
Other changes
7379
~~~~~~~~~~~~~

docs/dev/library.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ There are many different types of queries. Just as an example,
237237
:class:`CollectionQuery`) takes a set of other query objects and bundles them
238238
together, matching only albums/items that match all constituent queries.
239239

240-
Beets has a human-writable plain-text query syntax that can be parsed into
241-
:class:`Query` objects. Calling ``AndQuery.from_strings`` parses a list of query
242-
parts into a query object that can then be used with |Library| objects.
240+
Beets has a human-writable plain-text query syntax. The |Library| query methods
241+
accept this syntax directly:
242+
243+
.. code-block:: python
244+
245+
items = lib.items('artist:"The Beatles" year-')
246+
247+
When code needs the :class:`Query` and sort objects directly, use
248+
``Item.parse_query`` for item queries or ``Album.parse_query`` for album
249+
queries. These class methods accept either a query string or a sequence of query
250+
parts and return a :class:`~beets.dbcore.queryparse.ModelQuery` containing both
251+
objects.
252+
253+
Use :meth:`FieldQuery.from_model` when constructing a field query directly. This
254+
applies the model's field aliases and normalization rules:
255+
256+
.. code-block:: python
257+
258+
query = MatchQuery.from_model(Item, "artist", "The Beatles")

0 commit comments

Comments
 (0)