forked from metabrainz/python-discid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.py
More file actions
111 lines (88 loc) · 3.35 KB
/
track.py
File metadata and controls
111 lines (88 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright (C) 2013 Johannes Dewender
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Please submit bug reports to GitHub:
# https://github.com/metabrainz/python-discid/issues
"""Track class"""
from ctypes import c_char_p, c_int, c_void_p
from typing import TYPE_CHECKING
from discid.libdiscid import _LIB
from discid.util import _decode, _sectors_to_seconds
if TYPE_CHECKING:
from .disc import Disc
class Track(object):
"""Track objects are part of the :class:`Disc` class.
:param disc: the :class:`Disc` object
:param number: the track number
"""
def __init__(self, disc: "Disc", number: int):
self._disc = disc
self._number = number
assert self._disc._handle and self._disc._handle.value is not None
def __str__(self):
assert self._disc._success
return str(self.number)
_LIB.discid_get_track_offset.argtypes = (c_void_p, c_int)
_LIB.discid_get_track_offset.restype = c_int
def _get_track_offset(self):
assert self._disc._success
return _LIB.discid_get_track_offset(self._disc._handle, self.number)
_LIB.discid_get_track_length.argtypes = (c_void_p, c_int)
_LIB.discid_get_track_length.restype = c_int
def _get_track_length(self):
assert self._disc._success
return _LIB.discid_get_track_length(self._disc._handle, self.number)
try:
_LIB.discid_get_track_isrc.argtypes = (c_void_p, c_int)
_LIB.discid_get_track_isrc.restype = c_char_p
except AttributeError:
pass
def _get_track_isrc(self):
assert self._disc._success
if "isrc" in self._disc._requested_features:
try:
result = _LIB.discid_get_track_isrc(self._disc._handle, self.number)
except AttributeError:
return None
else:
return _decode(result)
else:
return None
@property
def number(self) -> int:
"""The track number"""
return self._number
@property
def offset(self) -> int:
"""The track offset"""
return self._get_track_offset()
@property
def sectors(self) -> int:
"""The track length in sectors"""
return self._get_track_length()
length = sectors
"""This is an alias for :attr:`sectors`"""
@property
def seconds(self) -> int:
"""Track length in seconds"""
return _sectors_to_seconds(self.sectors)
@property
def isrc(self) -> str | None:
"""The International Standard Recording Code
This will be :obj:`None` when the `"isrc"` feature was not requested
or not supported, otherwise this is a :obj:`str <python:str>` object.
"""
return self._get_track_isrc()
# vim:set shiftwidth=4 smarttab expandtab: