From 4636982682d69c0fc5fea724344f02c94c6cb741 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 21 Aug 2009 09:28:16 +0200 Subject: rename misc->common --- nephilim/common.py | 135 ++++++++++++++++++++++++++++++++++++++++ nephilim/misc.py | 135 ---------------------------------------- nephilim/nephilim_app.py | 2 +- nephilim/plugins/AlbumCover.py | 14 ++--- nephilim/plugins/Lyrics.py | 16 ++--- nephilim/plugins/Notify.py | 2 +- nephilim/plugins/PlayControl.py | 2 +- nephilim/plugins/Systray.py | 2 +- nephilim/settings_wg.py | 2 +- nephilim/song.py | 2 +- nephilim/winMain.py | 2 +- 11 files changed, 157 insertions(+), 157 deletions(-) create mode 100644 nephilim/common.py delete mode 100644 nephilim/misc.py (limited to 'nephilim') 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 +# Copyright (C) 2009 Anton Khirnov +# +# 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 . +# + +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 diff --git a/nephilim/misc.py b/nephilim/misc.py deleted file mode 100644 index d4d7b47..0000000 --- a/nephilim/misc.py +++ /dev/null @@ -1,135 +0,0 @@ -# -# Copyright (C) 2008 jerous -# Copyright (C) 2009 Anton Khirnov -# -# 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 . -# - -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 diff --git a/nephilim/nephilim_app.py b/nephilim/nephilim_app.py index 8caf187..ad340da 100644 --- a/nephilim/nephilim_app.py +++ b/nephilim/nephilim_app.py @@ -18,7 +18,7 @@ from PyQt4 import QtGui, QtCore from winMain import winMain -from misc import ORGNAME, APPNAME, appIcon +from common import ORGNAME, APPNAME, appIcon from mpclient import MPClient from settings_wg import SettingsWidget from connect_wg import ConnectWidget diff --git a/nephilim/plugins/AlbumCover.py b/nephilim/plugins/AlbumCover.py index c64875d..f54b101 100644 --- a/nephilim/plugins/AlbumCover.py +++ b/nephilim/plugins/AlbumCover.py @@ -21,7 +21,7 @@ from PyQt4.QtCore import QVariant import os from ..plugin import Plugin -from .. import misc +from .. import common class AlbumCoverWidget(QtGui.QLabel): @@ -144,7 +144,7 @@ class AlbumCover(Plugin): # store covers groupbox self.store = QtGui.QGroupBox('Store covers.') - self.store.setToolTip('Should %s store its own copy of covers?'%misc.APPNAME) + self.store.setToolTip('Should %s store its own copy of covers?'%common.APPNAME) self.store.setCheckable(True) self.store.setChecked(self.settings.value('store').toBool()) self.store.setLayout(QtGui.QGridLayout()) @@ -155,12 +155,12 @@ class AlbumCover(Plugin): '$musicdir will be expanded to path to MPD music library (as set by user)\n' '$songdir will be expanded to path to the song (relative to $musicdir\n' 'other tags same as in covername' - %misc.APPNAME) + %common.APPNAME) self.covername = QtGui.QLineEdit(self.settings.value('covername').toString()) self.covername.setToolTip('Filename for %s cover files.\n' 'All tags supported by MPD will be expanded to their\n' 'values for current song, e.g. $title, $track, $artist,\n' - '$album, $genre etc.'%misc.APPNAME) + '$album, $genre etc.'%common.APPNAME) self.store.layout().addWidget(QtGui.QLabel('Cover directory'), 0, 0) self.store.layout().addWidget(self.coverdir, 0, 1) self.store.layout().addWidget(QtGui.QLabel('Cover filename'), 1, 0) @@ -202,7 +202,7 @@ class AlbumCover(Plugin): self.settings.endGroup() self.plugin.refresh() - class FetcherLastfm(misc.MetadataFetcher): + class FetcherLastfm(common.MetadataFetcher): name = 'Last.fm' def fetch(self, song): @@ -318,7 +318,7 @@ class AlbumCover(Plugin): self.__cover_path = '' return self.o.set_cover(None, None) - (self.__cover_dir, self.__cover_path) = misc.generate_metadata_path(song, + (self.__cover_dir, self.__cover_path) = common.generate_metadata_path(song, self.settings.value(self.name + '/coverdir').toString(), self.settings.value(self.name + '/covername').toString()) try: @@ -360,7 +360,7 @@ class AlbumCover(Plugin): if not song: path = self.__cover_path else: - path = misc.generate_metadata_path(song, self.settings.value(self.name + '/coverdir').toString(), + path = common.generate_metadata_path(song, self.settings.value(self.name + '/coverdir').toString(), self.settings.value(self.name + '/covername').toString()) if not QtCore.QFile.remove(path): self.logger.error('Error removing file %s.'%path) diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py index 0c90e8c..15160d8 100644 --- a/nephilim/plugins/Lyrics.py +++ b/nephilim/plugins/Lyrics.py @@ -23,7 +23,7 @@ import re from lxml import etree from ..plugin import Plugin -from .. import misc +from .. import common class LyricsWidget(QtGui.QWidget): #public @@ -140,7 +140,7 @@ class Lyrics(Plugin): elif self.__results >= len(self.__fetchers) and not self.o.lyrics_loaded: self.o.set_lyrics(song, None) - class FetchLyricwiki(misc.MetadataFetcher): + class FetchLyricwiki(common.MetadataFetcher): name = 'Lyricwiki' def fetch(self, song): @@ -189,7 +189,7 @@ class Lyrics(Plugin): lyrics += etree.tostring(elem, method = 'text', encoding = 'utf-8') self.finish(lyrics) - class FetchAnimelyrics(misc.MetadataFetcher): + class FetchAnimelyrics(common.MetadataFetcher): name = 'Animelyrics' def fetch(self, song): @@ -246,7 +246,7 @@ class Lyrics(Plugin): # store lyrics groupbox self.store = QtGui.QGroupBox('Store lyrics.') - self.store.setToolTip('Should %s store its own copy of lyrics?'%misc.APPNAME) + self.store.setToolTip('Should %s store its own copy of lyrics?'%common.APPNAME) self.store.setCheckable(True) self.store.setChecked(self.settings.value('store').toBool()) self.store.setLayout(QtGui.QGridLayout()) @@ -257,12 +257,12 @@ class Lyrics(Plugin): '$musicdir will be expanded to path to MPD music library (as set by user)\n' '$songdir will be expanded to path to the song (relative to $musicdir\n' 'other tags same as in lyricname' - %misc.APPNAME) + %common.APPNAME) self.lyricname = QtGui.QLineEdit(self.settings.value('lyricname').toString()) self.lyricname.setToolTip('Filename for %s lyricsfiles.\n' 'All tags supported by MPD will be expanded to their\n' 'values for current song, e.g. $title, $track, $artist,\n' - '$album, $genre etc.'%misc.APPNAME) + '$album, $genre etc.'%common.APPNAME) self.store.layout().addWidget(QtGui.QLabel('Lyrics directory'), 0, 0) self.store.layout().addWidget(self.lyricdir, 0, 1) self.store.layout().addWidget(QtGui.QLabel('Lyrics filename'), 1, 0) @@ -333,7 +333,7 @@ class Lyrics(Plugin): self.__lyrics_path = '' return self.o.set_lyrics(None, None) - (self.__lyrics_dir, self.__lyrics_path) = misc.generate_metadata_path(song, + (self.__lyrics_dir, self.__lyrics_path) = common.generate_metadata_path(song, self.settings.value(self.name + '/lyricdir').toString(), self.settings.value(self.name + '/lyricname').toString()) try: @@ -370,7 +370,7 @@ class Lyrics(Plugin): if not song: path = self.__lyrics_path else: - path = misc.generate_metadata_path(song, self.settings.value(self.name + '/lyricdir').toString(), + path = common.generate_metadata_path(song, self.settings.value(self.name + '/lyricdir').toString(), self.settings.value(self.name + '/lyricname').toString()) try: diff --git a/nephilim/plugins/Notify.py b/nephilim/plugins/Notify.py index e2204fa..d8b26b9 100644 --- a/nephilim/plugins/Notify.py +++ b/nephilim/plugins/Notify.py @@ -20,7 +20,7 @@ from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant from traceback import print_exc -from ..misc import sec2min, APPNAME, expand_tags +from ..common import sec2min, APPNAME, expand_tags from ..plugin import Plugin from .. import plugins diff --git a/nephilim/plugins/PlayControl.py b/nephilim/plugins/PlayControl.py index 8153eb7..0784bab 100644 --- a/nephilim/plugins/PlayControl.py +++ b/nephilim/plugins/PlayControl.py @@ -18,7 +18,7 @@ from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant -from ..misc import Button +from ..common import Button from ..plugin import Plugin class wgPlayControl(QtGui.QToolBar): diff --git a/nephilim/plugins/Systray.py b/nephilim/plugins/Systray.py index 558fc24..e093c9f 100644 --- a/nephilim/plugins/Systray.py +++ b/nephilim/plugins/Systray.py @@ -20,7 +20,7 @@ from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant from ..plugin import Plugin -from ..misc import sec2min, APPNAME, appIcon, expand_tags +from ..common import sec2min, APPNAME, appIcon, expand_tags class Systray(Plugin): o = None diff --git a/nephilim/settings_wg.py b/nephilim/settings_wg.py index 5d5aafe..1175936 100644 --- a/nephilim/settings_wg.py +++ b/nephilim/settings_wg.py @@ -20,7 +20,7 @@ from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant import os -from misc import Button +from common import Button import plugin diff --git a/nephilim/song.py b/nephilim/song.py index bc0d1c4..008312a 100644 --- a/nephilim/song.py +++ b/nephilim/song.py @@ -19,7 +19,7 @@ from PyQt4 import QtCore import os -from misc import sec2min +from common import sec2min class Song: """The Song class offers an abstraction of a song.""" diff --git a/nephilim/winMain.py b/nephilim/winMain.py index 54f7551..2912303 100644 --- a/nephilim/winMain.py +++ b/nephilim/winMain.py @@ -20,7 +20,7 @@ from PyQt4 import QtGui, QtCore from PyQt4.QtCore import QVariant import logging -from misc import APPNAME, sec2min, appIcon +from common import APPNAME, sec2min, appIcon DEFAULT_LAYOUT_FILE = 'default_layout' -- cgit v1.2.3