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."""
27 def __init__(self
, data
):
28 # decode all strings to unicode
30 if isinstance(data
[tag
], str):
31 data
[tag
] = data
[tag
].decode('utf-8')
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
)
51 return int(self
['track'])
54 return int(self
['track'].split('/')[0])
58 return sec2min(int(self
['time']))
65 def __nonzero__(self
):
66 return bool(self
['file'])
68 def expand_tags(self
, str):
69 """Expands tags in form $tag in str."""
71 ret
= ret
.replace('$title', self
['title']) #to ensure that it is set to at least filename
72 for tag
in self
.keys() + ['tracknum', 'length', 'id']:
73 ret
= ret
.replace('$' + tag
, unicode(self
[tag
]))
74 ret
= ret
.replace('$songdir', os
.path
.dirname(self
['file']))
79 """SongRef stores only a reference to the song (uri) instead
80 of full metadata to conserve memory. Song's tags can be accessed
81 as in Song, but it requires a call to MPD for each tag. """
85 def __init__(self
, mpclient
, path
):
86 self
.mpclient
= mpclient
89 def __getitem__(self
, key
):
90 return self
.song()[key
]
92 def __nonzero__(self
):
93 return bool(self
.path
)
97 return Song(self
.mpclient
.find('file', self
.path
)[0])
101 class PlaylistEntryRef
:
102 """This class stores a reference to a playlist entry instead of full
103 metadata to conserve memory. Song's tags can be accessed
104 as in Song, but it requires a call to MPD for each tag. """
108 def __init__(self
, mpclient
, id):
109 self
.mpclient
= mpclient
112 def __getitem__(self
, key
):
113 return self
.song()[key
]
115 def __nonzero__(self
):
116 return self
.id != '-1'
120 return Song(self
.mpclient
.playlistid(id)[0])