2 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
4 # Nephilim is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # Nephilim is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
18 from PyQt4
import QtCore
21 from common
import sec2min
24 """Song provides a dictionary-like wrapper around song metadata.
25 Its instances _shouldn't_ be stored, use SongRef or PlaylistEntryRef for that.
26 Access '?tag' to get always at least an empty string. """
28 def __init__(self
, data
):
30 if isinstance(data
[tag
], list):
31 data
[tag
] = ','.join(data
[tag
])
33 dict.__init
__(self
, data
)
35 def __eq__(self
, other
):
36 if not isinstance(other
, Song
):
38 return self
['file'] == other
['file']
40 def __ne__(self
, other
):
41 if not isinstance(other
, Song
):
43 return self
['file'] != other
['file']
45 def __getitem__(self
, key
):
47 return dict.__getitem
__(self
, key
)
49 if key
and key
[0] == '?':
51 return self
[key
] if key
in self
else ''
54 return int(self
['track'])
57 return int(self
['track'].split('/')[0])
61 return sec2min(int(self
['time']))
66 elif key
== 'albumartist':
70 def __contains__(self
, item
):
71 if dict.__contains
__(self
, item
):
79 def __nonzero__(self
):
80 return bool(self
['file'])
82 def expand_tags(self
, str):
83 """Expands tags in form $tag in str."""
85 ret
= ret
.replace('$title', self
['title']) #to ensure that it is set to at least filename
86 for tag
in self
.keys() + ['tracknum', 'length', 'id']:
87 ret
= ret
.replace('$' + tag
, unicode(self
[tag
]))
88 ret
= ret
.replace('$songdir', os
.path
.dirname(self
['file']))
92 """SongRef stores only a reference to the song (uri) instead
93 of full metadata to conserve memory. Song's tags can be accessed
94 as in Song, but it requires a call to MPD for each tag. """
98 def __init__(self
, mpclient
, path
):
99 self
.mpclient
= mpclient
102 def __getitem__(self
, key
):
103 return self
.song()[key
]
105 def __nonzero__(self
):
106 return bool(self
.path
)
110 return Song(self
.mpclient
.find('file', self
.path
)[0])
114 class PlaylistEntryRef
:
115 """This class stores a reference to a playlist entry instead of full
116 metadata to conserve memory. Song's tags can be accessed
117 as in Song, but it requires a call to MPD for each tag. """
121 def __init__(self
, mpclient
, plid
):
122 self
.mpclient
= mpclient
125 def __getitem__(self
, key
):
126 return self
.plid
if key
== 'id' else self
.song()[key
]
128 def __nonzero__(self
):
129 return self
.plid
!= '-1'
133 return self
.mpclient
.playlistid(self
.plid
)