← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~phill-ridout/openlp/wow_import into lp:openlp

 

phill has proposed merging lp:~phill-ridout/openlp/wow_import into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)

-- 
https://code.launchpad.net/~phill-ridout/openlp/wow_import/+merge/32051
Your team OpenLP Core is requested to review the proposed merge of lp:~phill-ridout/openlp/wow_import into lp:openlp.
=== modified file 'openlp/plugins/songs/lib/__init__.py'
--- openlp/plugins/songs/lib/__init__.py	2010-07-28 13:32:12 +0000
+++ openlp/plugins/songs/lib/__init__.py	2010-08-08 15:23:47 +0000
@@ -147,5 +147,6 @@
 try:
     from sofimport import SofImport
     from oooimport import OooImport
+    from wowimport import WowImport
 except ImportError:
     pass

=== added file 'openlp/plugins/songs/lib/test/wowtest.zip'
Binary files openlp/plugins/songs/lib/test/wowtest.zip	1970-01-01 00:00:00 +0000 and openlp/plugins/songs/lib/test/wowtest.zip	2010-08-08 15:23:47 +0000 differ
=== added file 'openlp/plugins/songs/lib/wowimport.py'
--- openlp/plugins/songs/lib/wowimport.py	1970-01-01 00:00:00 +0000
+++ openlp/plugins/songs/lib/wowimport.py	2010-08-08 15:23:47 +0000
@@ -0,0 +1,173 @@
+# -*- 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                          #
+###############################################################################
+
+import os
+import logging
+from zipfile import ZipFile
+
+from openlp.plugins.songs.lib.songimport import SongImport
+
+log = logging.getLogger(__name__)
+
+class WowImportError(Exception):
+    pass
+
+class WowImport(object):
+    """
+    Block means verse/chorus/bridge
+
+    Words Of Worship Song File Format
+    
+    The song title is the file name minus the extension
+    
+    Header
+    Chars counted from one, i.e. the first char is char 1
+    
+    Chars up to the 57 char can change, but no correlation has been found!
+    
+    The 57th Char Specifies how many blocks there are.
+    First block starts with char 83 after "CSongDoc::CBlock".
+    
+    Blocks
+    Each block has a start, some Lyric Lines, and an end
+    Each block starts with 4 chars the first char specifies how many lines are 
+    in that block, the next three chars are nul.
+    
+    Lines
+    Each line starts with a char which specifies how long that line is chars.
+    
+    This is then followed by the lyrics for that line.
+    
+    The Line ends with a nul char
+    
+    Each block ends with 4 chars, the first char specifies what type of block 
+    it is:
+        NUL (\x00) - Verse
+        SOH (\x01) - Chorus
+        STX (\x02) - Bridge
+    the next three chars are nul.
+    
+    Blocks are seperated by two chars, the first, SOH (\x01), and the second, 
+    € (\x80)
+    
+    Fotter / Copyright / Author
+    The fotter follows on straight after the last block, the first char 
+    specifies the length of the Author text, followed by the author text, if 
+    this char is null, there is no author text. After the author text a char 
+    specifies the length of the copyright text, followed by the copyright text. 
+    After this there are 4 nul chars, which ends the file.
+    """
+    
+    def __init__(self, songmanager):
+        """
+        Initialise the class. Requires a songmanager class which is passed to 
+        SongImport for writing song to disk
+        """
+        self.songmanager = songmanager
+        self.song = None
+
+    def do_import(self, filename, commit=True):
+        """
+        Import either a single Words of Worship file, or a zipfile containing 
+        multiple opensong files If the commit parameter is set False, the 
+        import will not be committed to the database (useful for test scripts)
+        """
+        ext = os.path.splitext(filename)[1]
+        if ext.lower() == ".zip":
+            log.info('Zipfile found %s', filename)
+            z = ZipFile(filename, u'r')
+            for song in z.infolist():
+                parts = os.path.split(song.filename)
+                if parts[-1] == u'':
+                    #No final part => directory
+                    continue
+                songfile = z.open(song, 'rb')
+                self.do_import_file(songfile,  song)
+                if commit:
+                    self.finish()
+        else:
+            log.info('Direct import %s', filename)
+            self.do_import_file(filename)
+            if commit:
+                self.finish()
+
+    def do_import_file(self,  file_name):
+            """
+            Process the Words of Worship file.
+            """
+            # TODO: check that it is a valid words of worship file (could check 
+            # header for WoW File Song Word)
+            self.song_import = SongImport(self.songmanager)
+            self.author = u''
+            self.copyright= u''
+            BLOCK_TYPES = (u'V',  u'C',  u'B')
+            # Get the song title
+            self.real_file_name = os.path.split(file_name)[1]
+            self.title = self.real_file_name.rpartition(u'.')[0]
+            with open(file_name, 'rb') as f:
+                # Seek to byte which stores number of blocks in the song
+                f.seek(56) 
+                self.no_of_blocks = ord(f.read(1))
+                # Seek to the beging of the first block
+                f.seek(82) 
+                for block in range(self.no_of_blocks):
+                    self.lines_to_read = ord(f.read(1))
+                    # Skip 3 nulls to the beginnig of the 1st line
+                    f.seek(3, os.SEEK_CUR) 
+                    self.block_text = u''
+                    for line in range(self.lines_to_read):
+                        self.length_of_line = ord(f.read(1))
+                        self.line_text = unicode(f.read(self.length_of_line),
+                            u'utf-8', u'replace')
+                        f.seek(1, os.SEEK_CUR)
+                        if self.block_text != u'':
+                            self.block_text += u'\n'
+                        self.block_text += self.line_text
+                    self.block_type = BLOCK_TYPES[ord(f.read(1))]
+                    # Skip 3 nulls at the end of the block
+                    f.seek(3, os.SEEK_CUR)
+                    # Blocks are seperated by 2 bytes, skip them, but not if 
+                    # this is the last block!
+                    if (block +1) < self.no_of_blocks:
+                        f.seek(2, os.SEEK_CUR)
+                    self.song_import.add_verse(self.block_text, self.block_type)
+                # Now to extact the author
+                self.author_length = ord(f.read(1))
+                if self.author_length != 0:
+                    self.author = unicode(f.read(self.author_length),
+                        u'utf-8', u'replace')
+                # Finally the copyright
+                self.copyright_length = ord(f.read(1))
+                if self.copyright_length != 0:
+                    self.copyright = unicode(f.read(self.copyright_length),
+                        u'utf-8', u'replace')
+            self.song_import.title = self.title
+            self.song_import.parse_author(self.author)
+            self.song_import.add_copyright(self.copyright)
+
+    def finish(self):
+        """ Separate function, allows test suite to not pollute database"""
+        self.song_import.finish()

