Added static method isValidSource so importers can validate their input. Also tidyup: removed SongImport.errorLog

This commit is contained in:
Samuel Findlay 2012-05-19 20:43:19 +10:00
parent a03af240df
commit cdd867a755
3 changed files with 31 additions and 20 deletions

View File

@ -386,6 +386,7 @@ class SongImportForm(OpenLPWizard):
source_format = self.formatComboBox.currentIndex()
QtCore.QSettings().setValue(u'songs/last import type',
source_format)
import_class = SongFormat.get_class(source_format)
if source_format == SongFormat.OpenLP2:
if self.openLP2FilenameEdit.text().isEmpty():
critical_error_message_box(UiStrings().NFSs,
@ -400,8 +401,8 @@ class SongImportForm(OpenLPWizard):
return False
elif source_format == SongFormat.PowerSong:
if self.powerSongFilenameEdit.text().isEmpty() or \
not self.isPowerSongFolder(
self.powerSongFilenameEdit.text()):
not import_class.isValidSource(
folder=self.powerSongFilenameEdit.text()):
critical_error_message_box(UiStrings().NFdSs,
WizardStrings.YouSpecifyFolder % WizardStrings.PS)
self.powerSongBrowseButton.setFocus()
@ -484,16 +485,6 @@ class SongImportForm(OpenLPWizard):
elif self.currentPage() == self.progressPage:
return True
def isPowerSongFolder(self, dir):
"""
Checks if a folder is a PowerSong 1.0 folder
"""
if os.path.isdir(dir):
for file in os.listdir(dir):
if fnmatch.fnmatch(file, u'*.song'):
return True
return False
def getFiles(self, title, listbox, filters=u''):
"""
Opens a QFileDialog and writes the filenames to the given listbox.
@ -797,7 +788,7 @@ class SongImportForm(OpenLPWizard):
elif source_format == SongFormat.PowerSong:
# Import PowerSong folder
importer = self.plugin.importSongs(SongFormat.PowerSong,
filename=unicode(self.powerSongFilenameEdit.text())
folder=unicode(self.powerSongFilenameEdit.text())
)
elif source_format == SongFormat.OpenLyrics:
# Import OpenLyrics songs
@ -863,11 +854,7 @@ class SongImportForm(OpenLPWizard):
filenames=self.getListOfFiles(self.foilPresenterFileListWidget)
)
importer.doImport()
if importer.errorLog:
self.progressLabel.setText(translate(
'SongsPlugin.SongImportForm', 'Your song import failed.'))
else:
self.progressLabel.setText(WizardStrings.FinishedImport)
self.progressLabel.setText(WizardStrings.FinishedImport)
def onErrorCopyToButtonClicked(self):
"""

View File

@ -72,6 +72,21 @@ class PowerSongImport(SongImport):
* .song
"""
@staticmethod
def isValidSource(**kwargs):
"""
Checks if source is a PowerSong 1.0 folder:
* is a directory
* contains at least one *.song file
"""
if u'folder' in kwargs:
dir = kwargs[u'folder']
if os.path.isdir(dir):
for file in os.listdir(dir):
if fnmatch.fnmatch(file, u'*.song'):
return True
return False
def doImport(self):
"""
Receive either a list of files or a folder (unicode) to import.

View File

@ -50,6 +50,13 @@ class SongImport(QtCore.QObject):
whether the authors etc already exist and add them or refer to them
as necessary
"""
@staticmethod
def isValidSource(**kwargs):
"""
Override this method to validate the source prior to import.
"""
pass
def __init__(self, manager, **kwargs):
"""
Initialise and create defaults for properties
@ -65,14 +72,16 @@ class SongImport(QtCore.QObject):
self.importSource = kwargs[u'filename']
elif u'filenames' in kwargs:
self.importSource = kwargs[u'filenames']
elif u'folder' in kwargs:
self.importSource = kwargs[u'folder']
else:
raise KeyError(u'Keyword arguments "filename[s]" not supplied.')
raise KeyError(
u'Keyword arguments "filename[s]" or "folder" not supplied.')
log.debug(self.importSource)
self.importWizard = None
self.song = None
self.stopImportFlag = False
self.setDefaults()
self.errorLog = []
QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlp_stop_wizard'), self.stopImport)