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

View File

@ -72,6 +72,21 @@ class PowerSongImport(SongImport):
* .song * .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): def doImport(self):
""" """
Receive either a list of files or a folder (unicode) to import. 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 whether the authors etc already exist and add them or refer to them
as necessary as necessary
""" """
@staticmethod
def isValidSource(**kwargs):
"""
Override this method to validate the source prior to import.
"""
pass
def __init__(self, manager, **kwargs): def __init__(self, manager, **kwargs):
""" """
Initialise and create defaults for properties Initialise and create defaults for properties
@ -65,14 +72,16 @@ class SongImport(QtCore.QObject):
self.importSource = kwargs[u'filename'] self.importSource = kwargs[u'filename']
elif u'filenames' in kwargs: elif u'filenames' in kwargs:
self.importSource = kwargs[u'filenames'] self.importSource = kwargs[u'filenames']
elif u'folder' in kwargs:
self.importSource = kwargs[u'folder']
else: 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) log.debug(self.importSource)
self.importWizard = None self.importWizard = None
self.song = None self.song = None
self.stopImportFlag = False self.stopImportFlag = False
self.setDefaults() self.setDefaults()
self.errorLog = []
QtCore.QObject.connect(Receiver.get_receiver(), QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlp_stop_wizard'), self.stopImport) QtCore.SIGNAL(u'openlp_stop_wizard'), self.stopImport)