Skip to content

Commit 8eaeaac

Browse files
committed
Merge branch 'toc_string'
see pull request #36 Conflicts: test_discid.py
2 parents 93f3fc1 + 7c4830a commit 8eaeaac

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

discid/disc.py

+42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""Disc class
1919
"""
2020

21+
import re
2122
from ctypes import c_int, c_void_p, c_char_p, c_uint
2223

2324
from discid.libdiscid import _LIB, FEATURES
@@ -204,6 +205,23 @@ def _get_submission_url(self):
204205
result = _LIB.discid_get_submission_url(self._handle)
205206
return _decode(result)
206207

208+
try:
209+
_LIB.discid_get_toc_string.argtypes = (c_void_p, )
210+
_LIB.discid_get_toc_string.restype = c_char_p
211+
except AttributeError:
212+
pass
213+
def _get_toc_string(self):
214+
"""The TOC suitable as value of the `toc parameter`
215+
when accessing the MusicBrainz Web Service.
216+
"""
217+
assert self._success
218+
try:
219+
result = _LIB.discid_get_toc_string(self._handle)
220+
except AttributeError:
221+
return None
222+
else:
223+
return _decode(result)
224+
207225
_LIB.discid_get_first_track_num.argtypes = (c_void_p, )
208226
_LIB.discid_get_first_track_num.restype = c_int
209227
def _get_first_track_num(self):
@@ -279,6 +297,30 @@ def submission_url(self):
279297
url = url.replace("/bare/cdlookup.html", "/cdtoc/attach")
280298
return url
281299

300+
@property
301+
def toc_string(self):
302+
"""The TOC suitable as value of the `toc parameter`
303+
when accessing the MusicBrainz Web Service.
304+
305+
This is a :obj:`unicode` or :obj:`str <python:str>` object
306+
and enables fuzzy searching when the actual Disc ID is not found.
307+
308+
Note that this is the unencoded value, which still contains spaces.
309+
310+
.. seealso:: `MusicBrainz Web Service <http://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#discid>`_
311+
"""
312+
toc_string = self._get_toc_string()
313+
if toc_string is None and self.submission_url:
314+
# probably an old version of libdiscid (< 0.6.0)
315+
# gather toc string from submission_url
316+
match = re.search("toc=([0-9+]+)", self.submission_url)
317+
if match is None:
318+
raise ValueError("can't get toc string from submission url")
319+
else:
320+
return match.group(1).replace("+", " ")
321+
else:
322+
return toc_string
323+
282324
@property
283325
def first_track_num(self):
284326
"""Number of the first track"""

doc/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Disc object
3333
.. autoattribute:: id
3434
.. autoattribute:: freedb_id
3535
.. autoattribute:: submission_url
36+
.. autoattribute:: toc_string
3637
.. autoattribute:: first_track_num
3738
.. autoattribute:: last_track_num
3839
.. autoattribute:: sectors

test_discid.py

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def test_put_success(self):
100100
self.assertEqual(track.seconds,
101101
math.floor((track.sectors / 75.0) + 0.5))
102102
self.assertEqual(type(track.seconds), int)
103+
toc_string = ["1", disc.last_track_num, disc.sectors] + track_offsets
104+
toc_string = " ".join(map(str, toc_string))
105+
self.assertEqual(disc.toc_string, toc_string)
103106

104107

105108
class TestDisc(unittest.TestCase):
@@ -121,6 +124,7 @@ def test_read_simple(self):
121124
self.assertEqual(len(disc.id), 28, "Invalid Disc ID")
122125
self.assertEqual(len(disc.freedb_id), 8, "Invalid FreeDB Disc ID")
123126
self.assertTrue(disc.submission_url, "Invalid submission url")
127+
self.assertTrue(disc.toc_string, "Invalid toc string")
124128
self.assertEqual(disc.last_track_num, len(disc.tracks),
125129
"Wrong amount of tracks")
126130
self.assertEqual(disc.sectors,
@@ -142,6 +146,7 @@ def test_read_simple(self):
142146
disc_id = disc.id
143147
freedb_id = disc.freedb_id
144148
submission_url = disc.submission_url
149+
toc_string = disc.toc_string
145150
first = disc.first_track_num
146151
last = disc.last_track_num
147152
sectors = disc.sectors
@@ -154,6 +159,8 @@ def test_read_simple(self):
154159
"different freedb id after put")
155160
self.assertEqual(disc.submission_url, submission_url,
156161
"different submission_url after put")
162+
self.assertEqual(disc.toc_string, toc_string,
163+
"different toc_string after put")
157164
self.assertEqual(disc.first_track_num, first,
158165
"different first track after put")
159166
self.assertEqual(disc.last_track_num, last,
@@ -171,6 +178,7 @@ def test_read_features(self):
171178
disc = discid.read(features=["mcn", "isrc"]) # read from default drive
172179
self.assertEqual(len(disc.id), 28, "Invalid Disc ID")
173180
self.assertTrue(disc.submission_url, "Invalid submission url")
181+
self.assertTrue(disc.toc_string, "Invalid toc string")
174182

175183
if "mcn" in discid.FEATURES:
176184
self.assertTrue(disc.mcn is not None)

0 commit comments

Comments
 (0)