=== modified file 'openlp/plugins/songs/songsplugin.py'
--- openlp/plugins/songs/songsplugin.py	2010-07-31 02:06:44 +0000
+++ openlp/plugins/songs/songsplugin.py	2010-08-08 15:23:47 +0000
@@ -39,7 +39,7 @@
 except ImportError:
     OOo_available = False
 
-from openlp.plugins.songs.lib import OpenSongImport
+from openlp.plugins.songs.lib import OpenSongImport,  WowImport
 
 log = logging.getLogger(__name__)
 
@@ -169,6 +169,24 @@
         import_menu.addAction(self.ImportOpenLPSongItem)
         QtCore.QObject.connect(self.ImportOpenLPSongItem,
             QtCore.SIGNAL(u'triggered()'), self.onImportOpenLPSongItemClick)
+        # Words of Worship import menu item - will be removed and the
+        # functionality will be contained within the import wizard
+        self.ImportWowItem = QtGui.QAction(import_menu)
+        self.ImportWowItem.setObjectName(u'ImportWowItem')
+        self.ImportWowItem.setText(
+            translate('SongsPlugin',
+                'Words of Worship (temp menu item)'))
+        self.ImportWowItem.setToolTip(
+            translate('SongsPlugin',
+                'Import songs from Words of Worship files' +
+                '(either raw text or ZIPfiles)'))
+        self.ImportWowItem.setStatusTip(
+            translate('SongsPlugin',
+                'Import songs from Words of Worship files' +
+                '(either raw text or ZIPfiles)'))
+        import_menu.addAction(self.ImportWowItem)
+        QtCore.QObject.connect(self.ImportWowItem,
+                QtCore.SIGNAL(u'triggered()'), self.onImportWowItemClick)
 
     def addExportMenuItem(self, export_menu):
         """
@@ -240,6 +258,23 @@
                 'Error importing OpenLP v2 database(s)'))
         Receiver.send_message(u'songs_load_list')
 
+    def onImportWowItemClick(self):
+        filenames = QtGui.QFileDialog.getOpenFileNames(
+            None, translate('SongsPlugin',
+                'Open Words of Worship file'),
+            u'', u'Words of Worship file (*.wsg *.wow-song)')
+        try:
+            for filename in filenames:
+                wowimport = WowImport(self.manager)
+                wowimport.do_import(unicode(filename))
+        except:
+            log.exception('Could not import WoW file')
+            QtGui.QMessageBox.critical(None,
+                translate('SongsPlugin', 'Import Error'),
+                translate('SongsPlugin', 'Error importing Words of '
+                    'Worship file.'))
+        Receiver.send_message(u'songs_load_list')
+
     def onImportOooItemClick(self):
         filenames = QtGui.QFileDialog.getOpenFileNames(
             None, translate('SongsPlugin', 'Open documents or presentations'),


Follow ups