summaryrefslogtreecommitdiff
path: root/nephilim/common.py
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-08-21 09:28:16 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-08-21 09:28:16 +0200
commit4636982682d69c0fc5fea724344f02c94c6cb741 (patch)
tree6ea1151861297066c2b0517cbb25758da4a1213e /nephilim/common.py
parent7b9278975c57b57f651641cd4671c84ffc1edd02 (diff)
rename misc->common
Diffstat (limited to 'nephilim/common.py')
-rw-r--r--nephilim/common.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/nephilim/common.py b/nephilim/common.py
new file mode 100644
index 0000000..d4d7b47
--- /dev/null
+++ b/nephilim/common.py
@@ -0,0 +1,135 @@
+#
+# 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
+
+socket.setdefaulttimeout(8)
+
+appIcon = 'gfx/nephilim_small.png'
+APPNAME = 'nephilim'
+ORGNAME = 'nephilim'
+
+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.connect(self, QtCore.SIGNAL('clicked(bool)'), 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(str, expanders):
+ #ensure that str is QString
+ str = QtCore.QString(str)
+ for expander in expanders:
+ str = expander.expand_tags(str)
+
+ #remove unexpanded tags
+ return str.replace(QtCore.QRegExp('\\$\\w+'), '')
+
+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.filepath()):
+ dirname = os.path.dirname(song.filepath())
+ filepath = ''
+ elif '://' in song.filepath(): # 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
+
+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 QString,
+ Python unicode string or None if not found."""
+ #public, read-only
+ logger = None
+ name = ''
+
+ #private
+ nam = None # NetworkAccessManager
+ srep = None # search results NetworkReply
+ mrep = None # metadata page NetworkReply
+ song = None # current song
+
+ #### 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
+ if self.srep:
+ self.srep.finished.disconnect()
+ self.srep.abort()
+ self.srep = None
+ if self.mrep:
+ self.mrep.finished.disconnect()
+ self.mrep.abort()
+ self.mrep = None
+ self.song = song
+
+ self.logger.info('Searching %s: %s.'%(self. name, url))
+ self.srep = self.nam.get(QtNetwork.QNetworkRequest(url))
+
+ def finish(self, metadata = None):
+ """A private convenience function to clean up and emit finished().
+ Feel free to reimplement/not use it."""
+ self.srep = None
+ self.mrep = None
+ self.emit(QtCore.SIGNAL('finished'), self.song, metadata)
+ self.song = None
+
+ #### public ####
+ def fetch(self, song):
+ """Reimplement this in subclasses."""
+ pass