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':
68 elif key
== 'songdir':
69 return os
.path
.dirname(self
['file'])
73 def __contains__(self
, item
):
74 if dict.__contains
__(self
, item
):
82 def __nonzero__(self
):
83 return bool(self
['file'])
85 def expand_tags(self
, str):
86 """Expands tags in form $tag in str."""
88 ret
= ret
.replace('$title', self
['title']) #to ensure that it is set to at least filename
89 for tag
in self
.keys() + ['tracknum', 'length', 'id']:
90 ret
= ret
.replace('$' + tag
, unicode(self
[tag
]))
91 ret
= ret
.replace('$songdir', os
.path
.dirname(self
['file']))
95 """SongRef stores only a reference to the song (uri) instead
96 of full metadata to conserve memory. Song's tags can be accessed
97 as in Song, but it requires a call to MPD for each tag. """
101 def __init__(self
, mpclient
, path
):
102 self
.mpclient
= mpclient
105 def __getitem__(self
, key
):
106 return self
.song()[key
]
108 def __nonzero__(self
):
109 return bool(self
.path
)
113 return Song(self
.mpclient
.find('file', self
.path
)[0])
117 class PlaylistEntryRef
:
118 """This class stores a reference to a playlist entry instead of full
119 metadata to conserve memory. Song's tags can be accessed
120 as in Song, but it requires a call to MPD for each tag. """
124 def __init__(self
, mpclient
, plid
):
125 self
.mpclient
= mpclient
128 def __getitem__(self
, key
):
129 return self
.plid
if key
== 'id' else self
.song()[key
]
131 def __nonzero__(self
):
132 return self
.plid
!= '-1'
136 return self
.mpclient
.playlistid(self
.plid
)