Skip to content

Commit f74fe2e

Browse files
committed
doc: improve docstrings
1 parent 5b768aa commit f74fe2e

1 file changed

Lines changed: 93 additions & 72 deletions

File tree

src/taglib.pyx

Lines changed: 93 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,25 @@ VariantMap = Mapping[str, Variant]
2424
class Picture:
2525
"""Represents an embedded picture (cover art) in an audio file.
2626
27-
Attributes
28-
----------
29-
data : bytes
30-
The raw image data (e.g., JPEG or PNG bytes)
31-
mime_type : str
32-
MIME type of the image (e.g., "image/jpeg", "image/png")
33-
description : str
34-
Optional description of the picture (default: "")
35-
picture_type : str
36-
Type of picture (default: "Front Cover"). Common values:
37-
"Front Cover", "Back Cover", "Artist", "Band", etc.
38-
width : int or None
39-
Image width in pixels (may be None, mainly available for FLAC)
40-
height : int or None
41-
Image height in pixels (may be None, mainly available for FLAC)
42-
43-
Example
44-
-------
45-
>>> # Create a picture from a file
46-
>>> with open('cover.jpg', 'rb') as img:
47-
... pic = taglib.Picture(
48-
... data=img.read(),
49-
... mime_type='image/jpeg',
50-
... description='Album artwork',
51-
... picture_type='Front Cover'
52-
... )
27+
Attributes:
28+
data: The raw image data (e.g., JPEG or PNG bytes).
29+
mime_type: MIME type of the image (e.g., "image/jpeg", "image/png").
30+
description: Optional description of the picture (default: "").
31+
picture_type: Type of picture (default: "Front Cover"). Common values:
32+
"Front Cover", "Back Cover", "Artist", "Band", etc.
33+
width: Image width in pixels (may be None, mainly available for FLAC).
34+
height: Image height in pixels (may be None, mainly available for FLAC).
35+
36+
Example:
37+
Create a picture from a file::
38+
39+
with open('cover.jpg', 'rb') as img:
40+
pic = taglib.Picture(
41+
data=img.read(),
42+
mime_type='image/jpeg',
43+
description='Album artwork',
44+
picture_type='Front Cover'
45+
)
5346
"""
5447
data: bytes
5548
mime_type: str
@@ -88,30 +81,35 @@ class Picture:
8881
cdef class File:
8982
"""Class representing an audio file with metadata ("tags").
9083

91-
To read tags from an audio file, create a *File* object, passing the file's path to the
92-
constructor (should be a unicode string):
84+
To read tags from an audio file, create a File object, passing the file's path to the
85+
constructor (should be a unicode string).
9386

94-
>>> f = taglib.File('/path/to/file.ogg')
95-
96-
The tags are stored in the attribute *tags* as a *dict* mapping strings (tag names)
87+
The tags are stored in the attribute ``tags`` as a dict mapping strings (tag names)
9788
to lists of strings (tag values).
9889

99-
>>> for tag, values in f:
100-
>>> print('{}->{}'.format(tag, ', '.join(values)))
101-
10290
If the file contains some metadata that is not supported by pytaglib or not representable
10391
as strings (e.g. cover art, proprietary data written by some programs, ...), according
104-
identifiers will be placed into the *unsupported* attribute of the File object. Using the
105-
method *removeUnsupportedProperties*, some or all of those can be removed.
92+
identifiers will be placed into the ``unsupported`` attribute of the File object. Using the
93+
method ``removeUnsupportedProperties``, some or all of those can be removed.
10694

107-
Additionally, the readonly attributes *length*, *bitrate*, *sampleRate*, and *channels* are
108-
available with their obvious meanings.
95+
Additionally, the readonly attributes ``length``, ``bitrate``, ``sampleRate``, and
96+
``channels`` are available with their obvious meanings.
10997

110-
>>> print('File length: {}'.format(f.length))
98+
Changes to the ``tags`` attribute are stored using the ``save`` method.
11199

112-
Changes to the *tags* attribute are stored using the *save* method.
100+
Attributes:
101+
tags: Dict mapping tag names to lists of tag values.
102+
path: Path to the audio file.
103+
unsupported: List of unsupported property identifiers.
113104

114-
>>> f.save()
105+
Example:
106+
::
107+
108+
f = taglib.File('/path/to/file.ogg')
109+
for tag, values in f.tags.items():
110+
print(f'{tag}->{", ".join(values)}')
111+
print(f'File length: {f.length}')
112+
f.save()
115113
"""
116114
cdef ctypes.FileRef *cFile
117115
cdef public dict[str | bytes, str | bytes] tags
@@ -152,18 +150,19 @@ cdef class File:
152150
self.unsupported.append(toStr(cString))
153151
154152
def save(self) -> dict[str, str]:
155-
"""Store the tags currently hold in the `tags` attribute into the file.
153+
"""Store the tags currently held in the ``tags`` attribute into the file.
156154

