Let Track.date be an ISO-8601 string

This lets us have less precision than full dates. E.g. Spotify tracks only got
release year, not full release date.

The original MPD server regularly expose data like this as "Date: 1977", so we
don't need to fake more precision for MPD's sake.
This commit is contained in:
Stein Magnus Jodal 2012-09-12 23:06:06 +02:00
parent 0ed59a8845
commit 7525cad94c
5 changed files with 16 additions and 15 deletions

View File

@ -73,6 +73,9 @@ v0.8 (in development)
- Fixed crash on lookup of unknown path when using local backend.
- Support tracks with only release year, and not a full release date, like e.g.
Spotify tracks.
v0.7.3 (2012-08-11)
===================

View File

@ -1,4 +1,3 @@
import datetime as dt
import logging
from spotify import Link, SpotifyError
@ -31,9 +30,8 @@ class SpotifyTranslator(object):
if not spotify_track.is_loaded():
return Track(uri=uri, name=u'[loading...]')
spotify_album = spotify_track.album()
if (spotify_album is not None and spotify_album.is_loaded()
and dt.MINYEAR <= int(spotify_album.year()) <= dt.MAXYEAR):
date = dt.date(spotify_album.year(), 1, 1)
if spotify_album is not None and spotify_album.is_loaded():
date = spotify_album.year()
else:
date = None
return Track(

View File

@ -242,7 +242,7 @@ def _list_date(context, query):
playlist = context.backend.library.find_exact(**query).get()
for track in playlist.tracks:
if track.date is not None:
dates.add((u'Date', track.date.strftime('%Y-%m-%d')))
dates.add((u'Date', track.date))
return dates
@handle_request(r'^listall "(?P<uri>[^"]+)"')

View File

@ -157,8 +157,8 @@ class Track(ImmutableObject):
:type album: :class:`Album`
:param track_no: track number in album
:type track_no: integer
:param date: track release date
:type date: :class:`datetime.date`
:param date: track release date (YYYY or YYYY-MM-DD)
:type date: string
:param length: track length in milliseconds
:type length: integer
:param bitrate: bitrate in kbit/s

View File

@ -338,7 +338,7 @@ class TrackTest(unittest.TestCase):
self.assertRaises(AttributeError, setattr, track, 'track_no', None)
def test_date(self):
date = datetime.date(1977, 1, 1)
date = '1977-01-01'
track = Track(date=date)
self.assertEqual(track.date, date)
self.assertRaises(AttributeError, setattr, track, 'date', None)
@ -434,7 +434,7 @@ class TrackTest(unittest.TestCase):
self.assertEqual(hash(track1), hash(track2))
def test_eq_date(self):
date = datetime.date.today()
date = '1977-01-01'
track1 = Track(date=date)
track2 = Track(date=date)
self.assertEqual(track1, track2)
@ -459,7 +459,7 @@ class TrackTest(unittest.TestCase):
self.assertEqual(hash(track1), hash(track2))
def test_eq(self):
date = datetime.date.today()
date = '1977-01-01'
artists = [Artist()]
album = Album()
track1 = Track(uri=u'uri', name=u'name', artists=artists, album=album,
@ -508,8 +508,8 @@ class TrackTest(unittest.TestCase):
self.assertNotEqual(hash(track1), hash(track2))
def test_ne_date(self):
track1 = Track(date=datetime.date.today())
track2 = Track(date=datetime.date.today()-datetime.timedelta(days=1))
track1 = Track(date='1977-01-01')
track2 = Track(date='1977-01-02')
self.assertNotEqual(track1, track2)
self.assertNotEqual(hash(track1), hash(track2))
@ -534,12 +534,12 @@ class TrackTest(unittest.TestCase):
def test_ne(self):
track1 = Track(uri=u'uri1', name=u'name1',
artists=[Artist(name=u'name1')], album=Album(name=u'name1'),
track_no=1, date=datetime.date.today(), length=100, bitrate=100,
track_no=1, date='1977-01-01', length=100, bitrate=100,
musicbrainz_id='id1')
track2 = Track(uri=u'uri2', name=u'name2',
artists=[Artist(name=u'name2')], album=Album(name=u'name2'),
track_no=2, date=datetime.date.today()-datetime.timedelta(days=1),
length=200, bitrate=200, musicbrainz_id='id2')
track_no=2, date='1977-01-02', length=200, bitrate=200,
musicbrainz_id='id2')
self.assertNotEqual(track1, track2)
self.assertNotEqual(hash(track1), hash(track2))