1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#
# Copyright (C) 2008 jerous <jerous@gmail.com>
# Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
#
# Nephilim is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Nephilim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
#
from PyQt4 import QtCore, QtGui, QtNetwork
import socket
import logging
import os
import re
from htmlentitydefs import name2codepoint as n2cp
socket.setdefaulttimeout(8)
appIcon = ':icons/nephilim_small.png'
APPNAME = 'nephilim'
ORGNAME = 'nephilim'
# custom mimetypes used for drag&drop
MIMETYPES = {'songs' : 'application/x-mpd-songlist', 'plistsongs' : 'application/x-mpd-playlistsonglist'}
def sec2min(secs):
"""Converts seconds to min:sec."""
min=int(secs/60)
sec=secs%60
if sec<10:sec='0'+str(sec)
return str(min)+':'+str(sec)
class Button(QtGui.QPushButton):
iconSize=32
"""A simple Button class which calls $onClick when clicked."""
def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None):
QtGui.QPushButton.__init__(self, parent)
if onClick:
self.clicked.connect(onClick)
if iconPath:
self.changeIcon(iconPath)
if not(iconPath and iconOnly):
QtGui.QPushButton.setText(self, caption)
self.setToolTip(caption)
def setText(self, caption):
self.setToolTip(caption)
if self.icon()==None:
self.setText(caption)
def changeIcon(self, iconPath):
icon=QtGui.QIcon()
icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
self.setIcon(icon)
def expand_tags(string, expanders):
for expander in expanders:
string = expander.expand_tags(string)
#remove unexpanded tags
return re.sub('\$\w+', '', string)
def generate_metadata_path(song, dir_tag, file_tag):
"""Generate dirname and (db files only) full file path for reading/writing metadata files
(cover, lyrics) from $tags in dir/filename."""
if QtCore.QDir.isAbsolutePath(song['file']):
dirname = os.path.dirname(song['file'])
filepath = ''
elif '://' in song['file']: # we are streaming
dirname = ''
filepath = ''
else:
dirname = expand_tags(dir_tag, (QtGui.QApplication.instance(), song))
filepath = '%s/%s'%(dirname, expand_tags(file_tag, (QtGui.QApplication.instance(), song)).replace('/', '_'))
return dirname, filepath
def substitute_entity(match):
ent = match.group(3)
if match.group(1) == "#":
if match.group(2) == '':
return unichr(int(ent))
elif match.group(2) == 'x':
return unichr(int('0x'+ent, 16))
else:
cp = n2cp.get(ent)
if cp:
return unichr(cp)
else:
return match.group()
def decode_htmlentities(string):
entity_re = re.compile(r'&(#?)(x?)(\w+);')
return entity_re.subn(substitute_entity, string)[0]
class MetadataFetcher(QtCore.QObject):
"""A basic class for metadata fetchers. Provides a fetch(song) function,
emits a finished(song, metadata) signal when done; lyrics is either a Python
unicode string or None if not found."""
#public, read-only
logger = None
name = ''
#private
nam = None # NetworkAccessManager
rep = None # current NetworkReply.
song = None # current song
# SIGNALS
finished = QtCore.pyqtSignal(['song', 'metadata'])
#### private ####
def __init__(self, plugin):
QtCore.QObject.__init__(self, plugin)
self.nam = QtNetwork.QNetworkAccessManager()
self.logger = plugin.logger
def fetch2(self, song, url):
"""A private convenience function to initiate fetch process."""
# abort any existing connections
self.abort()
self.song = song
self.logger.info('Searching %s: %s.'%(self. name, url.toString()))
self.rep = self.nam.get(QtNetwork.QNetworkRequest(url))
self.rep.error.connect(self.handle_error)
def finish(self, metadata = None):
"""A private convenience function to clean up and emit finished().
Feel free to reimplement/not use it."""
self.rep = None
self.finished.emit(self.song, metadata)
self.song = None
def handle_error(self):
"""Print the error and abort."""
self.logger.error(self.rep.errorString())
self.abort()
self.finish()
#### public ####
def fetch(self, song):
"""Reimplement this in subclasses."""
pass
def abort(self):
"""Abort all downloads currently in progress."""
if self.rep:
self.rep.blockSignals(True)
self.rep.abort()
self.rep = None
class SongsMimeData(QtCore.QMimeData):
# private
__songs = None
__plistsongs = None
def set_songs(self, songs):
self.__songs = songs
def songs(self):
return self.__songs
def set_plistsongs(self, songs):
self.__plistsongs = songs
def plistsongs(self):
return self.__plistsongs
def formats(self):
types = QtCore.QMimeData.formats(self)
if self.__songs:
types += MIMETYPES['songs']
if self.__plistsongs:
types += MIMETYPES['plistsongs']
return types
def hasFormat(self, format):
if format == MIMETYPES['songs'] and self.__songs:
return True
elif format == MIMETYPES['plistsongs'] and self.__plistsongs:
return True
return QtCore.QMimeData.hasFormat(self, format)
|