157155
If some tags cannot be stored because the underlying metadata format does not support them,
158-
the unsuccesful tags are returned as a "sub-dictionary" of `self.tags` which will be empty
159-
if everything is ok.
160-
161-
Raises
162-
------
163-
OSError
164-
If the save operation fails completely (file does not exist, insufficient rights, ...).
165-
ValueError
166-
When attempting to save after the file was closed.
156+
the unsuccessful tags are returned as a "sub-dictionary" of ``self.tags`` which will be
157+
empty if everything is ok.
158+
159+
Returns:
160+
Dict of tags that could not be saved (empty if all saved successfully).
161+
162+
Raises:
163+
OSError: If the save operation fails completely (file does not exist,
164+
insufficient rights, ...).
165+
ValueError: When attempting to save after the file was closed.
167166
"""
168167
self.check_writable()
169168
cdef:
@@ -200,6 +199,9 @@ cdef class File:
200199

201200
Complex properties are metadata that cannot be represented as simple strings,
202201
such as embedded cover art images.
202+
203+
Yields:
204+
Keys of available complex properties.
203205
"""
204206
self.check_closed()
205207
cdef:
@@ -211,7 +213,14 @@ cdef class File:
211213
def complex_properties(self, key: str) -> Sequence[VariantMap]:
212214
"""Get complex properties for a given key (e.g., "PICTURE").
213215

214-
Raises ValueError if the file is closed.
216+
Args:
217+
key: The complex property key to retrieve.
218+
219+
Returns:
220+
Sequence of variant maps containing the complex property data.
221+
222+
Raises:
223+
ValueError: If the file is closed.
215224
"""
216225
self.check_closed()
217226
cdef ctypes.List[ctypes.VariantMap] props = self.cFile.complexProperties(toCStr(key))
@@ -221,7 +230,17 @@ cdef class File:
221230
"""Set complex properties for a given key (e.g., "PICTURE").
222231

223232
Pass an empty list to remove all complex properties for the key.
224-
Raises ValueError if closed, OSError if read-only.
233+
234+
Args:
235+
key: The complex property key to set.
236+
value: Iterable of variant maps containing the complex property data.
237+
238+
Returns:
239+
True if the operation was successful.
240+
241+
Raises:
242+
ValueError: If the file is closed.
243+
OSError: If the file is read-only.
225244
"""
226245
self.check_writable()
227246
cdef ctypes.List[ctypes.VariantMap] cProps = list_to_variant_map_list(list(value))
@@ -231,9 +250,9 @@ cdef class File:
231250
def pictures(self) -> list[Picture]:
232251
"""Get embedded pictures (cover art) from the file.
233252

234-
Returns
235-
-------
236-
list[Picture]
253+
This is a convenience method for the complex_properties interface that only works for pictures.
254+
255+
Returns:
237256
List of Picture objects, empty if no pictures embedded.
238257
"""
239258
return [Picture._from_variant_map(d) for d in self.complex_properties('PICTURE')]
@@ -244,23 +263,21 @@ cdef class File:
244263

245264
Set to an empty list to remove all pictures.
246265

247-
Parameters
248-
----------
249-
value : list[Picture]
250-
List of Picture objects
266+
This is a convenience method for the complex_properties interface that only works for pictures.
251267

252-
Example
253-
-------
254-
>>> f = taglib.File('song.mp3')
255-
>>> f.pictures = [pic1, pic2]
256-
>>> f.save()
268+
Args:
269+
value: List of Picture objects.
257270
"""
258271
self.set_complex_properties('PICTURE', [p._to_variant_map() for p in value])
259272
260273
def close(self):
261-
"""Closes the file by deleting the underlying Taglib::File object. This will close any open
262-
streams. Calling methods like `save()` or the read-only properties after `close()` will
263-
raise an exception."""
274+
"""Close the file by deleting the underlying Taglib::File object.
275+
276+
Calling any method on the file after calling close will raise an exception.
277+
278+
Raises:
279+
ValueError: If the file is already closed.
280+
"""
264281
if self.is_closed:
265282
raise ValueError("File already closed")
266283
del self.cFile
@@ -319,10 +336,14 @@ cdef class File:
319336
return f"File('{self.path}')"
320337
321338
def taglib_version() -> tuple[int, int]:
322-
"""Taglib major and minor version, as 2-tuple.
339+
"""Get Taglib major and minor version as a 2-tuple.
340+
341+
Note:
342+
This is the version used for compiling the Cython module. Under certain
343+
circumstances (e.g. dynamic linking, or re-using the cythonized code after
344+
upgrading Taglib) the actually running Taglib version might be different.
323345

324-
Note: this is the version used for compiling the Cython module. Under certain
325-
circumstances (e.g. dynamic linking, or re-using the cythonized code after
326-
upgrading Taglib) the actually running Taglib version might be different.
346+
Returns:
347+
Tuple of (major_version, minor_version).
327348
"""
328349
return ctypes.TAGLIB_MAJOR_VERSION, ctypes.TAGLIB_MINOR_VERSION

0 commit comments

Comments
 (0)