Skip to content

Commit 20bdffc

Browse files
committed
API docs: slightly less skeletal
1 parent d6dac1d commit 20bdffc

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

beets/library.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,8 @@ def script(self, statements):
15651565

15661566

15671567
class Library(object):
1568-
"""A music library using an SQLite database as a metadata store."""
1568+
"""A database of music containing songs and albums.
1569+
"""
15691570
def __init__(self, path='library.blb',
15701571
directory='~/Music',
15711572
path_formats=((PF_KEY_DEFAULT,
@@ -1703,19 +1704,17 @@ def _tx_stack(self):
17031704
yield self._tx_stacks[thread_id]
17041705

17051706
def transaction(self):
1706-
"""Get a transaction object for interacting with the database.
1707-
This should *always* be used as a context manager.
1707+
"""Get a :class:`Transaction` object for interacting directly
1708+
with the underlying SQLite database.
17081709
"""
17091710
return Transaction(self)
17101711

17111712

17121713
# Adding objects to the database.
17131714

17141715
def add(self, item):
1715-
"""Add the item as a new object to the library database. The id
1716-
field will be updated; the new id is returned. If copy, then
1717-
each item is copied to the destination location before it is
1718-
added.
1716+
"""Add the :class:`Item` object to the library database. The
1717+
item's id field will be updated; the new id is returned.
17191718
"""
17201719
item.added = time.time()
17211720
if not item._lib:
@@ -1756,7 +1755,7 @@ def add(self, item):
17561755
def add_album(self, items):
17571756
"""Create a new album in the database with metadata derived
17581757
from its items. The items are added to the database if they
1759-
don't yet have an ID. Returns an Album object.
1758+
don't yet have an ID. Returns an :class:`Album` object.
17601759
"""
17611760
# Set the metadata from the first item.
17621761
album_values = dict((key, items[0][key]) for key in ALBUM_KEYS_ITEM)
@@ -1809,15 +1808,17 @@ def _fetch(self, model_cls, query, order_by=None):
18091808
return Results(model_cls, rows, self, None if where else query)
18101809

18111810
def albums(self, query=None):
1812-
"""Get a sorted list of Album objects matching the given query.
1811+
"""Get a sorted list of :class:`Album` objects matching the
1812+
given query.
18131813
"""
18141814
order = '{0}, album'.format(
18151815
_orelse("albumartist_sort", "albumartist")
18161816
)
18171817
return self._fetch(Album, query, order)
18181818

18191819
def items(self, query=None):
1820-
"""Get a sorted list of Item objects matching the given query.
1820+
"""Get a sorted list of :class:`Item` objects matching the given
1821+
query.
18211822
"""
18221823
order = '{0}, album'.format(
18231824
_orelse("artist_sort", "artist")
@@ -1834,14 +1835,15 @@ def _get(self, model_cls, id):
18341835
return self._fetch(model_cls, MatchQuery('id', id)).get()
18351836

18361837
def get_item(self, id):
1837-
"""Fetch an Item by its ID. Returns None if no match is found.
1838+
"""Fetch an :class:`Item` by its ID. Returns `None` if no match is
1839+
found.
18381840
"""
18391841
return self._get(Item, id)
18401842

18411843
def get_album(self, item_or_id):
1842-
"""Given an album ID or an item associated with an album,
1843-
return an Album object for the album. If no such album exists,
1844-
returns None.
1844+
"""Given an album ID or an item associated with an album, return
1845+
an :class:`Album` object for the album. If no such album exists,
1846+
returns `None`.
18451847
"""
18461848
if isinstance(item_or_id, int):
18471849
album_id = item_or_id

docs/dev/api.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,77 @@ API Documentation
33

44
.. currentmodule:: beets.library
55

6+
This page describes the internal API of beets' core. It's a work in
7+
progress---since beets is an application first and a library second, its API
8+
has been mainly undocumented until recently. Please file bugs if you run
9+
across incomplete or incorrect docs here.
10+
11+
The :class:`Library` object is the central repository for data in beets. It
12+
represents a database containing songs, which are :class:`Item` instances, and
13+
groups of items, which are :class:`Album` instances.
14+
615
The Library Class
716
-----------------
817

918
.. autoclass:: Library(path, directory[, path_formats[, replacements]])
19+
20+
.. automethod:: items
21+
22+
.. automethod:: albums
23+
24+
.. automethod:: get_item
25+
26+
.. automethod:: get_album
27+
28+
.. automethod:: add
29+
30+
.. automethod:: add_album
31+
32+
.. automethod:: transaction
33+
34+
Transactions
35+
''''''''''''
36+
37+
The :class:`Library` class provides the basic methods necessary to access and
38+
manipulate its contents. To perform more complicated operations atomically, or
39+
to interact directly with the underlying SQLite database, you must use a
40+
*transaction*. For example::
41+
42+
lib = Library()
43+
with lib.transaction() as tx:
44+
items = lib.items(query)
45+
lib.add_album(list(items))
46+
47+
.. autoclass:: Transaction
1048
:members:
1149

1250
Model Classes
1351
-------------
1452

53+
The two model entities in beets libraries, :class:`Item` and :class:`Album`,
54+
share base classes that provide generic data storage. The :class:`LibModel`
55+
class inherits from :class:`FlexModel`, and both :class:`Item` and
56+
:class:`Album` inherit from it.
57+
58+
The fields model classes can be accessed using attributes (dots, as in
59+
``item.artist``) or items (brackets, as in ``item['artist']``). The
60+
:class:`FlexModel` base class provides some methods that resemble `dict`
61+
objects.
62+
1563
.. autoclass:: FlexModel
1664
:members:
1765

1866
.. autoclass:: LibModel
1967
:members:
2068

69+
Item
70+
''''
71+
2172
.. autoclass:: Item
2273
:members:
2374

75+
Album
76+
'''''
77+
2478
.. autoclass:: Album
2579
:members:

0 commit comments

Comments
 (0)