Completed refactor of importer.py

This commit is contained in:
Samuel Findlay 2012-05-30 19:11:44 +10:00
parent 465aecc61c
commit e882607360
5 changed files with 199 additions and 175 deletions

View File

@ -44,20 +44,9 @@ class WizardStrings(object):
# Applications/Formats we import from or export to. These get used in # Applications/Formats we import from or export to. These get used in
# multiple places but do not need translating unless you find evidence of # multiple places but do not need translating unless you find evidence of
# the writers translating their own product name. # the writers translating their own product name.
CCLI = u'CCLI/SongSelect'
CSV = u'CSV' CSV = u'CSV'
DB = u'DreamBeam'
EW = u'EasyWorship'
ES = u'EasySlides'
FP = u'Foilpresenter'
OL = u'OpenLyrics'
OS = u'OpenSong' OS = u'OpenSong'
OSIS = u'OSIS' OSIS = u'OSIS'
PS = u'PowerSong 1.0'
SB = u'SongBeamer'
SoF = u'Songs of Fellowship'
SSP = u'SongShow Plus'
WoW = u'Words of Worship'
# These strings should need a good reason to be retranslated elsewhere. # These strings should need a good reason to be retranslated elsewhere.
FinishedImport = translate('OpenLP.Ui', 'Finished import.') FinishedImport = translate('OpenLP.Ui', 'Finished import.')
FormatLabel = translate('OpenLP.Ui', 'Format:') FormatLabel = translate('OpenLP.Ui', 'Format:')
@ -76,10 +65,12 @@ class WizardStrings(object):
PercentSymbolFormat = unicode(translate('OpenLP.Ui', '%p%')) PercentSymbolFormat = unicode(translate('OpenLP.Ui', '%p%'))
Ready = translate('OpenLP.Ui', 'Ready.') Ready = translate('OpenLP.Ui', 'Ready.')
StartingImport = translate('OpenLP.Ui', 'Starting import...') StartingImport = translate('OpenLP.Ui', 'Starting import...')
YouSpecifyFile = unicode(translate('OpenLP.Ui', 'You need to specify at ' YouSpecifyFile = unicode(translate('OpenLP.Ui', 'You need to specify one '
'%s file to import from.', 'A file type e.g. OpenSong'))
YouSpecifyFiles = unicode(translate('OpenLP.Ui', 'You need to specify at '
'least one %s file to import from.', 'A file type e.g. OpenSong')) 'least one %s file to import from.', 'A file type e.g. OpenSong'))
YouSpecifyFolder = unicode(translate('OpenLP.Ui', 'You need to specify a ' YouSpecifyFolder = unicode(translate('OpenLP.Ui', 'You need to specify one '
'%s folder to import from.', 'A file type e.g. OpenSong')) '%s folder to import from.', 'A song format e.g. PowerSong'))
class OpenLPWizard(QtGui.QWizard): class OpenLPWizard(QtGui.QWizard):

View File

