Adapted PowerSongImport class to receive a folder

This commit is contained in:
Samuel Findlay 2012-05-07 23:10:26 +10:00
parent 28d9a05731
commit 3483231a32
2 changed files with 32 additions and 15 deletions

View File

@ -398,7 +398,8 @@ class SongImportForm(OpenLPWizard):
self.openLP1BrowseButton.setFocus() self.openLP1BrowseButton.setFocus()
return False return False
elif source_format == SongFormat.PowerSong: elif source_format == SongFormat.PowerSong:
if self.powerSongFilenameEdit.text().isEmpty(): if self.powerSongFilenameEdit.text().isEmpty() or \
not os.path.isdir(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()

View File

@ -29,8 +29,12 @@ The :mod:`powersongimport` module provides the functionality for importing
PowerSong songs into the OpenLP database. PowerSong songs into the OpenLP database.
""" """
import logging import logging
import glob
import os
import fnmatch
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.core.ui.wizard import WizardStrings
from openlp.plugins.songs.lib.songimport import SongImport from openlp.plugins.songs.lib.songimport import SongImport
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -71,11 +75,22 @@ class PowerSongImport(SongImport):
def doImport(self): def doImport(self):
""" """
Receive a list of files to import. Receive either a list of files or a folder (unicode) to import.
""" """
if not isinstance(self.importSource, list): if isinstance(self.importSource, unicode):
if os.path.isdir(self.importSource):
dir = self.importSource
self.importSource = []
for file in os.listdir(dir):
if fnmatch.fnmatch(file, u'*.song'):
self.importSource.append(os.path.join(dir, file))
else:
self.importSource = u''
if not self.importSource or not isinstance(self.importSource, list):
self.logError(unicode(translate('SongsPlugin.PowerSongImport', self.logError(unicode(translate('SongsPlugin.PowerSongImport',
'No files to import.'))) 'No songs to import.')),
unicode(translate('SongsPlugin.PowerSongImport',
'No %s files found.' % WizardStrings.PS)))
return return
self.importWizard.progressBar.setMaximum(len(self.importSource)) self.importWizard.progressBar.setMaximum(len(self.importSource))
for file in self.importSource: for file in self.importSource:
@ -92,9 +107,10 @@ class PowerSongImport(SongImport):
field = self._readString(song_data) field = self._readString(song_data)
except ValueError: except ValueError:
parse_error = True parse_error = True
self.logError(file, unicode( self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'Invalid PowerSong file. Unexpected byte value.'))) 'Invalid %s file. Unexpected byte value.'
% WizardStrings.PS)))
break break
else: else:
if label == u'TITLE': if label == u'TITLE':
@ -110,26 +126,26 @@ class PowerSongImport(SongImport):
continue continue
# Check that file had TITLE field # Check that file had TITLE field
if not self.title: if not self.title:
self.logError(file, unicode( self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'Invalid PowerSong file. Missing "TITLE" header.'))) 'Invalid %s file. Missing "TITLE" header.'
% WizardStrings.PS)))
continue continue
# Check that file had COPYRIGHTLINE label # Check that file had COPYRIGHTLINE label
if not found_copyright: if not found_copyright:
self.logError(file, unicode( self.logError(self.title, unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'"%s" Invalid PowerSong file. Missing "COPYRIGHTLINE" ' 'Invalid %s file. Missing "COPYRIGHTLINE" '
'header.' % self.title))) 'header.' % WizardStrings.PS)))
continue continue
# Check that file had at least one verse # Check that file had at least one verse
if not self.verses: if not self.verses:
self.logError(file, unicode( self.logError(self.title, unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'"%s" Verses not found. Missing "PART" header.' 'Verses not found. Missing "PART" header.')))
% self.title)))
continue continue
if not self.finish(): if not self.finish():
self.logError(file) self.logError(self.title)
def _readString(self, file_object): def _readString(self, file_object):
""" """