openlp/openlp/plugins/songs/lib/songbeamerimport.py

237 lines
9.0 KiB
Python
Raw Normal View History

2010-09-23 19:02:18 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
# --------------------------------------------------------------------------- #
# 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; version 2 of the License. #
# #
# 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, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`songbeamerimport` module provides the functionality for importing
SongBeamer songs into the OpenLP database.
"""
2010-09-29 20:37:52 +00:00
import logging
2010-09-23 19:02:18 +00:00
import os
2010-09-27 18:17:39 +00:00
import re
2010-09-29 20:37:52 +00:00
import chardet
import codecs
2010-09-23 19:02:18 +00:00
from openlp.plugins.songs.lib.songimport import SongImport
log = logging.getLogger(__name__)
2010-09-27 18:17:39 +00:00
class SongBeamerTypes(object):
MarkTypes = {
u'Refrain': u'C',
u'Chorus': u'C',
u'Vers': u'V',
u'Verse': u'V',
u'Strophe': u'V',
u'Intro': u'I',
u'Coda': u'E',
u'Ending': u'E',
u'Bridge': u'B',
u'Interlude': u'B',
u'Zwischenspiel': u'B',
u'Pre-Chorus': u'P',
u'Pre-Refrain': u'P',
u'Pre-Bridge': u'O',
u'Pre-Coda': u'O',
u'Unbekannt': u'O',
u'Unknown': u'O'
}
2010-09-23 19:02:18 +00:00
class SongBeamerImport(SongImport):
"""
2010-09-29 20:37:52 +00:00
Import Song Beamer files(s)
Song Beamer file format is text based
in the beginning are one or more control tags written
2010-09-23 19:02:18 +00:00
"""
def __init__(self, master_manager, **kwargs):
"""
Initialise the import.
``master_manager``
The song manager for the running OpenLP installation.
"""
SongImport.__init__(self, master_manager)
self.master_manager = master_manager
if kwargs.has_key(u'filename'):
self.import_source = kwargs[u'filename']
if kwargs.has_key(u'filenames'):
self.import_source = kwargs[u'filenames']
log.debug(self.import_source)
def do_import(self):
"""
Recieve a single file, or a list of files to import.
"""
if isinstance(self.import_source, list):
self.import_wizard.importProgressBar.setMaximum(
len(self.import_source))
for file in self.import_source:
# TODO: check that it is a valid SongBeamer file
self.current_verse = u''
self.current_verse_type = u'V'
self.file_name = os.path.split(file)[1]
self.import_wizard.incrementProgressBar(
"Importing %s" % (self.file_name), 0)
2010-09-29 20:37:52 +00:00
if os.path.isfile(file):
detect_file = open(file, u'r')
details = chardet.detect(detect_file.read(2048))
detect_file.close()
infile = codecs.open(file, u'r', details['encoding'])
self.songData = infile.readlines()
else:
return False
2010-09-23 19:02:18 +00:00
for line in self.songData:
2010-09-29 20:37:52 +00:00
line = line.strip()
2010-09-23 19:02:18 +00:00
if line.startswith('#'):
log.debug(u'find tag: %s' % line)
if not self.parse_tags(line):
return False
elif line.startswith('---'):
log.debug(u'find ---')
if len(self.current_verse) > 0:
2010-09-29 20:37:52 +00:00
self.add_verse(self.current_verse,
self.current_verse_type)
2010-09-23 19:02:18 +00:00
self.current_verse = u''
self.current_verse_type = u'V'
self.read_verse = True
self.verse_start = True
elif self.read_verse:
if self.verse_start:
self.check_verse_marks(line)
self.verse_start = False
else:
self.current_verse += u'%s\n' % line
if len(self.current_verse) > 0:
self.add_verse(self.current_verse, self.current_verse_type)
self.finish()
self.import_wizard.incrementProgressBar(
"Importing %s" % (self.file_name))
return True
def parse_tags(self, line):
tag_val = line.split('=')
2010-09-24 04:16:48 +00:00
if len(tag_val[0]) == 0 or \
len(tag_val[1]) == 0:
2010-09-23 19:02:18 +00:00
return True
if tag_val[0] == '#(c)':
self.add_copyright(tag_val[1])
elif tag_val[0] == '#AddCopyrightInfo':
pass
elif tag_val[0] == '#Author':
#TODO split Authors
self.add_author(tag_val[1])
elif tag_val[0] == '#BackgroundImage':
pass
elif tag_val[0] == '#Bible':
pass
elif tag_val[0] == '#Categories':
2010-09-29 20:37:52 +00:00
self.topics = line.split(',')
2010-09-23 19:02:18 +00:00
elif tag_val[0] == '#CCLI':
2010-09-27 18:17:39 +00:00
self.ccli_number = tag_val[1]
2010-09-23 19:02:18 +00:00
elif tag_val[0] == '#Chords':
pass
elif tag_val[0] == '#ChurchSongID':
pass
elif tag_val[0] == '#ColorChords':
pass
elif tag_val[0] == '#Comments':
2010-09-27 18:17:39 +00:00
self.comments = tag_val[1]
2010-09-23 19:02:18 +00:00
elif tag_val[0] == '#Editor':
pass
elif tag_val[0] == '#Font':
pass
elif tag_val[0] == '#FontLang2':
pass
elif tag_val[0] == '#FontSize':
pass
elif tag_val[0] == '#Format':
pass
elif tag_val[0] == '#Format_PreLine':
pass
elif tag_val[0] == '#Format_PrePage':
pass
elif tag_val[0] == '#ID':
pass
elif tag_val[0] == '#Key':
pass
elif tag_val[0] == '#Keywords':
pass
elif tag_val[0] == '#LangCount':
pass
elif tag_val[0] == '#Melody':
#TODO split Authors
self.add_author(tag_val[1])
elif tag_val[0] == '#NatCopyright':
pass
elif tag_val[0] == '#OTitle':
pass
elif tag_val[0] == '#OutlineColor':
pass
elif tag_val[0] == '#OutlinedFont':
pass
elif tag_val[0] == '#QuickFind':
pass
elif tag_val[0] == '#Rights':
2010-09-27 18:17:39 +00:00
song_book_pub = tag_val[1]
2010-09-23 19:02:18 +00:00
elif tag_val[0] == '#Songbook':
2010-09-27 18:17:39 +00:00
book_num = tag_val[1].split(' / ')
self.song_book_name = book_num[0]
if len(book_num) == book_num[1]:
self.song_number = u''
2010-09-23 19:02:18 +00:00
elif tag_val[0] == '#Speed':
pass
elif tag_val[0] == '#TextAlign':
pass
elif tag_val[0] == '#Title':
self.title = u'%s' % tag_val[1]
elif tag_val[0] == '#TitleAlign':
pass
elif tag_val[0] == '#TitleFontSize':
pass
elif tag_val[0] == '#TitleLang2':
pass
elif tag_val[0] == '#TitleLang3':
pass
elif tag_val[0] == '#TitleLang4':
pass
elif tag_val[0] == '#Translation':
pass
elif tag_val[0] == '#Transpose':
pass
elif tag_val[0] == '#TransposeAccidental':
pass
elif tag_val[0] == '#Version':
pass
else:
pass
return True
def check_verse_marks(self, line):
2010-09-27 18:17:39 +00:00
marks = line.split(' ')
if len(marks) <= 2 and \
marks[0] in SongBeamerTypes.MarkTypes:
self.current_verse_type = SongBeamerTypes.MarkTypes[marks[0]]
if len(marks) == 2:
#TODO: may check, because of only digits are allowed
self.current_verse_type += marks[1]