@ -36,8 +36,8 @@ from PyQt4 import QtCore, QtGui
from openlp.core.lib import Receiver, SettingsManager, translate from openlp.core.lib import Receiver, SettingsManager, translate
from openlp.core.lib.ui import UiStrings, critical_error_message_box from openlp.core.lib.ui import UiStrings, critical_error_message_box
from openlp.core.ui.wizard import OpenLPWizard, WizardStrings from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
from openlp.plugins.songs.lib.importer import SongFormat, SongFormatAttribute, \ from openlp.plugins.songs.lib.importer import (SongFormat, SongFormatAttr,
SongFormatSelect SongFormatSelect)
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -60,7 +60,7 @@ class SongImportForm(OpenLPWizard):
""" """
self.clipboard = plugin.formParent.clipboard self.clipboard = plugin.formParent.clipboard
self.activeFormat = None self.activeFormat = None
self.formatWidgets = dict([(f, {}) for f in SongFormat.get_formats()]) self.formatWidgets = dict([(f, {}) for f in SongFormat.get_format_list()])
OpenLPWizard.__init__(self, parent, plugin, u'songImportWizard', OpenLPWizard.__init__(self, parent, plugin, u'songImportWizard',
u':/wizards/wizard_importsong.bmp') u':/wizards/wizard_importsong.bmp')
@ -84,16 +84,16 @@ class SongImportForm(OpenLPWizard):
self.activeFormat = index self.activeFormat = index
self.formatStack.setCurrentIndex(self.activeFormat) self.formatStack.setCurrentIndex(self.activeFormat)
next_button = self.button(QtGui.QWizard.NextButton) next_button = self.button(QtGui.QWizard.NextButton)
next_button.setEnabled(SongFormatAttribute.get(self.activeFormat, next_button.setEnabled(SongFormatAttr.get(self.activeFormat,
SongFormatAttribute.availability)) SongFormatAttr.availability))
def customInit(self): def customInit(self):
""" """
Song wizard specific initialisation. Song wizard specific initialisation.
""" """
for format in SongFormat.get_formats(): for format in SongFormat.get_format_list():
if not SongFormatAttribute.get(format, if not SongFormatAttr.get(format,
SongFormatAttribute.availability): SongFormatAttr.availability):
self.formatWidgets[format][u'disabledWidget'].setVisible(True) self.formatWidgets[format][u'disabledWidget'].setVisible(True)
self.formatWidgets[format][u'importWidget'].setVisible(False) self.formatWidgets[format][u'importWidget'].setVisible(False)
@ -101,9 +101,9 @@ class SongImportForm(OpenLPWizard):
""" """
Song wizard specific signals. Song wizard specific signals.
""" """
for format in SongFormat.get_formats(): for format in SongFormat.get_format_list():
select_mode = SongFormatAttribute.get(format, select_mode = SongFormatAttr.get(format,
SongFormatAttribute.select_mode) SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.MultipleFiles: if select_mode == SongFormatSelect.MultipleFiles:
QtCore.QObject.connect(self.formatWidgets[format][u'addButton'], QtCore.QObject.connect(self.formatWidgets[format][u'addButton'],
QtCore.SIGNAL(u'clicked()'), self.onAddButtonClicked) QtCore.SIGNAL(u'clicked()'), self.onAddButtonClicked)
@ -141,7 +141,7 @@ class SongImportForm(OpenLPWizard):
self.formatStack = QtGui.QStackedLayout() self.formatStack = QtGui.QStackedLayout()
self.formatStack.setObjectName(u'FormatStack') self.formatStack.setObjectName(u'FormatStack')
self.disablableFormats = [] self.disablableFormats = []
for self.activeFormat in SongFormat.get_formats(): for self.activeFormat in SongFormat.get_format_list():
self.addFileSelectItem() self.addFileSelectItem()
self.sourceLayout.addLayout(self.formatStack) self.sourceLayout.addLayout(self.formatStack)
self.addPage(self.sourcePage) self.addPage(self.sourcePage)
@ -163,11 +163,13 @@ class SongImportForm(OpenLPWizard):
self.sourcePage.setSubTitle(WizardStrings.ImportSelectLong) self.sourcePage.setSubTitle(WizardStrings.ImportSelectLong)
self.formatLabel.setText(WizardStrings.FormatLabel) self.formatLabel.setText(WizardStrings.FormatLabel)
for format in SongFormat.get_formats(): for format in SongFormat.get_format_list():
self.formatComboBox.setItemText(format, SongFormatAttribute.get( format_name, custom_combo_text, select_mode = SongFormatAttr.get(
format, SongFormatAttribute.combo_box_text)) format, SongFormatAttr.name, SongFormatAttr.combo_box_text,
select_mode = SongFormatAttribute.get(format, SongFormatAttr.select_mode)
SongFormatAttribute.select_mode) combo_box_text = custom_combo_text if custom_combo_text \
else format_name
self.formatComboBox.setItemText(format, combo_box_text)
if select_mode == SongFormatSelect.MultipleFiles: if select_mode == SongFormatSelect.MultipleFiles:
self.formatWidgets[format][u'addButton'].setText( self.formatWidgets[format][u'addButton'].setText(
translate('SongsPlugin.ImportWizardForm', 'Add Files...')) translate('SongsPlugin.ImportWizardForm', 'Add Files...'))
@ -175,17 +177,17 @@ class SongImportForm(OpenLPWizard):
translate('SongsPlugin.ImportWizardForm', 'Remove File(s)')) translate('SongsPlugin.ImportWizardForm', 'Remove File(s)'))
else: else:
if select_mode == SongFormatSelect.SingleFile: if select_mode == SongFormatSelect.SingleFile:
label = 'Filename:' f_label = 'Filename:'
elif select_mode == SongFormatSelect.SingleFolder: elif select_mode == SongFormatSelect.SingleFolder:
label = 'Folder:' f_label = 'Folder:'
self.formatWidgets[format][u'browseButton'].setText( self.formatWidgets[format][u'browseButton'].setText(
UiStrings().Browse) UiStrings().Browse)
self.formatWidgets[format][u'filepathLabel'].setText( self.formatWidgets[format][u'filepathLabel'].setText(
translate('SongsPlugin.ImportWizardForm', label)) translate('SongsPlugin.ImportWizardForm', f_label))
for format in self.disablableFormats: for format in self.disablableFormats:
self.formatWidgets[format][u'disabledLabel'].setText( self.formatWidgets[format][u'disabledLabel'].setText(
SongFormatAttribute.get(format, SongFormatAttr.get(format,
SongFormatAttribute.disabled_label_text)) SongFormatAttr.disabled_label_text))
self.progressPage.setTitle(WizardStrings.Importing) self.progressPage.setTitle(WizardStrings.Importing)
self.progressPage.setSubTitle( self.progressPage.setSubTitle(
@ -221,18 +223,19 @@ class SongImportForm(OpenLPWizard):
format = self.activeFormat format = self.activeFormat
QtCore.QSettings().setValue(u'songs/last import type', QtCore.QSettings().setValue(u'songs/last import type',
format) format)
select_mode = SongFormatAttribute.get(format, select_mode, class_, format_name, custom_error = \
SongFormatAttribute.select_mode) SongFormatAttr.get(format, SongFormatAttr.select_mode,
format_name = SongFormatAttribute.get(format, SongFormatAttr.class_, SongFormatAttr.name,
SongFormatAttribute.name) SongFormatAttr.invalid_source_msg)
class_ = SongFormatAttribute.get(format, error_msg = lambda str: (custom_error if custom_error
SongFormatAttribute.class_) else str % format_name)
if select_mode == SongFormatSelect.MultipleFiles: if select_mode == SongFormatSelect.MultipleFiles:
fileListWidget = self.formatWidgets[format][u'fileListWidget'] fileListWidget = self.formatWidgets[format][u'fileListWidget']
if fileListWidget.count() == 0 or not class_.isValidSource( if fileListWidget.count() == 0 or not class_.isValidSource(
filenames=self.getListOfFiles(fileListWidget)): filenames=self.getListOfFiles(fileListWidget)):
error_msg = error_msg if error_msg else WizardStrings.YouSpecifyFiles % format_name
critical_error_message_box(UiStrings().NFSp, critical_error_message_box(UiStrings().NFSp,
WizardStrings.YouSpecifyFile % format_name) error_msg(WizardStrings.YouSpecifyFiles))
self.formatWidgets[format][u'addButton'].setFocus() self.formatWidgets[format][u'addButton'].setFocus()
return False return False
elif select_mode == SongFormatSelect.SingleFile: elif select_mode == SongFormatSelect.SingleFile:
@ -240,7 +243,7 @@ class SongImportForm(OpenLPWizard):
if filepathEdit.text().isEmpty() or not class_.isValidSource( if filepathEdit.text().isEmpty() or not class_.isValidSource(
filename=filepathEdit.text()): filename=filepathEdit.text()):
critical_error_message_box(UiStrings().NFSs, critical_error_message_box(UiStrings().NFSs,
WizardStrings.YouSpecifyFile % format_name) error_msg(WizardStrings.YouSpecifyFile))
self.formatWidgets[format][u'browseButton'].setFocus() self.formatWidgets[format][u'browseButton'].setFocus()
return False return False
elif select_mode == SongFormatSelect.SingleFolder: elif select_mode == SongFormatSelect.SingleFolder:
@ -248,17 +251,9 @@ class SongImportForm(OpenLPWizard):
if filepathEdit.text().isEmpty() or not class_.isValidSource( if filepathEdit.text().isEmpty() or not class_.isValidSource(
folder=filepathEdit.text()): folder=filepathEdit.text()):
critical_error_message_box(UiStrings().NFdSs, critical_error_message_box(UiStrings().NFdSs,
WizardStrings.YouSpecifyFolder % format_name) error_msg(WizardStrings.YouSpecifyFolder))
self.formatWidgets[format][u'browseButton'].setFocus() self.formatWidgets[format][u'browseButton'].setFocus()
return False return False
# elif source_format == SongFormat.Generic:
# if self.genericFileListWidget.count() == 0:
# critical_error_message_box(UiStrings().NFSp,
# translate('SongsPlugin.ImportWizardForm',
# 'You need to specify at least one document or '
# 'presentation file to import from.'))
# self.genericAddButton.setFocus()
# return False
return True return True
elif self.currentPage() == self.progressPage: elif self.currentPage() == self.progressPage:
return True return True
@ -306,12 +301,12 @@ class SongImportForm(OpenLPWizard):
def onBrowseButtonClicked(self): def onBrowseButtonClicked(self):
format = self.activeFormat format = self.activeFormat
select_mode = SongFormatAttribute.get(format, select_mode = SongFormatAttr.get(format,
SongFormatAttribute.select_mode) SongFormatAttr.select_mode)
name = SongFormatAttribute.get(format, name = SongFormatAttr.get(format,
SongFormatAttribute.name) SongFormatAttr.name)
filter = SongFormatAttribute.get(format, filter = SongFormatAttr.get(format,
SongFormatAttribute.filter) SongFormatAttr.filter)
filepathEdit = self.formatWidgets[format][u'filepathEdit'] filepathEdit = self.formatWidgets[format][u'filepathEdit']
if select_mode == SongFormatSelect.SingleFile: if select_mode == SongFormatSelect.SingleFile:
self.getFileName(WizardStrings.OpenTypeFile % name, filepathEdit, self.getFileName(WizardStrings.OpenTypeFile % name, filepathEdit,
@ -321,30 +316,20 @@ class SongImportForm(OpenLPWizard):
def onAddButtonClicked(self): def onAddButtonClicked(self):
format = self.activeFormat format = self.activeFormat
select_mode = SongFormatAttribute.get(format, select_mode, format_name, filter, custom_title = \
SongFormatAttribute.select_mode) SongFormatAttr.get(format, SongFormatAttr.select_mode,
name = SongFormatAttribute.get(format, SongFormatAttr.name, SongFormatAttr.filter,
SongFormatAttribute.name) SongFormatAttr.get_files_title)
filter = SongFormatAttribute.get(format, title = custom_title if custom_title \
SongFormatAttribute.filter) else WizardStrings.OpenTypeFile % format_name
if select_mode == SongFormatSelect.MultipleFiles: if select_mode == SongFormatSelect.MultipleFiles:
self.getFiles(WizardStrings.OpenTypeFile % name, self.getFiles(title, self.formatWidgets[format][u'FileListWidget'],
self.formatWidgets[format][u'FileListWidget'], filter) filter)
def onRemoveButtonClicked(self): def onRemoveButtonClicked(self):
self.removeSelectedItems( self.removeSelectedItems(
self.formatWidgets[self.activeFormat][u'FileListWidget']) self.formatWidgets[self.activeFormat][u'FileListWidget'])
# def onGenericAddButtonClicked(self):
# """
# Get song database files
# """
# self.getFiles(
# translate('SongsPlugin.ImportWizardForm',
# 'Select Document/Presentation Files'),
# self.genericFileListWidget
# )
def setDefaults(self): def setDefaults(self):
""" """
Set default form values for the song import wizard. Set default form values for the song import wizard.
@ -358,9 +343,9 @@ class SongImportForm(OpenLPWizard):
last_import_type >= self.formatComboBox.count(): last_import_type >= self.formatComboBox.count():
last_import_type = 0 last_import_type = 0
self.formatComboBox.setCurrentIndex(last_import_type) self.formatComboBox.setCurrentIndex(last_import_type)
for format in SongFormat.get_formats(): for format in SongFormat.get_format_list():
select_mode = SongFormatAttribute.get(format, select_mode = SongFormatAttr.get(format,
SongFormatAttribute.select_mode) SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.MultipleFiles: if select_mode == SongFormatSelect.MultipleFiles:
self.formatWidgets[format][u'fileListWidget'].clear() self.formatWidgets[format][u'fileListWidget'].clear()
else: else:
@ -385,8 +370,8 @@ class SongImportForm(OpenLPWizard):
the actual importing. the actual importing.
""" """
source_format = self.activeFormat source_format = self.activeFormat
select_mode = SongFormatAttribute.get(source_format, select_mode = SongFormatAttr.get(source_format,
SongFormatAttribute.select_mode) SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.SingleFile: if select_mode == SongFormatSelect.SingleFile:
importer = self.plugin.importSongs(source_format, importer = self.plugin.importSongs(source_format,
filename=unicode( filename=unicode(
@ -422,19 +407,19 @@ class SongImportForm(OpenLPWizard):
def addFileSelectItem(self): def addFileSelectItem(self):
format = self.activeFormat format = self.activeFormat
prefix = SongFormatAttribute.get(format, SongFormatAttribute.prefix) prefix = SongFormatAttr.get(format, SongFormatAttr.prefix)
obj_prefix = SongFormatAttribute.get(format, obj_prefix = SongFormatAttr.get(format,
SongFormatAttribute.obj_prefix) SongFormatAttr.obj_prefix)
can_disable = SongFormatAttribute.get(format, can_disable = SongFormatAttr.get(format,
SongFormatAttribute.can_disable) SongFormatAttr.can_disable)
select_mode = SongFormatAttribute.get(format, select_mode = SongFormatAttr.get(format,
SongFormatAttribute.select_mode) SongFormatAttr.select_mode)
if not obj_prefix: if not obj_prefix:
obj_prefix = prefix obj_prefix = prefix
page = QtGui.QWidget() page = QtGui.QWidget()
page.setObjectName(obj_prefix + u'Page') page.setObjectName(obj_prefix + u'Page')
if can_disable: if can_disable:
importWidget = self.disablableWidget(page, prefix, obj_prefix) importWidget = self.disablableWidget(page, obj_prefix)
else: else:
importWidget = page importWidget = page
importLayout = QtGui.QVBoxLayout(importWidget) importLayout = QtGui.QVBoxLayout(importWidget)
@ -456,6 +441,10 @@ class SongImportForm(OpenLPWizard):
filepathLayout.addWidget(browseButton) filepathLayout.addWidget(browseButton)
importLayout.addLayout(filepathLayout) importLayout.addLayout(filepathLayout)
importLayout.addSpacerItem(self.stackSpacer) importLayout.addSpacerItem(self.stackSpacer)
self.formatWidgets[format][u'filepathLabel'] = filepathLabel
self.formatWidgets[format][u'filepathLayout'] = filepathLayout
self.formatWidgets[format][u'filepathEdit'] = filepathEdit
self.formatWidgets[format][u'browseButton'] = browseButton
elif select_mode == SongFormatSelect.MultipleFiles: elif select_mode == SongFormatSelect.MultipleFiles:
fileListWidget = QtGui.QListWidget(importWidget) fileListWidget = QtGui.QListWidget(importWidget)
fileListWidget.setSelectionMode( fileListWidget.setSelectionMode(
@ -474,23 +463,16 @@ class SongImportForm(OpenLPWizard):
removeButton.setObjectName(obj_prefix + u'RemoveButton') removeButton.setObjectName(obj_prefix + u'RemoveButton')
buttonLayout.addWidget(removeButton) buttonLayout.addWidget(removeButton)
importLayout.addLayout(buttonLayout) importLayout.addLayout(buttonLayout)
self.formatStack.addWidget(page)
#setattr(self, prefix + u'Page', page)
if select_mode == SongFormatSelect.SingleFile or \
select_mode == SongFormatSelect.SingleFolder:
self.formatWidgets[format][u'filepathLabel'] = filepathLabel
#setattr(self, prefix + u'FileLayout', fileLayout)
self.formatWidgets[format][u'filepathEdit'] = filepathEdit
self.formatWidgets[format][u'browseButton'] = browseButton
else:
self.formatWidgets[format][u'fileListWidget'] = fileListWidget self.formatWidgets[format][u'fileListWidget'] = fileListWidget
#setattr(self, prefix + u'ButtonLayout', buttonLayout) self.formatWidgets[format][u'buttonLayout'] = buttonLayout
self.formatWidgets[format][u'addButton'] = addButton self.formatWidgets[format][u'addButton'] = addButton
self.formatWidgets[format][u'removeButton'] = removeButton self.formatWidgets[format][u'removeButton'] = removeButton
#setattr(self, prefix + u'ImportLayout', importLayout) self.formatStack.addWidget(page)
self.formatWidgets[format][u'page'] = page
self.formatWidgets[format][u'importLayout'] = importLayout
self.formatComboBox.addItem(u'') self.formatComboBox.addItem(u'')
def disablableWidget(self, page, prefix, obj_prefix): def disablableWidget(self, page, obj_prefix):
format = self.activeFormat format = self.activeFormat
self.disablableFormats.append(format) self.disablableFormats.append(format)
layout = QtGui.QVBoxLayout(page) layout = QtGui.QVBoxLayout(page)
@ -512,9 +494,9 @@ class SongImportForm(OpenLPWizard):
importWidget = QtGui.QWidget(page) importWidget = QtGui.QWidget(page)
importWidget.setObjectName(obj_prefix + u'ImportWidget') importWidget.setObjectName(obj_prefix + u'ImportWidget')
layout.addWidget(importWidget) layout.addWidget(importWidget)
#setattr(self, prefix + u'Layout', layout) self.formatWidgets[format][u'layout'] = layout
self.formatWidgets[format][u'disabledWidget'] = disabledWidget self.formatWidgets[format][u'disabledWidget'] = disabledWidget
#setattr(self, prefix + u'DisabledLayout', disabledLayout) self.formatWidgets[format][u'disabledLayout'] = disabledLayout
self.formatWidgets[format][u'disabledLabel'] = disabledLabel self.formatWidgets[format][u'disabledLabel'] = disabledLabel
self.formatWidgets[format][u'importWidget'] = importWidget self.formatWidgets[format][u'importWidget'] = importWidget
return importWidget return importWidget

View File

@ -66,9 +66,8 @@ except ImportError:
class SongFormat(object): class SongFormat(object):
""" """
This is a special enumeration class that holds the various types of songs, This is a special enumeration class that holds the various types of song
plus a few helper functions to facilitate generic handling of song types importers and some helper functions to facilitate access.
for importing.
""" """
Unknown = -1 Unknown = -1
OpenLyrics = 0 OpenLyrics = 0
@ -89,7 +88,7 @@ class SongFormat(object):
#CSV = 15 #CSV = 15
@staticmethod @staticmethod
def get_formats(): def get_format_list():
""" """
Return a list of the supported song formats. Return a list of the supported song formats.
""" """
@ -111,55 +110,86 @@ class SongFormat(object):
SongFormat.WordsOfWorship SongFormat.WordsOfWorship
] ]
class SongFormatAttribute(object): class SongFormatAttr(object):
# Required attributes: """
This is a special static class that holds the attributes of each song format
to aid the importing of each song type.
The various definable attributes are enumerated as follows:
Required attributes for each song format:
* ``class_`` Import class, e.g. OpenLyricsImport
* ``name`` Name of this format, e.g. u'OpenLyrics'
* ``prefix`` Prefix for objects. Use camelCase, e.g. u'openLyrics'
See ``SongImportForm.addFileSelectItem()``.
Optional attributes for each song format:
* ``obj_prefix`` Alternate prefix for objects.
See ``SongImportForm.addFileSelectItem()``.
* ``can_disable`` Whether song format is disablable.
* ``availability`` Whether song format is available.
* ``select_mode`` Whether format accepts single file, multiple files, or
single folder.
* ``filter`` File extension filter for Open File/Folder dialog.
Optional/custom text values for SongImportForm widgets:
* ``combo_box_text`` Combo box selector (default is format name).
* ``disabled_label_text`` Required for disablable song formats.
* ``get_files_title`` Title for Open File/Folder dialog (default includes
format name).
* ``invalid_source_msg`` Message shown when source does not validate.
"""
# Required attributes
class_ = 0 class_ = 0
name = 1 name = 1
prefix = 2 prefix = 2
# Optional attributes: # Optional attributes
obj_prefix = 10 obj_prefix = 10
can_disable = 11 can_disable = 11
availability = 12 availability = 12
select_mode = 13 select_mode = 13
# Required widget Text: filter = 14
# Optional/custom text values
combo_box_text = 20 combo_box_text = 20
# Optional widget Text disabled_label_text = 21
disabled_label_text = 30 get_files_title = 22
filter = 31 invalid_source_msg = 23
# Set attribute defaults (if not specified here, default is None) # Set optional attribute defaults
_defaults = { _defaults = {
obj_prefix: None, obj_prefix: None,
can_disable: False, can_disable: False,
availability: True, availability: True,
select_mode: SongFormatSelect.MultipleFiles, select_mode: SongFormatSelect.MultipleFiles,
filter: u'',
combo_box_text: None,
disabled_label_text: u'', disabled_label_text: u'',
filter: u'' get_files_title: None,
invalid_source_msg: None
} }
# Set attribute values # Set attribute values
_attributes = { _attributes = {
SongFormat.OpenLyrics: { SongFormat.OpenLyrics: {
class_: OpenLyricsImport, class_: OpenLyricsImport,
name: WizardStrings.OL, name: u'OpenLyrics',
prefix: u'openLyrics', prefix: u'openLyrics',
obj_prefix: u'OpenLyrics', obj_prefix: u'OpenLyrics',
can_disable: True, can_disable: True,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'OpenLyrics Files'),
combo_box_text: translate('SongsPlugin.ImportWizardForm', combo_box_text: translate('SongsPlugin.ImportWizardForm',
'OpenLyrics or OpenLP 2.0 Exported Song'), 'OpenLyrics or OpenLP 2.0 Exported Song'),
disabled_label_text: translate('SongsPlugin.ImportWizardForm', disabled_label_text: translate('SongsPlugin.ImportWizardForm',
'The OpenLyrics importer has not yet been developed, but as ' 'The OpenLyrics importer has not yet been developed, but as '
'you can see, we are still intending to do so. Hopefully it ' 'you can see, we are still intending to do so. Hopefully it '
'will be in the next release.'), 'will be in the next release.')
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'OpenLyrics Files')
}, },
SongFormat.OpenLP2: { SongFormat.OpenLP2: {
class_: OpenLPSongImport, class_: OpenLPSongImport,
name: UiStrings().OLPV2, name: UiStrings().OLPV2,
prefix: u'openLP2', prefix: u'openLP2',
select_mode: SongFormatSelect.SingleFile, select_mode: SongFormatSelect.SingleFile,
combo_box_text: UiStrings().OLPV2,
filter: u'%s (*.sqlite)' % (translate( filter: u'%s (*.sqlite)' % (translate(
'SongsPlugin.ImportWizardForm', 'OpenLP 2.0 Databases')) 'SongsPlugin.ImportWizardForm', 'OpenLP 2.0 Databases'))
}, },
@ -169,61 +199,59 @@ class SongFormatAttribute(object):
prefix: u'openLP1', prefix: u'openLP1',
can_disable: True, can_disable: True,
select_mode: SongFormatSelect.SingleFile, select_mode: SongFormatSelect.SingleFile,
combo_box_text: UiStrings().OLPV1,
disabled_label_text: WizardStrings.NoSqlite,
filter: u'%s (*.olp)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.olp)' % translate('SongsPlugin.ImportWizardForm',
'openlp.org v1.x Databases') 'openlp.org v1.x Databases'),
disabled_label_text: WizardStrings.NoSqlite
}, },
SongFormat.Generic: { SongFormat.Generic: {
class_: OooImport, class_: OooImport,
# name: , name: translate('SongsPlugin.ImportWizardForm',
'Generic Document/Presentation'),
prefix: u'generic', prefix: u'generic',
can_disable: True, can_disable: True,
combo_box_text: translate('SongsPlugin.ImportWizardForm',
'Generic Document/Presentation'),
disabled_label_text: translate('SongsPlugin.ImportWizardForm', disabled_label_text: translate('SongsPlugin.ImportWizardForm',
'The generic document/presentation importer has been disabled ' 'The generic document/presentation importer has been disabled '
'because OpenLP cannot access OpenOffice or LibreOffice.') 'because OpenLP cannot access OpenOffice or LibreOffice.'),
get_files_title: translate('SongsPlugin.ImportWizardForm',
'Select Document/Presentation Files'),
invalid_source_msg: translate('SongsPlugin.ImportWizardForm',
'You need to specify at least one document or presentation '
'file to import from.')
}, },
SongFormat.CCLI: { SongFormat.CCLI: {
class_: CCLIFileImport, class_: CCLIFileImport,
name: WizardStrings.CCLI, name: u'CCLI/SongSelect',
prefix: u'ccli', prefix: u'ccli',
combo_box_text: WizardStrings.CCLI,
filter: u'%s (*.usr *.txt)' % translate( filter: u'%s (*.usr *.txt)' % translate(
'SongsPlugin.ImportWizardForm', 'CCLI SongSelect Files') 'SongsPlugin.ImportWizardForm', 'CCLI SongSelect Files')
}, },
SongFormat.DreamBeam: { SongFormat.DreamBeam: {
class_: DreamBeamImport, class_: DreamBeamImport,
name: WizardStrings.DB, name: u'DreamBeam',
prefix: u'dreamBeam', prefix: u'dreamBeam',
combo_box_text: WizardStrings.DB,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'DreamBeam Song Files') 'DreamBeam Song Files')
}, },
SongFormat.EasySlides: { SongFormat.EasySlides: {
class_: EasySlidesImport, class_: EasySlidesImport,
name: WizardStrings.ES, name: u'EasySlides',
prefix: u'easySlides', prefix: u'easySlides',
select_mode: SongFormatSelect.SingleFile, select_mode: SongFormatSelect.SingleFile,
combo_box_text: WizardStrings.ES,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'EasySlides XML File') 'EasySlides XML File')
}, },
SongFormat.EasyWorship: { SongFormat.EasyWorship: {
class_: EasyWorshipSongImport, class_: EasyWorshipSongImport,
name: WizardStrings.EW, name: u'EasyWorship',
prefix: u'ew', prefix: u'ew',
select_mode: SongFormatSelect.SingleFile, select_mode: SongFormatSelect.SingleFile,
combo_box_text: WizardStrings.EW,
filter: u'%s (*.db)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.db)' % translate('SongsPlugin.ImportWizardForm',
'EasyWorship Song Database') 'EasyWorship Song Database')
}, },
SongFormat.FoilPresenter: { SongFormat.FoilPresenter: {
class_: FoilPresenterImport, class_: FoilPresenterImport,
name: WizardStrings.FP, name: u'Foilpresenter',
prefix: u'foilPresenter', prefix: u'foilPresenter',
combo_box_text: WizardStrings.FP,
filter: u'%s (*.foil)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.foil)' % translate('SongsPlugin.ImportWizardForm',
'Foilpresenter Song Files') 'Foilpresenter Song Files')
}, },
@ -231,49 +259,43 @@ class SongFormatAttribute(object):
class_: OpenSongImport, class_: OpenSongImport,
name: WizardStrings.OS, name: WizardStrings.OS,
prefix: u'openSong', prefix: u'openSong',
obj_prefix: u'OpenSong', obj_prefix: u'OpenSong'
combo_box_text: WizardStrings.OS
}, },
SongFormat.PowerSong: { SongFormat.PowerSong: {
class_: PowerSongImport, class_: PowerSongImport,
name: WizardStrings.PS, name: u'PowerSong 1.0',
prefix: u'powerSong', prefix: u'powerSong',
select_mode: SongFormatSelect.SingleFolder, select_mode: SongFormatSelect.SingleFolder
combo_box_text: WizardStrings.PS
}, },
SongFormat.SongBeamer: { SongFormat.SongBeamer: {
class_: SongBeamerImport, class_: SongBeamerImport,
name: WizardStrings.SB, name: u'SongBeamer',
prefix: u'songBeamer', prefix: u'songBeamer',
combo_box_text: WizardStrings.SB,
filter: u'%s (*.sng)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.sng)' % translate('SongsPlugin.ImportWizardForm',
'SongBeamer Files') 'SongBeamer Files')
}, },
SongFormat.SongShowPlus: { SongFormat.SongShowPlus: {
class_: SongShowPlusImport, class_: SongShowPlusImport,
name: WizardStrings.SSP, name: u'SongShow Plus',
prefix: u'songShowPlus', prefix: u'songShowPlus',
combo_box_text: WizardStrings.SSP,
filter: u'%s (*.sbsong)' % translate('SongsPlugin.ImportWizardForm', filter: u'%s (*.sbsong)' % translate('SongsPlugin.ImportWizardForm',
'SongShow Plus Song Files') 'SongShow Plus Song Files')
}, },
SongFormat.SongsOfFellowship: { SongFormat.SongsOfFellowship: {
class_: SofImport, class_: SofImport,
name: WizardStrings.SoF, name: u'Songs of Fellowship',
prefix: u'songsOfFellowship', prefix: u'songsOfFellowship',
can_disable: True, can_disable: True,
combo_box_text: WizardStrings.SoF, filter: u'%s (*.rtf)' % translate('SongsPlugin.ImportWizardForm',
'Songs Of Fellowship Song Files'),
disabled_label_text: translate('SongsPlugin.ImportWizardForm', disabled_label_text: translate('SongsPlugin.ImportWizardForm',
'The Songs of Fellowship importer has been disabled because ' 'The Songs of Fellowship importer has been disabled because '
'OpenLP cannot access OpenOffice or LibreOffice.'), 'OpenLP cannot access OpenOffice or LibreOffice.')
filter: u'%s (*.rtf)' % translate('SongsPlugin.ImportWizardForm',
'Songs Of Fellowship Song Files')
}, },
SongFormat.WordsOfWorship: { SongFormat.WordsOfWorship: {
class_: WowImport, class_: WowImport,
name: WizardStrings.WoW, name: u'Words of Worship',
prefix: u'wordsOfWorship', prefix: u'wordsOfWorship',
combo_box_text: WizardStrings.WoW,
filter: u'%s (*.wsg *.wow-song)' % translate( filter: u'%s (*.wsg *.wow-song)' % translate(
'SongsPlugin.ImportWizardForm', 'Words Of Worship Song Files') 'SongsPlugin.ImportWizardForm', 'Words Of Worship Song Files')
# }, # },
@ -282,31 +304,59 @@ class SongFormatAttribute(object):
# name: WizardStrings.CSV, # name: WizardStrings.CSV,
# prefix: u'csv', # prefix: u'csv',
# obj_prefix: u'CSV', # obj_prefix: u'CSV',
# select_mode: SongFormatSelect.SingleFile, # select_mode: SongFormatSelect.SingleFile
# combo_box_text: WizardStrings.CSV
} }
} }
@staticmethod @staticmethod
def get(format, attribute): def get(format, *attributes):
default = _defaults.get(attribute) """
return SongFormat._attributes[format].get(attribute, default) Return requested song format attribute(s).
``format``
A song format from SongFormat.
``*attributes``
Zero or more song format attributes from SongFormatAttr.
Return type depends on number of supplied attributes:
* 0 : Return dict containing all defined attributes for the format.
* 1 : Return the attribute value.
* >1 : Return tuple of requested attribute values.
"""
if not attributes:
return SongFormat._attributes.get(format)
elif len(attributes) == 1:
default = _defaults.get(attributes[0])
return SongFormat._attributes[format].get(attributes[0], default)
else:
values = []
for attr in attributes:
default = _defaults.get(attr)
values.append(SongFormat._attributes[format].get(attr, default))
return tuple(values)
@staticmethod @staticmethod
def set(format, attribute, value): def set(format, attribute, value):
"""
Set specified song format attribute to supplied value.
"""
SongFormat._attributes[format][attribute] = value SongFormat._attributes[format][attribute] = value
class SongFormatSelect(object): class SongFormatSelect(object):
"""
This is a special enumeration class listing available file selection modes.
"""
SingleFile = 0 SingleFile = 0
MultipleFiles = 1 MultipleFiles = 1
SingleFolder = 2 SingleFolder = 2
SongFormatAttribute.set( SongFormatAttr.set(
SongFormat.OpenLP1, SongFormatAttribute.availability, HAS_OPENLP1) SongFormat.OpenLP1, SongFormatAttr.availability, HAS_OPENLP1)
SongFormatAttribute.set( SongFormatAttr.set(
SongFormat.SongsOfFellowship, SongFormatAttribute.availability, HAS_SOF) SongFormat.SongsOfFellowship, SongFormatAttr.availability, HAS_SOF)
SongFormatAttribute.set( SongFormatAttr.set(
SongFormat.Generic, SongFormatAttribute.availability, HAS_OOO) SongFormat.Generic, SongFormatAttr.availability, HAS_OOO)
__all__ = [u'SongFormat'] __all__ = [u'SongFormat', u'SongFormatAttr', u'SongFormatSelect']

View File

@ -33,8 +33,8 @@ import fnmatch
import os import os
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
from openlp.plugins.songs.lib.importer import SongFormat, SongFormatAttr
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -91,6 +91,8 @@ class PowerSongImport(SongImport):
""" """
Receive either a list of files or a folder (unicode) to import. Receive either a list of files or a folder (unicode) to import.
""" """
PS_string = SongFormatAttr.get(SongFormat.PowerSong,
SongFormatAttr.name)
if isinstance(self.importSource, unicode): if isinstance(self.importSource, unicode):
if os.path.isdir(self.importSource): if os.path.isdir(self.importSource):
dir = self.importSource dir = self.importSource
@ -104,7 +106,7 @@ class PowerSongImport(SongImport):
self.logError(unicode(translate('SongsPlugin.PowerSongImport', self.logError(unicode(translate('SongsPlugin.PowerSongImport',
'No songs to import.')), 'No songs to import.')),
unicode(translate('SongsPlugin.PowerSongImport', unicode(translate('SongsPlugin.PowerSongImport',
'No %s files found.' % WizardStrings.PS))) 'No %s files found.' % PS_string)))
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:
@ -124,7 +126,7 @@ class PowerSongImport(SongImport):
self.logError(os.path.basename(file), unicode( self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Unexpected byte value.' 'Invalid %s file. Unexpected byte value.'
% WizardStrings.PS))) % PS_string)))
break break
else: else:
if label == u'TITLE': if label == u'TITLE':
@ -142,15 +144,14 @@ class PowerSongImport(SongImport):
if not self.title: if not self.title:
self.logError(os.path.basename(file), unicode( self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Missing "TITLE" header.' 'Invalid %s file. Missing "TITLE" header.' % PS_string)))
% 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(self.title, unicode( self.logError(self.title, unicode(
translate('SongsPlugin.PowerSongImport', translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Missing "COPYRIGHTLINE" ' 'Invalid %s file. Missing "COPYRIGHTLINE" '
'header.' % WizardStrings.PS))) 'header.' % PS_string)))
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:

View File

@ -39,7 +39,7 @@ from openlp.core.utils.actions import ActionList
from openlp.plugins.songs.lib import clean_song, upgrade, SongMediaItem, \ from openlp.plugins.songs.lib import clean_song, upgrade, SongMediaItem, \
SongsTab SongsTab
from openlp.plugins.songs.lib.db import init_schema, Song from openlp.plugins.songs.lib.db import init_schema, Song
from openlp.plugins.songs.lib.importer import SongFormatAttribute from openlp.plugins.songs.lib.importer import SongFormatAttr
from openlp.plugins.songs.lib.olpimport import OpenLPSongImport from openlp.plugins.songs.lib.olpimport import OpenLPSongImport
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -194,7 +194,7 @@ class SongsPlugin(Plugin):
self.manager.save_object(song) self.manager.save_object(song)
def importSongs(self, format, **kwargs): def importSongs(self, format, **kwargs):
class_ = SongFormatAttribute.get(format, SongFormatAttribute.class_) class_ = SongFormatAttr.get(format, SongFormatAttr.class_)
importer = class_(self.manager, **kwargs) importer = class_(self.manager, **kwargs)
importer.register(self.mediaItem.importWizard) importer.register(self.mediaItem.importWizard)
return importer return importer