openlp/openlp/plugins/songs/lib/importers/dreambeam.py

160 lines
7.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
"""
2014-07-04 09:35:10 +00:00
The :mod:`dreambeam` module provides the functionality for importing DreamBeam songs into the OpenLP database.
"""
import logging
2012-03-27 20:09:01 +00:00
from lxml import etree, objectify
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
2014-07-04 09:31:06 +00:00
from openlp.plugins.songs.lib.importers.songimport import SongImport
from openlp.plugins.songs.lib.ui import SongStrings
2018-10-02 04:39:42 +00:00
log = logging.getLogger(__name__)
2014-03-06 20:40:08 +00:00
class DreamBeamImport(SongImport):
"""
The :class:`DreamBeamImport` class provides the ability to import song files from
DreamBeam.
2013-01-18 23:31:02 +00:00
2012-03-25 17:25:08 +00:00
An example of DreamBeam xml mark-up::
2013-01-18 23:31:02 +00:00
2012-03-25 17:25:08 +00:00
<?xml version="1.0"?>
2013-01-18 23:31:02 +00:00
<DreamSong xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2012-03-25 17:25:08 +00:00
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<WordWrap>false</WordWrap>
<Version>0.80</Version>
<Title>Amazing Grace</Title>
<Author>John Newton</Author>
<Collection />
<Number />
<Notes />
<KeyRangeLow>F</KeyRangeLow>
<KeyRangeHigh>G</KeyRangeHigh>
<MinorKey>false</MinorKey>
<DualLanguage>false</DualLanguage>
<SongLyrics>
<LyricsItem Type="Verse" Number="1">Amazing Grace, how sweet the sound,
That saved a wretch like me.
I once was lost but now am found,
Was blind, but now, I see.</LyricsItem>
<LyricsItem Type="Verse" Number="2">Twas Grace that taught my heart to fear.
And Grace, my fears relieved.
How precious did that Grace appear
the hour I first believed.</LyricsItem>
</SongLyrics>
<Sequence>
<LyricsSequenceItem Type="Verse" Number="1" />
<LyricsSequenceItem Type="Verse" Number="2" />
</Sequence>
<ShowRectangles>false</ShowRectangles>
</DreamSong>
Valid extensions for a DreamBeam song file are:
2018-07-04 20:42:55 +00:00
* .xml
"""
2013-01-18 23:31:02 +00:00
2014-03-06 20:40:08 +00:00
def do_import(self):
"""
2017-09-30 20:16:30 +00:00
Receive a single file_path or a list of files to import.
"""
2013-03-07 08:05:43 +00:00
if isinstance(self.import_source, list):
self.import_wizard.progress_bar.setMaximum(len(self.import_source))
2017-09-30 20:16:30 +00:00
for file_path in self.import_source:
2013-02-07 11:33:47 +00:00
if self.stop_import_flag:
return
2014-03-05 18:58:22 +00:00
self.set_defaults()
author_copyright = ''
parser = etree.XMLParser(remove_blank_text=True, recover=True)
try:
2017-09-30 20:16:30 +00:00
with file_path.open('r') as xml_file:
parsed_file = etree.parse(xml_file, parser)
except etree.XMLSyntaxError:
log.exception('XML syntax error in file {name}'.format(name=file_path))
2017-09-30 20:16:30 +00:00
self.log_error(file_path, SongStrings.XMLSyntaxError)
continue
except UnicodeDecodeError:
log.exception('Unreadable characters in {name}'.format(name=file_path))
self.log_error(file_path, SongStrings.XMLSyntaxError)
continue
file_str = etree.tostring(parsed_file)
if not file_str:
log.exception('Could not find XML in file {name}'.format(name=file_path))
self.log_error(file_path, SongStrings.XMLSyntaxError)
continue
xml = file_str.decode()
song_xml = objectify.fromstring(xml)
2013-08-31 18:17:38 +00:00
if song_xml.tag != 'DreamSong':
2014-03-06 20:40:08 +00:00
self.log_error(
2017-09-30 20:16:30 +00:00
file_path,
2017-09-30 22:45:24 +00:00
translate('SongsPlugin.DreamBeamImport',
'Invalid DreamBeam song file. Missing DreamSong tag.'))
continue
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Version'):
self.version = float(song_xml.Version.text)
else:
self.version = 0
# Version numbers found in DreamBeam Source /FileTypes/Song.cs
if self.version >= 0.5:
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Title'):
self.title = str(song_xml.Title.text)
if hasattr(song_xml, 'Author'):
author_copyright = song_xml.Author.text
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'SongLyrics'):
for lyrics_item in song_xml.SongLyrics.iterchildren():
2014-03-21 21:38:08 +00:00
verse_type = lyrics_item.get('Type')
2013-08-31 18:17:38 +00:00
verse_number = lyrics_item.get('Number')
verse_text = str(lyrics_item.text)
self.add_verse(verse_text,
'{verse}{number}'.format(verse=verse_type[:1], number=verse_number))
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Collection'):
2014-03-05 18:58:22 +00:00
self.song_book_name = str(song_xml.Collection.text)
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Number'):
2014-03-05 18:58:22 +00:00
self.song_number = str(song_xml.Number.text)
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Sequence'):
2014-03-06 20:40:08 +00:00
for lyrics_sequence_item in (song_xml.Sequence.iterchildren()):
item = lyrics_sequence_item.get('Type')[:1]
number = lyrics_sequence_item.get('Number')
self.verse_order_list.append("{item}{number}".format(item=item, number=number))
2013-08-31 18:17:38 +00:00
if hasattr(song_xml, 'Notes'):
self.comments = str(song_xml.Notes.text)
else:
2013-08-31 18:17:38 +00:00
if hasattr(song_xml.Text0, 'Text'):
self.title = str(song_xml.Text0.Text.text)
if hasattr(song_xml.Text1, 'Text'):
self.lyrics = str(song_xml.Text1.Text.text)
for verse in self.lyrics.split('\n\n\n'):
2014-03-05 18:58:22 +00:00
self.add_verse(verse)
2013-08-31 18:17:38 +00:00
if hasattr(song_xml.Text2, 'Text'):
author_copyright = song_xml.Text2.Text.text
if author_copyright:
2013-08-31 18:17:38 +00:00
author_copyright = str(author_copyright)
if author_copyright.find(SongStrings.CopyrightSymbol) >= 0:
2014-03-05 18:58:22 +00:00
self.add_copyright(author_copyright)
else:
2013-03-07 08:05:43 +00:00
self.parse_author(author_copyright)
if not self.finish():
2017-09-30 20:16:30 +00:00
self.log_error(file_path)