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

151 lines
6.2 KiB
Python
Raw Normal View History

2012-06-25 22:26:46 +00:00
# -*- 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/>. #
##########################################################################
2012-06-25 22:26:46 +00:00
"""
2014-07-04 09:35:10 +00:00
The :mod:`songpro` module provides the functionality for importing SongPro
2012-06-25 22:26:46 +00:00
songs into the OpenLP database.
"""
import logging
2012-06-25 22:26:46 +00:00
import re
from pathlib import Path
2012-06-25 22:26:46 +00:00
from openlp.core.common.i18n import translate
2012-07-03 21:14:12 +00:00
from openlp.plugins.songs.lib import strip_rtf
2014-07-04 09:31:06 +00:00
from openlp.plugins.songs.lib.importers.songimport import SongImport
2012-06-25 22:26:46 +00:00
log = logging.getLogger(__name__)
2013-10-13 20:36:42 +00:00
2012-06-25 22:26:46 +00:00
class SongProImport(SongImport):
"""
The :class:`SongProImport` class provides the ability to import song files
from SongPro export files.
**SongPro Song File Format:**
2015-09-08 19:13:59 +00:00
| SongPro has the option to export under its File menu
| This produces files containing single or multiple songs
| The file is text with lines tagged with # followed by an identifier.
| This is documented here: http://creationsoftware.com/ImportIdentifiers.php
| An example here: http://creationsoftware.com/ExampleImportingManySongs.txt
|
| #A - next line is the Song Author
| #B - the lines following until next tagged line are the "Bridge" words
| (can be in rtf or plain text) which we map as B1
| #C - the lines following until next tagged line are the chorus words
| (can be in rtf or plain text)
| which we map as C1
| #D - the lines following until next tagged line are the "Ending" words
| (can be in rtf or plain text) which we map as E1
| #E - this song ends here, so we process the song -
| and start again at the next line
| #G - next line is the Group
| #M - next line is the Song Number
| #N - next line are Notes
| #R - next line is the SongCopyright
| #O - next line is the Verse Sequence
| #T - next line is the Song Title
| #1 - #7 the lines following until next tagged line are the verse x words
| (can be in rtf or plain text)
2012-06-25 22:26:46 +00:00
"""
def __init__(self, manager, **kwargs):
"""
Initialise the SongPro importer.
"""
super(SongProImport, self).__init__(manager, **kwargs)
2012-06-25 22:26:46 +00:00
2014-03-06 20:40:08 +00:00
def do_import(self):
2012-06-25 22:26:46 +00:00
"""
Receive a single file or a list of files to import.
"""
self.encoding = None
2017-09-30 20:16:30 +00:00
self.import_source = Path(self.import_source)
with self.import_source.open('rt', errors='ignore') as songs_file:
2013-03-07 08:05:43 +00:00
self.import_wizard.progress_bar.setMaximum(0)
2013-08-31 18:17:38 +00:00
tag = ''
text = ''
2012-06-25 22:26:46 +00:00
for file_line in songs_file:
2013-02-07 11:33:47 +00:00
if self.stop_import_flag:
2012-06-25 22:26:46 +00:00
break
file_text = file_line.rstrip()
2013-08-31 18:17:38 +00:00
if file_text and file_text[0] == '#':
try:
self.process_section(tag, text.rstrip())
except ValueError:
log.exception('Missing data in {name}'.format(name=self.import_source))
self.log_error(self.import_source, translate('SongsPlugin.SongProImport',
'File is not a valid SongPro file.'))
return
2012-06-25 22:26:46 +00:00
tag = file_text[1:]
2013-08-31 18:17:38 +00:00
text = ''
2012-06-25 22:26:46 +00:00
else:
text += file_line
self.finish()
2012-06-25 22:26:46 +00:00
2014-03-08 19:58:58 +00:00
def process_section(self, tag, text):
2012-06-25 22:26:46 +00:00
"""
Process a section of the song, i.e. title, verse etc.
"""
2013-08-31 18:17:38 +00:00
if tag == 'T':
2014-03-05 18:58:22 +00:00
self.set_defaults()
2012-06-25 22:26:46 +00:00
if text:
self.title = text
return
2013-08-31 18:17:38 +00:00
elif tag == 'E':
2012-06-25 22:26:46 +00:00
self.finish()
return
2013-08-31 18:17:38 +00:00
if 'rtf1' in text:
2013-02-04 17:47:02 +00:00
result = strip_rtf(text, self.encoding)
if result is None:
return
text, self.encoding = result
text = text.rstrip()
2012-06-25 22:26:46 +00:00
if not text:
return
2013-08-31 18:17:38 +00:00
if tag == 'A':
2013-03-07 08:05:43 +00:00
self.parse_author(text)
2013-08-31 18:17:38 +00:00
elif tag in ['B', 'C']:
2014-03-05 18:58:22 +00:00
self.add_verse(text, tag)
2013-08-31 18:17:38 +00:00
elif tag == 'D':
2014-03-05 18:58:22 +00:00
self.add_verse(text, 'E')
2013-08-31 18:17:38 +00:00
elif tag == 'G':
2012-06-25 22:26:46 +00:00
self.topics.append(text)
2013-08-31 18:17:38 +00:00
elif tag == 'M':
2012-06-25 22:26:46 +00:00
matches = re.findall(r'\d+', text)
if matches:
2014-03-05 18:58:22 +00:00
self.song_number = matches[-1]
self.song_book_name = text[:text.rfind(self.song_number)]
2013-08-31 18:17:38 +00:00
elif tag == 'N':
2012-06-25 22:26:46 +00:00
self.comments = text
2013-08-31 18:17:38 +00:00
elif tag == 'O':
2012-06-25 22:26:46 +00:00
for char in text:
2013-08-31 18:17:38 +00:00
if char == 'C':
2014-03-05 18:58:22 +00:00
self.verse_order_list.append('C1')
2013-08-31 18:17:38 +00:00
elif char == 'B':
2014-03-05 18:58:22 +00:00
self.verse_order_list.append('B1')
2013-08-31 18:17:38 +00:00
elif char == 'D':
2014-03-05 18:58:22 +00:00
self.verse_order_list.append('E1')
2013-08-31 18:17:38 +00:00
elif '1' <= char <= '7':
2014-03-05 18:58:22 +00:00
self.verse_order_list.append('V' + char)
2013-08-31 18:17:38 +00:00
elif tag == 'R':
2014-03-05 18:58:22 +00:00
self.add_copyright(text)
2013-08-31 18:17:38 +00:00
elif '1' <= tag <= '7':
2014-03-05 18:58:22 +00:00
self.add_verse(text, 'V' + tag[1:])