~ubuntu-branches/debian/stretch/openlp/stretch

« back to all changes in this revision

Viewing changes to openlp/plugins/songs/lib/mediashoutimport.py

  • Committer: Package Import Robot
  • Author(s): Raoul Snyman
  • Date: 2014-02-04 20:37:41 UTC
  • mfrom: (1.2.1) (3.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20140204203741-dydzq1h14rak8ksz
Tags: 2.0.4-1
* New upstream release
* Update build-depends to debhelper 8
* Change maintainer e-mail

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
 
3
 
 
4
###############################################################################
 
5
# OpenLP - Open Source Lyrics Projection                                      #
 
6
# --------------------------------------------------------------------------- #
 
7
# Copyright (c) 2008-2014 Raoul Snyman                                        #
 
8
# Portions copyright (c) 2008-2014 Tim Bentley, Gerald Britton, Jonathan      #
 
9
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub,      #
 
10
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer.   #
 
11
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru,          #
 
12
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith,             #
 
13
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock,              #
 
14
# Frode Woldsund, Martin Zibricky                                             #
 
15
# --------------------------------------------------------------------------- #
 
16
# This program is free software; you can redistribute it and/or modify it     #
 
17
# under the terms of the GNU General Public License as published by the Free  #
 
18
# Software Foundation; version 2 of the License.                              #
 
19
#                                                                             #
 
20
# This program is distributed in the hope that it will be useful, but WITHOUT #
 
21
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
 
22
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    #
 
23
# more details.                                                               #
 
24
#                                                                             #
 
25
# You should have received a copy of the GNU General Public License along     #
 
26
# with this program; if not, write to the Free Software Foundation, Inc., 59  #
 
27
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 
28
###############################################################################
 
29
"""
 
30
The :mod:`mediashoutimport` module provides the functionality for importing
 
31
a MediaShout database into the OpenLP database.
 
32
"""
 
33
import re
 
34
import os
 
35
import pyodbc
 
36
 
 
37
from openlp.core.lib import translate
 
38
from openlp.plugins.songs.lib.songimport import SongImport
 
39
 
 
40
VERSE_TAGS = [u'V', u'C', u'B', u'O', u'P', u'I', u'E']
 
41
 
 
42
class MediaShoutImport(SongImport):
 
43
    """
 
44
    The :class:`MediaShoutImport` class provides the ability to import the
 
45
    MediaShout Access Database
 
46
    """
 
47
    def __init__(self, manager, **kwargs):
 
48
        """
 
49
        Initialise the MediaShout importer.
 
50
        """
 
51
        SongImport.__init__(self, manager, **kwargs)
 
52
 
 
53
    def doImport(self):
 
54
        """
 
55
        Receive a single file to import.
 
56
        """
 
57
        try:
 
58
           conn = pyodbc.connect(u'DRIVER={Microsoft Access Driver (*.mdb)};'
 
59
            u'DBQ=%s;PWD=6NOZ4eHK7k' % self.importSource)
 
60
        except:
 
61
            # Unfortunately no specific exception type
 
62
            self.logError(self.importSource,
 
63
                translate('SongsPlugin.MediaShoutImport',
 
64
                    'Unable to open the MediaShout database.'))
 
65
            return
 
66
        cursor = conn.cursor()
 
67
        cursor.execute(u'SELECT Record, Title, Author, Copyright, '
 
68
                       u'SongID, CCLI, Notes FROM Songs ORDER BY Title')
 
69
        songs = cursor.fetchall()
 
70
        self.importWizard.progressBar.setMaximum(len(songs))
 
71
        for song in songs:
 
72
            if self.stopImportFlag:
 
73
                break
 
74
            cursor.execute(u'SELECT Type, Number, Text FROM Verses '
 
75
                u'WHERE Record = %s ORDER BY Type, Number' % song.Record)
 
76
            verses = cursor.fetchall()
 
77
            cursor.execute(u'SELECT Type, Number, POrder FROM PlayOrder '
 
78
                u'WHERE Record = %s ORDER BY POrder' % song.Record)
 
79
            verse_order = cursor.fetchall()
 
80
            cursor.execute(u'SELECT Name FROM Themes INNER JOIN SongThemes '
 
81
                u'ON SongThemes.ThemeId = Themes.ThemeId '
 
82
                u'WHERE SongThemes.Record = %s' % song.Record)
 
83
            topics = cursor.fetchall()
 
84
            cursor.execute(u'SELECT Name FROM Groups INNER JOIN SongGroups '
 
85
                           u'ON SongGroups.GroupId = Groups.GroupId '
 
86
                           u'WHERE SongGroups.Record = %s' % song.Record)
 
87
            topics += cursor.fetchall()
 
88
            self.processSong(song, verses, verse_order, topics)
 
89
 
 
90
    def processSong(self, song, verses, verse_order, topics):
 
91
        """
 
92
        Create the song, i.e. title, verse etc.
 
93
        """
 
94
        self.setDefaults()
 
95
        self.title = song.Title
 
96
        self.parseAuthor(song.Author)
 
97
        self.addCopyright(song.Copyright)
 
98
        self.comments = song.Notes
 
99
        for topic in topics:
 
100
            self.topics.append(topic.Name)
 
101
        if u'-' in song.SongID:
 
102
            self.songBookName, self.songNumber = song.SongID.split(u'-', 1)
 
103
        else:
 
104
            self.songBookName = song.SongID
 
105
        for verse in verses:
 
106
            tag = VERSE_TAGS[verse.Type] + unicode(verse.Number) \
 
107
                if verse.Type < len(VERSE_TAGS) else u'O'
 
108
            self.addVerse(verse.Text, tag)
 
109
        for order in verse_order:
 
110
            if order.Type < len(VERSE_TAGS):
 
111
                self.verseOrderList.append(VERSE_TAGS[order.Type]
 
112
                    + unicode(order.Number))
 
113
        self.finish()