Move expand_tags for musicdir from winMain to nephilim_app.
[nephilim.git] / nephilim / misc.py
blobf7dd2a0c6cb1200ed6d50b99fbb82b687c6a9d4b
2 # Copyright (C) 2008 jerous <jerous@gmail.com>
3 # Copyright (C) 2009 Anton Khirnov <wyskas@gmail.com>
5 # Nephilim is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Nephilim is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
19 from PyQt4 import QtCore, QtGui
20 import socket
21 import logging
22 import os
24 socket.setdefaulttimeout(8)
26 appIcon = 'gfx/nephilim_small.png'
27 APPNAME = 'nephilim'
28 ORGNAME = 'nephilim'
30 def sec2min(secs):
31 """Converts seconds to min:sec."""
32 min=int(secs/60)
33 sec=secs%60
34 if sec<10:sec='0'+str(sec)
35 return str(min)+':'+str(sec)
37 class Button(QtGui.QPushButton):
38 iconSize=32
39 """A simple Button class which calls $onClick when clicked."""
40 def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None):
41 QtGui.QPushButton.__init__(self, parent)
43 if onClick:
44 self.connect(self, QtCore.SIGNAL('clicked(bool)'), onClick)
45 if iconPath:
46 self.changeIcon(iconPath)
48 if not(iconPath and iconOnly):
49 QtGui.QPushButton.setText(self, caption)
51 self.setToolTip(caption)
53 def setText(self, caption):
54 self.setToolTip(caption)
55 if self.icon()==None:
56 self.setText(caption)
58 def changeIcon(self, iconPath):
59 icon=QtGui.QIcon()
60 icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
61 self.setIcon(icon)
63 def expand_tags(str, expanders):
64 #ensure that str is QString
65 str = QtCore.QString(str)
66 for expander in expanders:
67 str = expander.expand_tags(str)
69 #remove unexpanded tags
70 return str.replace(QtCore.QRegExp('\\$\\w+'), '')
72 def generate_metadata_path(song, dir_tag, file_tag):
73 """Generate dirname and (db files only) full file path for reading/writing metadata files
74 (cover, lyrics) from $tags in dir/filename.
75 HACK main_win argument should be removed ASAP"""
76 if QtCore.QDir.isAbsolutePath(song.filepath()):
77 dirname = os.path.dirname(song.filepath())
78 filepath = ''
79 elif '://' in song.filepath(): # we are streaming
80 dirname = ''
81 filepath = ''
82 else:
83 dirname = expand_tags(dir_tag, (QtGui.QApplication.instance(), song))
84 filepath = '%s/%s'%(dirname, expand_tags(file_tag, (QtGui.QApplication.instance(), song)).replace('/', '_'))
86 return dirname, filepath