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
# multiple places but do not need translating unless you find evidence of
# the writers translating their own product name.
CCLI = u'CCLI/SongSelect'
CSV = u'CSV'
DB = u'DreamBeam'
EW = u'EasyWorship'
ES = u'EasySlides'
FP = u'Foilpresenter'
OL = u'OpenLyrics'
OS = u'OpenSong'
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.
FinishedImport = translate('OpenLP.Ui', 'Finished import.')
FormatLabel = translate('OpenLP.Ui', 'Format:')
@ -76,10 +65,12 @@ class WizardStrings(object):
PercentSymbolFormat = unicode(translate('OpenLP.Ui', '%p%'))
Ready = translate('OpenLP.Ui', 'Ready.')
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'))
YouSpecifyFolder = unicode(translate('OpenLP.Ui', 'You need to specify a '
'%s folder to import from.', 'A file type e.g. OpenSong'))
YouSpecifyFolder = unicode(translate('OpenLP.Ui', 'You need to specify one '
'%s folder to import from.', 'A song format e.g. PowerSong'))
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.ui import UiStrings, critical_error_message_box
from openlp.core.ui.wizard import OpenLPWizard, WizardStrings
from openlp.plugins.songs.lib.importer import SongFormat, SongFormatAttribute, \
SongFormatSelect
from openlp.plugins.songs.lib.importer import (SongFormat, SongFormatAttr,
SongFormatSelect)
log = logging.getLogger(__name__)
@ -60,7 +60,7 @@ class SongImportForm(OpenLPWizard):
"""
self.clipboard = plugin.formParent.clipboard
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',
u':/wizards/wizard_importsong.bmp')
@ -84,16 +84,16 @@ class SongImportForm(OpenLPWizard):
self.activeFormat = index
self.formatStack.setCurrentIndex(self.activeFormat)
next_button = self.button(QtGui.QWizard.NextButton)
next_button.setEnabled(SongFormatAttribute.get(self.activeFormat,
SongFormatAttribute.availability))
next_button.setEnabled(SongFormatAttr.get(self.activeFormat,
SongFormatAttr.availability))
def customInit(self):
"""
Song wizard specific initialisation.
"""
for format in SongFormat.get_formats():
if not SongFormatAttribute.get(format,
SongFormatAttribute.availability):
for format in SongFormat.get_format_list():
if not SongFormatAttr.get(format,
SongFormatAttr.availability):
self.formatWidgets[format][u'disabledWidget'].setVisible(True)
self.formatWidgets[format][u'importWidget'].setVisible(False)
@ -101,9 +101,9 @@ class SongImportForm(OpenLPWizard):
"""
Song wizard specific signals.
"""
for format in SongFormat.get_formats():
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
for format in SongFormat.get_format_list():
select_mode = SongFormatAttr.get(format,
SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.MultipleFiles:
QtCore.QObject.connect(self.formatWidgets[format][u'addButton'],
QtCore.SIGNAL(u'clicked()'), self.onAddButtonClicked)
@ -141,7 +141,7 @@ class SongImportForm(OpenLPWizard):
self.formatStack = QtGui.QStackedLayout()
self.formatStack.setObjectName(u'FormatStack')
self.disablableFormats = []
for self.activeFormat in SongFormat.get_formats():
for self.activeFormat in SongFormat.get_format_list():
self.addFileSelectItem()
self.sourceLayout.addLayout(self.formatStack)
self.addPage(self.sourcePage)
@ -163,11 +163,13 @@ class SongImportForm(OpenLPWizard):
self.sourcePage.setSubTitle(WizardStrings.ImportSelectLong)
self.formatLabel.setText(WizardStrings.FormatLabel)
for format in SongFormat.get_formats():
self.formatComboBox.setItemText(format, SongFormatAttribute.get(
format, SongFormatAttribute.combo_box_text))
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
for format in SongFormat.get_format_list():
format_name, custom_combo_text, select_mode = SongFormatAttr.get(
format, SongFormatAttr.name, SongFormatAttr.combo_box_text,
SongFormatAttr.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:
self.formatWidgets[format][u'addButton'].setText(
translate('SongsPlugin.ImportWizardForm', 'Add Files...'))
@ -175,17 +177,17 @@ class SongImportForm(OpenLPWizard):
translate('SongsPlugin.ImportWizardForm', 'Remove File(s)'))
else:
if select_mode == SongFormatSelect.SingleFile:
label = 'Filename:'
f_label = 'Filename:'
elif select_mode == SongFormatSelect.SingleFolder:
label = 'Folder:'
f_label = 'Folder:'
self.formatWidgets[format][u'browseButton'].setText(
UiStrings().Browse)
self.formatWidgets[format][u'filepathLabel'].setText(
translate('SongsPlugin.ImportWizardForm', label))
translate('SongsPlugin.ImportWizardForm', f_label))
for format in self.disablableFormats:
self.formatWidgets[format][u'disabledLabel'].setText(
SongFormatAttribute.get(format,
SongFormatAttribute.disabled_label_text))
SongFormatAttr.get(format,
SongFormatAttr.disabled_label_text))
self.progressPage.setTitle(WizardStrings.Importing)
self.progressPage.setSubTitle(
@ -221,18 +223,19 @@ class SongImportForm(OpenLPWizard):
format = self.activeFormat
QtCore.QSettings().setValue(u'songs/last import type',
format)
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
format_name = SongFormatAttribute.get(format,
SongFormatAttribute.name)
class_ = SongFormatAttribute.get(format,
SongFormatAttribute.class_)
select_mode, class_, format_name, custom_error = \
SongFormatAttr.get(format, SongFormatAttr.select_mode,
SongFormatAttr.class_, SongFormatAttr.name,
SongFormatAttr.invalid_source_msg)
error_msg = lambda str: (custom_error if custom_error
else str % format_name)
if select_mode == SongFormatSelect.MultipleFiles:
fileListWidget = self.formatWidgets[format][u'fileListWidget']
if fileListWidget.count() == 0 or not class_.isValidSource(
filenames=self.getListOfFiles(fileListWidget)):
error_msg = error_msg if error_msg else WizardStrings.YouSpecifyFiles % format_name
critical_error_message_box(UiStrings().NFSp,
WizardStrings.YouSpecifyFile % format_name)
error_msg(WizardStrings.YouSpecifyFiles))
self.formatWidgets[format][u'addButton'].setFocus()
return False
elif select_mode == SongFormatSelect.SingleFile:
@ -240,7 +243,7 @@ class SongImportForm(OpenLPWizard):
if filepathEdit.text().isEmpty() or not class_.isValidSource(
filename=filepathEdit.text()):
critical_error_message_box(UiStrings().NFSs,
WizardStrings.YouSpecifyFile % format_name)
error_msg(WizardStrings.YouSpecifyFile))
self.formatWidgets[format][u'browseButton'].setFocus()
return False
elif select_mode == SongFormatSelect.SingleFolder:
@ -248,17 +251,9 @@ class SongImportForm(OpenLPWizard):
if filepathEdit.text().isEmpty() or not class_.isValidSource(
folder=filepathEdit.text()):
critical_error_message_box(UiStrings().NFdSs,
WizardStrings.YouSpecifyFolder % format_name)
error_msg(WizardStrings.YouSpecifyFolder))
self.formatWidgets[format][u'browseButton'].setFocus()
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
elif self.currentPage() == self.progressPage:
return True
@ -306,12 +301,12 @@ class SongImportForm(OpenLPWizard):
def onBrowseButtonClicked(self):
format = self.activeFormat
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
name = SongFormatAttribute.get(format,
SongFormatAttribute.name)
filter = SongFormatAttribute.get(format,
SongFormatAttribute.filter)
select_mode = SongFormatAttr.get(format,
SongFormatAttr.select_mode)
name = SongFormatAttr.get(format,
SongFormatAttr.name)
filter = SongFormatAttr.get(format,
SongFormatAttr.filter)
filepathEdit = self.formatWidgets[format][u'filepathEdit']
if select_mode == SongFormatSelect.SingleFile:
self.getFileName(WizardStrings.OpenTypeFile % name, filepathEdit,
@ -321,30 +316,20 @@ class SongImportForm(OpenLPWizard):
def onAddButtonClicked(self):
format = self.activeFormat
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
name = SongFormatAttribute.get(format,
SongFormatAttribute.name)
filter = SongFormatAttribute.get(format,
SongFormatAttribute.filter)
select_mode, format_name, filter, custom_title = \
SongFormatAttr.get(format, SongFormatAttr.select_mode,
SongFormatAttr.name, SongFormatAttr.filter,
SongFormatAttr.get_files_title)
title = custom_title if custom_title \
else WizardStrings.OpenTypeFile % format_name
if select_mode == SongFormatSelect.MultipleFiles:
self.getFiles(WizardStrings.OpenTypeFile % name,
self.formatWidgets[format][u'FileListWidget'], filter)
self.getFiles(title, self.formatWidgets[format][u'FileListWidget'],
filter)
def onRemoveButtonClicked(self):
self.removeSelectedItems(
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):
"""
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 = 0
self.formatComboBox.setCurrentIndex(last_import_type)
for format in SongFormat.get_formats():
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
for format in SongFormat.get_format_list():
select_mode = SongFormatAttr.get(format,
SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.MultipleFiles:
self.formatWidgets[format][u'fileListWidget'].clear()
else:
@ -385,8 +370,8 @@ class SongImportForm(OpenLPWizard):
the actual importing.
"""
source_format = self.activeFormat
select_mode = SongFormatAttribute.get(source_format,
SongFormatAttribute.select_mode)
select_mode = SongFormatAttr.get(source_format,
SongFormatAttr.select_mode)
if select_mode == SongFormatSelect.SingleFile:
importer = self.plugin.importSongs(source_format,
filename=unicode(
@ -422,19 +407,19 @@ class SongImportForm(OpenLPWizard):
def addFileSelectItem(self):
format = self.activeFormat
prefix = SongFormatAttribute.get(format, SongFormatAttribute.prefix)
obj_prefix = SongFormatAttribute.get(format,
SongFormatAttribute.obj_prefix)
can_disable = SongFormatAttribute.get(format,
SongFormatAttribute.can_disable)
select_mode = SongFormatAttribute.get(format,
SongFormatAttribute.select_mode)
prefix = SongFormatAttr.get(format, SongFormatAttr.prefix)
obj_prefix = SongFormatAttr.get(format,
SongFormatAttr.obj_prefix)
can_disable = SongFormatAttr.get(format,
SongFormatAttr.can_disable)
select_mode = SongFormatAttr.get(format,
SongFormatAttr.select_mode)
if not obj_prefix:
obj_prefix = prefix
page = QtGui.QWidget()
page.setObjectName(obj_prefix + u'Page')
if can_disable:
importWidget = self.disablableWidget(page, prefix, obj_prefix)
importWidget = self.disablableWidget(page, obj_prefix)
else:
importWidget = page
importLayout = QtGui.QVBoxLayout(importWidget)
@ -456,6 +441,10 @@ class SongImportForm(OpenLPWizard):
filepathLayout.addWidget(browseButton)
importLayout.addLayout(filepathLayout)
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:
fileListWidget = QtGui.QListWidget(importWidget)
fileListWidget.setSelectionMode(
@ -474,23 +463,16 @@ class SongImportForm(OpenLPWizard):
removeButton.setObjectName(obj_prefix + u'RemoveButton')
buttonLayout.addWidget(removeButton)
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
#setattr(self, prefix + u'ButtonLayout', buttonLayout)
self.formatWidgets[format][u'buttonLayout'] = buttonLayout
self.formatWidgets[format][u'addButton'] = addButton
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'')
def disablableWidget(self, page, prefix, obj_prefix):
def disablableWidget(self, page, obj_prefix):
format = self.activeFormat
self.disablableFormats.append(format)
layout = QtGui.QVBoxLayout(page)
@ -512,9 +494,9 @@ class SongImportForm(OpenLPWizard):
importWidget = QtGui.QWidget(page)
importWidget.setObjectName(obj_prefix + u'ImportWidget')
layout.addWidget(importWidget)
#setattr(self, prefix + u'Layout', layout)
self.formatWidgets[format][u'layout'] = layout
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'importWidget'] = importWidget
return importWidget

View File

@ -66,9 +66,8 @@ except ImportError:
class SongFormat(object):
"""
This is a special enumeration class that holds the various types of songs,
plus a few helper functions to facilitate generic handling of song types
for importing.
This is a special enumeration class that holds the various types of song
importers and some helper functions to facilitate access.
"""
Unknown = -1
OpenLyrics = 0
@ -89,7 +88,7 @@ class SongFormat(object):
#CSV = 15
@staticmethod
def get_formats():
def get_format_list():
"""
Return a list of the supported song formats.
"""
@ -111,55 +110,86 @@ class SongFormat(object):
SongFormat.WordsOfWorship
]
class SongFormatAttribute(object):
# Required attributes:
class SongFormatAttr(object):
"""
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
name = 1
prefix = 2
# Optional attributes:
# Optional attributes
obj_prefix = 10
can_disable = 11
availability = 12
select_mode = 13
# Required widget Text:
filter = 14
# Optional/custom text values
combo_box_text = 20
# Optional widget Text
disabled_label_text = 30
filter = 31
disabled_label_text = 21
get_files_title = 22
invalid_source_msg = 23
# Set attribute defaults (if not specified here, default is None)
# Set optional attribute defaults
_defaults = {
obj_prefix: None,
can_disable: False,
availability: True,
select_mode: SongFormatSelect.MultipleFiles,
filter: u'',
combo_box_text: None,
disabled_label_text: u'',
filter: u''
get_files_title: None,
invalid_source_msg: None
}
# Set attribute values
_attributes = {
SongFormat.OpenLyrics: {
class_: OpenLyricsImport,
name: WizardStrings.OL,
name: u'OpenLyrics',
prefix: u'openLyrics',
obj_prefix: u'OpenLyrics',
can_disable: True,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'OpenLyrics Files'),
combo_box_text: translate('SongsPlugin.ImportWizardForm',
'OpenLyrics or OpenLP 2.0 Exported Song'),
disabled_label_text: translate('SongsPlugin.ImportWizardForm',
'The OpenLyrics importer has not yet been developed, but as '
'you can see, we are still intending to do so. Hopefully it '
'will be in the next release.'),
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'OpenLyrics Files')
'will be in the next release.')
},
SongFormat.OpenLP2: {
class_: OpenLPSongImport,
name: UiStrings().OLPV2,
prefix: u'openLP2',
select_mode: SongFormatSelect.SingleFile,
combo_box_text: UiStrings().OLPV2,
filter: u'%s (*.sqlite)' % (translate(
'SongsPlugin.ImportWizardForm', 'OpenLP 2.0 Databases'))
},
@ -169,61 +199,59 @@ class SongFormatAttribute(object):
prefix: u'openLP1',
can_disable: True,
select_mode: SongFormatSelect.SingleFile,
combo_box_text: UiStrings().OLPV1,
disabled_label_text: WizardStrings.NoSqlite,
filter: u'%s (*.olp)' % translate('SongsPlugin.ImportWizardForm',
'openlp.org v1.x Databases')
'openlp.org v1.x Databases'),
disabled_label_text: WizardStrings.NoSqlite
},
SongFormat.Generic: {
class_: OooImport,
# name: ,
name: translate('SongsPlugin.ImportWizardForm',
'Generic Document/Presentation'),
prefix: u'generic',
can_disable: True,
combo_box_text: translate('SongsPlugin.ImportWizardForm',
'Generic Document/Presentation'),
disabled_label_text: translate('SongsPlugin.ImportWizardForm',
'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: {
class_: CCLIFileImport,
name: WizardStrings.CCLI,
name: u'CCLI/SongSelect',
prefix: u'ccli',
combo_box_text: WizardStrings.CCLI,
filter: u'%s (*.usr *.txt)' % translate(
'SongsPlugin.ImportWizardForm', 'CCLI SongSelect Files')
},
SongFormat.DreamBeam: {
class_: DreamBeamImport,
name: WizardStrings.DB,
name: u'DreamBeam',
prefix: u'dreamBeam',
combo_box_text: WizardStrings.DB,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'DreamBeam Song Files')
},
SongFormat.EasySlides: {
class_: EasySlidesImport,
name: WizardStrings.ES,
name: u'EasySlides',
prefix: u'easySlides',
select_mode: SongFormatSelect.SingleFile,
combo_box_text: WizardStrings.ES,
filter: u'%s (*.xml)' % translate('SongsPlugin.ImportWizardForm',
'EasySlides XML File')
},
SongFormat.EasyWorship: {
class_: EasyWorshipSongImport,
name: WizardStrings.EW,
name: u'EasyWorship',
prefix: u'ew',
select_mode: SongFormatSelect.SingleFile,
combo_box_text: WizardStrings.EW,
filter: u'%s (*.db)' % translate('SongsPlugin.ImportWizardForm',
'EasyWorship Song Database')
},
SongFormat.FoilPresenter: {
class_: FoilPresenterImport,
name: WizardStrings.FP,
name: u'Foilpresenter',
prefix: u'foilPresenter',
combo_box_text: WizardStrings.FP,
filter: u'%s (*.foil)' % translate('SongsPlugin.ImportWizardForm',
'Foilpresenter Song Files')
},
@ -231,49 +259,43 @@ class SongFormatAttribute(object):
class_: OpenSongImport,
name: WizardStrings.OS,
prefix: u'openSong',
obj_prefix: u'OpenSong',
combo_box_text: WizardStrings.OS
obj_prefix: u'OpenSong'
},
SongFormat.PowerSong: {
class_: PowerSongImport,
name: WizardStrings.PS,
name: u'PowerSong 1.0',
prefix: u'powerSong',
select_mode: SongFormatSelect.SingleFolder,
combo_box_text: WizardStrings.PS
select_mode: SongFormatSelect.SingleFolder
},
SongFormat.SongBeamer: {
class_: SongBeamerImport,
name: WizardStrings.SB,
name: u'SongBeamer',
prefix: u'songBeamer',
combo_box_text: WizardStrings.SB,
filter: u'%s (*.sng)' % translate('SongsPlugin.ImportWizardForm',
'SongBeamer Files')
},
SongFormat.SongShowPlus: {
class_: SongShowPlusImport,
name: WizardStrings.SSP,
name: u'SongShow Plus',
prefix: u'songShowPlus',
combo_box_text: WizardStrings.SSP,
filter: u'%s (*.sbsong)' % translate('SongsPlugin.ImportWizardForm',
'SongShow Plus Song Files')
},
SongFormat.SongsOfFellowship: {
class_: SofImport,
name: WizardStrings.SoF,
name: u'Songs of Fellowship',
prefix: u'songsOfFellowship',
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',
'The Songs of Fellowship importer has been disabled because '
'OpenLP cannot access OpenOffice or LibreOffice.'),
filter: u'%s (*.rtf)' % translate('SongsPlugin.ImportWizardForm',
'Songs Of Fellowship Song Files')
'OpenLP cannot access OpenOffice or LibreOffice.')
},
SongFormat.WordsOfWorship: {
class_: WowImport,
name: WizardStrings.WoW,
name: u'Words of Worship',
prefix: u'wordsOfWorship',
combo_box_text: WizardStrings.WoW,
filter: u'%s (*.wsg *.wow-song)' % translate(
'SongsPlugin.ImportWizardForm', 'Words Of Worship Song Files')
# },
@ -282,31 +304,59 @@ class SongFormatAttribute(object):
# name: WizardStrings.CSV,
# prefix: u'csv',
# obj_prefix: u'CSV',
# select_mode: SongFormatSelect.SingleFile,
# combo_box_text: WizardStrings.CSV
# select_mode: SongFormatSelect.SingleFile
}
}
@staticmethod
def get(format, attribute):
default = _defaults.get(attribute)
return SongFormat._attributes[format].get(attribute, default)
def get(format, *attributes):
"""
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
def set(format, attribute, value):
"""
Set specified song format attribute to supplied value.
"""
SongFormat._attributes[format][attribute] = value
class SongFormatSelect(object):
"""
This is a special enumeration class listing available file selection modes.
"""
SingleFile = 0
MultipleFiles = 1
SingleFolder = 2
SongFormatAttribute.set(
SongFormat.OpenLP1, SongFormatAttribute.availability, HAS_OPENLP1)
SongFormatAttribute.set(
SongFormat.SongsOfFellowship, SongFormatAttribute.availability, HAS_SOF)
SongFormatAttribute.set(
SongFormat.Generic, SongFormatAttribute.availability, HAS_OOO)
SongFormatAttr.set(
SongFormat.OpenLP1, SongFormatAttr.availability, HAS_OPENLP1)
SongFormatAttr.set(
SongFormat.SongsOfFellowship, SongFormatAttr.availability, HAS_SOF)
SongFormatAttr.set(
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
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.importer import SongFormat, SongFormatAttr
log = logging.getLogger(__name__)
@ -91,6 +91,8 @@ class PowerSongImport(SongImport):
"""
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 os.path.isdir(self.importSource):
dir = self.importSource
@ -104,7 +106,7 @@ class PowerSongImport(SongImport):
self.logError(unicode(translate('SongsPlugin.PowerSongImport',
'No songs to import.')),
unicode(translate('SongsPlugin.PowerSongImport',
'No %s files found.' % WizardStrings.PS)))
'No %s files found.' % PS_string)))
return
self.importWizard.progressBar.setMaximum(len(self.importSource))
for file in self.importSource:
@ -124,7 +126,7 @@ class PowerSongImport(SongImport):
self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Unexpected byte value.'
% WizardStrings.PS)))
% PS_string)))
break
else:
if label == u'TITLE':
@ -142,15 +144,14 @@ class PowerSongImport(SongImport):
if not self.title:
self.logError(os.path.basename(file), unicode(
translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Missing "TITLE" header.'
% WizardStrings.PS)))
'Invalid %s file. Missing "TITLE" header.' % PS_string)))
continue
# Check that file had COPYRIGHTLINE label
if not found_copyright:
self.logError(self.title, unicode(
translate('SongsPlugin.PowerSongImport',
'Invalid %s file. Missing "COPYRIGHTLINE" '
'header.' % WizardStrings.PS)))
'header.' % PS_string)))
continue
# Check that file had at least one verse
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, \
SongsTab
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
log = logging.getLogger(__name__)
@ -194,7 +194,7 @@ class SongsPlugin(Plugin):
self.manager.save_object(song)
def importSongs(self, format, **kwargs):
class_ = SongFormatAttribute.get(format, SongFormatAttribute.class_)
class_ = SongFormatAttr.get(format, SongFormatAttr.class_)
importer = class_(self.manager, **kwargs)
importer.register(self.mediaItem.importWizard)
return importer