2012-05-24 09:58:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-01-06 17:25:49 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2012-05-24 09:58:05 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2015-01-18 13:39:21 +00:00
|
|
|
# Copyright (c) 2008-2015 OpenLP Developers #
|
2012-05-24 09:58:05 +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; 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
2014-07-04 09:35:10 +00:00
|
|
|
The :mod:`zionworx` module provides the functionality for importing ZionWorx songs into the OpenLP database.
|
2012-05-24 09:58:05 +00:00
|
|
|
"""
|
2012-05-24 12:12:48 +00:00
|
|
|
import csv
|
2012-05-24 09:58:05 +00:00
|
|
|
import logging
|
|
|
|
|
2013-10-13 20:36:42 +00:00
|
|
|
from openlp.core.common import translate
|
2014-07-04 09:31:06 +00:00
|
|
|
from openlp.plugins.songs.lib.importers.songimport import SongImport
|
2012-05-24 09:58:05 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2012-06-26 14:58:08 +00:00
|
|
|
# Used to strip control chars (except 10=LF, 13=CR)
|
2014-03-21 21:38:08 +00:00
|
|
|
CONTROL_CHARS_MAP = dict.fromkeys(list(range(10)) + [11, 12] + list(range(14, 32)) + [127])
|
2012-06-26 14:58:08 +00:00
|
|
|
|
2014-03-08 19:58:58 +00:00
|
|
|
|
2012-05-24 09:58:05 +00:00
|
|
|
class ZionWorxImport(SongImport):
|
|
|
|
"""
|
2012-05-24 12:12:48 +00:00
|
|
|
The :class:`ZionWorxImport` class provides the ability to import songs
|
|
|
|
from ZionWorx, via a dump of the ZionWorx database to a CSV file.
|
|
|
|
|
|
|
|
ZionWorx song database fields:
|
|
|
|
|
2012-06-07 12:30:41 +00:00
|
|
|
* ``SongNum`` Song ID. (Discarded by importer)
|
|
|
|
* ``Title1`` Main Title.
|
|
|
|
* ``Title2`` Alternate Title.
|
|
|
|
* ``Lyrics`` Song verses, separated by blank lines.
|
|
|
|
* ``Writer`` Song author(s).
|
|
|
|
* ``Copyright`` Copyright information
|
|
|
|
* ``Keywords`` (Discarded by importer)
|
|
|
|
* ``DefaultStyle`` (Discarded by importer)
|
2012-05-24 12:12:48 +00:00
|
|
|
|
|
|
|
ZionWorx has no native export function; it uses the proprietary TurboDB
|
|
|
|
database engine. The TurboDB vendor, dataWeb, provides tools which can
|
|
|
|
export TurboDB tables to other formats, such as freeware console tool
|
|
|
|
TurboDB Data Exchange which is available for Windows and Linux. This command
|
|
|
|
exports the ZionWorx songs table to a CSV file:
|
|
|
|
|
|
|
|
``tdbdatax MainTable.dat songstable.csv -fsdf -s, -qd``
|
|
|
|
|
2012-06-07 12:30:41 +00:00
|
|
|
* -f Table format: ``sdf`` denotes text file.
|
|
|
|
* -s Separator character between fields.
|
|
|
|
* -q Quote character surrounding fields. ``d`` denotes double-quote.
|
2012-05-24 12:12:48 +00:00
|
|
|
|
|
|
|
CSV format expected by importer:
|
|
|
|
|
2012-06-07 12:30:41 +00:00
|
|
|
* Field separator character is comma ``,``
|
|
|
|
* Fields surrounded by double-quotes ``"``. This enables fields (such as
|
|
|
|
Lyrics) to include new-lines and commas. Double-quotes within a field
|
|
|
|
are denoted by two double-quotes ``""``
|
|
|
|
* Note: This is the default format of the Python ``csv`` module.
|
2012-05-24 12:12:48 +00:00
|
|
|
|
2012-05-24 09:58:05 +00:00
|
|
|
"""
|
2014-03-06 20:40:08 +00:00
|
|
|
def do_import(self):
|
2012-05-24 09:58:05 +00:00
|
|
|
"""
|
2012-05-24 12:12:48 +00:00
|
|
|
Receive a CSV file (from a ZionWorx database dump) to import.
|
2012-05-24 09:58:05 +00:00
|
|
|
"""
|
2015-10-28 21:50:32 +00:00
|
|
|
# Encoding should always be ISO-8859-1
|
|
|
|
with open(self.import_source, 'rt', encoding='ISO-8859-1') as songs_file:
|
2013-08-31 18:17:38 +00:00
|
|
|
field_names = ['SongNum', 'Title1', 'Title2', 'Lyrics', 'Writer', 'Copyright', 'Keywords',
|
2014-03-08 19:58:58 +00:00
|
|
|
'DefaultStyle']
|
2012-06-26 14:58:08 +00:00
|
|
|
songs_reader = csv.DictReader(songs_file, field_names)
|
2012-05-24 12:12:48 +00:00
|
|
|
try:
|
2012-06-05 09:16:24 +00:00
|
|
|
records = list(songs_reader)
|
2013-08-31 18:17:38 +00:00
|
|
|
except csv.Error as e:
|
2014-03-05 18:58:22 +00:00
|
|
|
self.log_error(translate('SongsPlugin.ZionWorxImport', 'Error reading CSV file.'),
|
2014-03-08 19:58:58 +00:00
|
|
|
translate('SongsPlugin.ZionWorxImport', 'Line %d: %s') % (songs_reader.line_num, e))
|
2012-06-05 14:12:19 +00:00
|
|
|
return
|
|
|
|
num_records = len(records)
|
2013-08-31 18:17:38 +00:00
|
|
|
log.info('%s records found in CSV file' % num_records)
|
2013-03-07 08:05:43 +00:00
|
|
|
self.import_wizard.progress_bar.setMaximum(num_records)
|
2012-06-05 14:12:19 +00:00
|
|
|
for index, record in enumerate(records, 1):
|
2013-02-07 11:33:47 +00:00
|
|
|
if self.stop_import_flag:
|
2012-06-05 14:12:19 +00:00
|
|
|
return
|
2014-03-05 18:58:22 +00:00
|
|
|
self.set_defaults()
|
2012-06-05 09:16:24 +00:00
|
|
|
try:
|
2013-08-31 18:17:38 +00:00
|
|
|
self.title = self._decode(record['Title1'])
|
|
|
|
if record['Title2']:
|
|
|
|
self.alternate_title = self._decode(record['Title2'])
|
|
|
|
self.parse_author(self._decode(record['Writer']))
|
2014-03-05 18:58:22 +00:00
|
|
|
self.add_copyright(self._decode(record['Copyright']))
|
2013-08-31 18:17:38 +00:00
|
|
|
lyrics = self._decode(record['Lyrics'])
|
|
|
|
except UnicodeDecodeError as e:
|
2014-03-05 18:58:22 +00:00
|
|
|
self.log_error(translate('SongsPlugin.ZionWorxImport', 'Record %d' % index),
|
2014-03-08 19:58:58 +00:00
|
|
|
translate('SongsPlugin.ZionWorxImport', 'Decoding error: %s') % e)
|
2012-06-05 14:12:19 +00:00
|
|
|
continue
|
2013-08-31 18:17:38 +00:00
|
|
|
except TypeError as e:
|
2014-03-05 18:58:22 +00:00
|
|
|
self.log_error(translate(
|
2013-08-31 18:17:38 +00:00
|
|
|
'SongsPlugin.ZionWorxImport', 'File not valid ZionWorx CSV format.'), 'TypeError: %s' % e)
|
2012-06-05 14:12:19 +00:00
|
|
|
return
|
2013-08-31 18:17:38 +00:00
|
|
|
verse = ''
|
2012-06-05 14:12:19 +00:00
|
|
|
for line in lyrics.splitlines():
|
|
|
|
if line and not line.isspace():
|
2013-08-31 18:17:38 +00:00
|
|
|
verse += line + '\n'
|
2012-06-05 14:12:19 +00:00
|
|
|
elif verse:
|
2015-10-28 21:50:32 +00:00
|
|
|
self.add_verse(verse, 'v')
|
2013-08-31 18:17:38 +00:00
|
|
|
verse = ''
|
2012-06-05 14:12:19 +00:00
|
|
|
if verse:
|
2015-10-28 21:50:32 +00:00
|
|
|
self.add_verse(verse, 'v')
|
2012-06-05 14:12:19 +00:00
|
|
|
title = self.title
|
|
|
|
if not self.finish():
|
2015-03-09 20:57:39 +00:00
|
|
|
self.log_error(translate('SongsPlugin.ZionWorxImport', 'Record %d') % index +
|
|
|
|
(': "' + title + '"' if title else ''))
|
2012-06-05 09:16:24 +00:00
|
|
|
|
|
|
|
def _decode(self, str):
|
|
|
|
"""
|
2015-10-28 21:50:32 +00:00
|
|
|
Strips all control characters (except new lines).
|
2012-06-05 09:16:24 +00:00
|
|
|
"""
|
2015-10-28 21:50:32 +00:00
|
|
|
# ZionWorx has no option for setting the encoding for its songs, so we assume encoding is always the same.
|
|
|
|
return str.translate(CONTROL_CHARS_MAP)
|