From a530851124840e0f4d4547b5d85d3874b9afd882 Mon Sep 17 00:00:00 2001 From: Andreas Preikschat Date: Mon, 18 Apr 2011 18:46:22 +0200 Subject: [PATCH] added error logger methods --- openlp/plugins/songs/forms/songimportform.py | 14 +++++--- openlp/plugins/songs/lib/cclifileimport.py | 10 +++--- openlp/plugins/songs/lib/easislidesimport.py | 12 +++---- openlp/plugins/songs/lib/ewimport.py | 3 +- .../plugins/songs/lib/foilpresenterimport.py | 3 ++ openlp/plugins/songs/lib/olp1import.py | 3 +- openlp/plugins/songs/lib/olpimport.py | 12 +++---- openlp/plugins/songs/lib/openlyricsimport.py | 3 ++ openlp/plugins/songs/lib/opensongimport.py | 11 +++--- openlp/plugins/songs/lib/songbeamerimport.py | 11 ++++-- openlp/plugins/songs/lib/songimport.py | 35 ++++++++++++++----- .../plugins/songs/lib/songshowplusimport.py | 22 +++++------- openlp/plugins/songs/lib/ui.py | 3 ++ openlp/plugins/songs/lib/wowimport.py | 24 +++++-------- 14 files changed, 97 insertions(+), 69 deletions(-) diff --git a/openlp/plugins/songs/forms/songimportform.py b/openlp/plugins/songs/forms/songimportform.py index 4986c26a4..e3b44237f 100644 --- a/openlp/plugins/songs/forms/songimportform.py +++ b/openlp/plugins/songs/forms/songimportform.py @@ -741,15 +741,19 @@ class SongImportForm(OpenLPWizard): filenames=self.getListOfFiles(self.foilPresenterFileListWidget) ) test = importer.do_import() + importer.write_error_report() if isinstance(test, bool): raise received_boolean if importer.stop_import_flag: - print importer.import_error_log - print u'cancelled' + self.progressLabel.setText(translate('SongsPlugin.SongImportForm', + 'Song import has been cancelled.')) elif importer.import_error_log: - self.progressLabel.setText(self.progressLabel.setText( - translate('SongsPlugin.SongImportForm', - 'Your song import failed.'))) + error_path = importer.write_error_report() + self.progressLabel.setTextInteractionFlags( + QtCore.Qt.TextSelectableByMouse) + self.progressLabel.setText(unicode(translate( + 'SongsPlugin.SongImportForm', 'Your song import failed. See ' + 'the error report for more details:\n%s')) % error_path) else: self.progressLabel.setText(WizardStrings.FinishedImport) diff --git a/openlp/plugins/songs/lib/cclifileimport.py b/openlp/plugins/songs/lib/cclifileimport.py index 35e138a19..d304b0241 100644 --- a/openlp/plugins/songs/lib/cclifileimport.py +++ b/openlp/plugins/songs/lib/cclifileimport.py @@ -79,10 +79,12 @@ class CCLIFileImport(SongImport): ext = os.path.splitext(filename)[1] if ext.lower() == u'.usr': log.info(u'SongSelect .usr format file found: %s', filename) - self.do_import_usr_file(lines) + if not self.do_import_usr_file(lines): + self.log_error(filename) elif ext.lower() == u'.txt': log.info(u'SongSelect .txt format file found: %s', filename) - self.do_import_txt_file(lines) + if not self.do_import_txt_file(lines): + self.log_error(filename) else: self.log_error(filename, translate('SongsPlugin.CCLIFileImport', @@ -214,7 +216,7 @@ class CCLIFileImport(SongImport): else: self.add_author(author) self.topics = [topic.strip() for topic in song_topics.split(u'/t')] - self.finish() + return self.finish() def do_import_txt_file(self, textList): """ @@ -331,4 +333,4 @@ class CCLIFileImport(SongImport): author_list = song_author.split(u'|') # Clean spaces before and after author names. [self.add_author(author_name.strip()) for author_name in author_list] - self.finish() + return self.finish() diff --git a/openlp/plugins/songs/lib/easislidesimport.py b/openlp/plugins/songs/lib/easislidesimport.py index f2b38c26e..0c710377a 100644 --- a/openlp/plugins/songs/lib/easislidesimport.py +++ b/openlp/plugins/songs/lib/easislidesimport.py @@ -70,7 +70,6 @@ class EasiSlidesImport(SongImport): self._parse_song(song) def _parse_song(self, song): - self.set_defaults() self._success = True self._add_unicode_attribute(u'title', song.Title1, True) self._add_unicode_attribute(u'alternate_title', song.Title2) @@ -84,7 +83,10 @@ class EasiSlidesImport(SongImport): self._add_unicode_attribute(u'song_book_name', song.BookReference) self._parse_and_add_lyrics(song) if self._success: - self.finish() + if not self.finish(): + self.log_error(song.Title1 if song.Title1 else u'') + else: + self.set_defaults() def _add_unicode_attribute(self, self_attribute, import_attribute, mandatory=False): @@ -116,10 +118,8 @@ class EasiSlidesImport(SongImport): def _add_authors(self, song): try: authors = unicode(song.Writer).split(u',') - for author in authors: - author = author.strip() - if len(author): - self.authors.append(author) + self.authors = \ + [author.strip() for author in authors if author.strip()] except UnicodeDecodeError: log.exception(u'Unicode decode error while decoding Writer') self._success = False diff --git a/openlp/plugins/songs/lib/ewimport.py b/openlp/plugins/songs/lib/ewimport.py index 66f6eec42..85a0b88c2 100644 --- a/openlp/plugins/songs/lib/ewimport.py +++ b/openlp/plugins/songs/lib/ewimport.py @@ -263,7 +263,8 @@ class EasyWorshipSongImport(SongImport): verse.strip(), VerseType.Tags[VerseType.Verse]) if self.stop_import_flag: break - self.finish() + if not self.finish(): + self.log_error(self.import_source) db_file.close() self.memo_file.close() diff --git a/openlp/plugins/songs/lib/foilpresenterimport.py b/openlp/plugins/songs/lib/foilpresenterimport.py index dd98bc415..dcbc4367d 100644 --- a/openlp/plugins/songs/lib/foilpresenterimport.py +++ b/openlp/plugins/songs/lib/foilpresenterimport.py @@ -97,6 +97,7 @@ from openlp.core.ui.wizard import WizardStrings from openlp.plugins.songs.lib import clean_song, VerseType from openlp.plugins.songs.lib.songimport import SongImport from openlp.plugins.songs.lib.db import Author, Book, Song, Topic +from openlp.plugins.songs.lib.ui import SongStrings from openlp.plugins.songs.lib.xml import SongXML log = logging.getLogger(__name__) @@ -128,8 +129,10 @@ class FoilPresenterImport(SongImport): parsed_file = etree.parse(file_path, parser) xml = unicode(etree.tostring(parsed_file)) if self.FoilPresenter.xml_to_song(xml) is None: + self.log_error(file_path, SongStrings.NoXML) log.debug(u'File could not be imported: %s' % file_path) except etree.XMLSyntaxError: + self.log_error(file_path, SongStrings.XMLSyntaxError) log.exception(u'XML syntax error in file %s' % file_path) diff --git a/openlp/plugins/songs/lib/olp1import.py b/openlp/plugins/songs/lib/olp1import.py index 765794d05..26fdf19a9 100644 --- a/openlp/plugins/songs/lib/olp1import.py +++ b/openlp/plugins/songs/lib/olp1import.py @@ -143,7 +143,8 @@ class OpenLP1SongImport(SongImport): break if self.stop_import_flag: break - self.finish() + if not self.finish(): + self.log_error(self.import_source) def get_encoding(self): """ diff --git a/openlp/plugins/songs/lib/olpimport.py b/openlp/plugins/songs/lib/olpimport.py index d89ba5c46..c69e5ded0 100644 --- a/openlp/plugins/songs/lib/olpimport.py +++ b/openlp/plugins/songs/lib/olpimport.py @@ -155,15 +155,9 @@ class OpenLPSongImport(SongImport): mapper(OldTopic, source_topics_table) source_songs = self.source_session.query(OldSong).all() - song_total = len(source_songs) if self.import_wizard: - self.import_wizard.progressBar.setMaximum(song_total) - song_count = 1 + self.import_wizard.progressBar.setMaximum(len(source_songs)) for song in source_songs: - if self.import_wizard: - self.import_wizard.incrementProgressBar( - unicode(translate('SongsPlugin.OpenLPSongImport', - 'Importing song %d of %d.')) % (song_count, song_total)) new_song = Song() new_song.title = song.title if has_media_files and hasattr(song, 'alternate_title'): @@ -218,7 +212,9 @@ class OpenLPSongImport(SongImport): # file_name=media_file.file_name)) clean_song(self.manager, new_song) self.manager.save_object(new_song) - song_count += 1 + if self.import_wizard: + self.import_wizard.incrementProgressBar( + WizardStrings.ImportingType % new_song.title) if self.stop_import_flag: break engine.dispose() diff --git a/openlp/plugins/songs/lib/openlyricsimport.py b/openlp/plugins/songs/lib/openlyricsimport.py index cf46d7449..687a949af 100644 --- a/openlp/plugins/songs/lib/openlyricsimport.py +++ b/openlp/plugins/songs/lib/openlyricsimport.py @@ -35,6 +35,7 @@ from lxml import etree from openlp.core.ui.wizard import WizardStrings from openlp.plugins.songs.lib.songimport import SongImport +from openlp.plugins.songs.lib.ui import SongStrings from openlp.plugins.songs.lib import OpenLyrics log = logging.getLogger(__name__) @@ -69,5 +70,7 @@ class OpenLyricsImport(SongImport): xml = unicode(etree.tostring(parsed_file)) if self.openLyrics.xml_to_song(xml) is None: log.debug(u'File could not be imported: %s' % file_path) + self.log_error(file_path, SongStrings.NoXML) except etree.XMLSyntaxError: log.exception(u'XML syntax error in file %s' % file_path) + self.log_error(file_path, SongStrings.XMLSyntaxError) diff --git a/openlp/plugins/songs/lib/opensongimport.py b/openlp/plugins/songs/lib/opensongimport.py index aebb865b8..7cff1522f 100644 --- a/openlp/plugins/songs/lib/opensongimport.py +++ b/openlp/plugins/songs/lib/opensongimport.py @@ -35,6 +35,7 @@ from lxml.etree import Error, LxmlError from openlp.core.ui.wizard import WizardStrings from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib.songimport import SongImport +from openlp.plugins.songs.lib.ui import SongStrings log = logging.getLogger(__name__) @@ -181,9 +182,6 @@ class OpenSongImport(SongImport): setattr(self, fn_or_string, ustring) else: fn_or_string(ustring) - if not len(self.title): - # to prevent creation of empty songs from wrong files - return if u'theme' in fields and unicode(root.theme) not in self.topics: self.topics.append(unicode(root.theme)) if u'alttheme' in fields and unicode(root.alttheme) not in self.topics: @@ -274,10 +272,11 @@ class OpenSongImport(SongImport): verse_tag = verse_def verse_num = u'1' verse_def = u'%s%s' % (verse_tag, verse_num) - if verses.has_key(verse_tag) \ - and verses[verse_tag].has_key(verse_num): + if verses.has_key(verse_tag) and \ + verses[verse_tag].has_key(verse_num): self.verse_order_list.append(verse_def) else: log.info(u'Got order %s but not in verse tags, dropping' u'this item from presentation order', verse_def) - self.finish() + if not self.finish(): + self.log_error(file.name) diff --git a/openlp/plugins/songs/lib/songbeamerimport.py b/openlp/plugins/songs/lib/songbeamerimport.py index be0fae589..2813240a4 100644 --- a/openlp/plugins/songs/lib/songbeamerimport.py +++ b/openlp/plugins/songs/lib/songbeamerimport.py @@ -35,6 +35,7 @@ import re from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib.songimport import SongImport +from openlp.plugins.songs.lib.ui import SongStrings log = logging.getLogger(__name__) @@ -72,6 +73,11 @@ class SongBeamerImport(SongImport): Initialise the Song Beamer importer. """ SongImport.__init__(self, manager, **kwargs) + self.log_error(u'/home/andreas/1.sng', u'aaaa') + self.log_error(u'/home/andreas/4.sng', u'asdfsdfsadfds') + self.log_error(u'/home/andreas/3.sng', u'asdf3q4') + self.log_error(u'/home/andreas/2.sng', u'sadfasdf') + self.log_error(u'/home/andreas/รค.sng', u'kqwjw32w3') def do_import(self): """ @@ -124,7 +130,8 @@ class SongBeamerImport(SongImport): if self.current_verse: self.replace_html_tags() self.add_verse(self.current_verse, self.current_verse_type) - self.finish() + if not self.finish(): + self.log_error(file) def replace_html_tags(self): """ @@ -184,7 +191,7 @@ class SongBeamerImport(SongImport): elif tag_val[0] == u'#Bible': pass elif tag_val[0] == u'#Categories': - self.topics = line.split(',') + self.topics = tag_val[1].split(',') elif tag_val[0] == u'#CCLI': self.ccli_number = tag_val[1] elif tag_val[0] == u'#Chords': diff --git a/openlp/plugins/songs/lib/songimport.py b/openlp/plugins/songs/lib/songimport.py index 87aa09bcb..80eebde43 100644 --- a/openlp/plugins/songs/lib/songimport.py +++ b/openlp/plugins/songs/lib/songimport.py @@ -23,13 +23,17 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### - +import codecs +import datetime import logging +import os import re + from PyQt4 import QtCore from openlp.core.lib import Receiver, translate from openlp.core.ui.wizard import WizardStrings +from openlp.core.utils import AppLocation from openlp.plugins.songs.lib import clean_song, VerseType from openlp.plugins.songs.lib.db import Song, Author, Topic, Book, MediaFile from openlp.plugins.songs.lib.ui import SongStrings @@ -67,7 +71,7 @@ class SongImport(QtCore.QObject): self.song = None self.stop_import_flag = False self.set_defaults() - self.import_error_log = [] + self.error_log = [] QtCore.QObject.connect(Receiver.get_receiver(), QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import) @@ -96,7 +100,7 @@ class SongImport(QtCore.QObject): self.copyright_string = unicode(translate( 'SongsPlugin.SongImport', 'copyright')) - def log_error(self, filepath, reason=None): + def log_error(self, filepath, reason=SongStrings.SongIncomplete): """ This should be called, when a song could not be imported. @@ -109,7 +113,22 @@ class SongImport(QtCore.QObject): The reason, why the import failed. The string should be as informative as possible. """ - self.import_error_log.append((filepath, unicode(reason))) + self.error_log.append((filepath, unicode(reason))) + + def write_error_report(self): + """ + Creates a error import containing all error messages. + """ + report_path = os.path.join(AppLocation.get_data_path(), unicode(translate( + 'SongsPlugin.SongImport','song_import_report (%s).txt')) % + datetime.datetime.now().strftime(u'%Y-%m-%dT%H:%M:%S')) + report_file = codecs.open(report_path, u'w', u'utf-8') + report_file.write(translate('SongsPlugin.SongImport', + 'The following songs could not be imported:\n')) + for filepath, reason in self.error_log: + report_file.write(u'- %s (%s)\n' % (filepath, reason)) + report_file.close() + return report_path def stop_import(self): """ @@ -257,7 +276,7 @@ class SongImport(QtCore.QObject): Author not checked here, if no author then "Author unknown" is automatically added """ - if self.title == u'' or len(self.verses) == 0: + if not self.title or not len(self.verses): return False else: return True @@ -268,13 +287,12 @@ class SongImport(QtCore.QObject): """ if not self.check_complete(): self.set_defaults() - return + return False log.info(u'committing song %s to database', self.title) song = Song() song.title = self.title self.import_wizard.incrementProgressBar( WizardStrings.ImportingType % song.title) - print WizardStrings.ImportingType song.alternate_title = self.alternate_title # Values will be set when cleaning the song. song.search_title = u'' @@ -331,7 +349,7 @@ class SongImport(QtCore.QObject): publisher=self.song_book_pub) song.book = song_book for topictext in self.topics: - if len(topictext) == 0: + if not topictext: continue topic = self.manager.get_object_filtered(Topic, Topic.name == topictext) @@ -341,6 +359,7 @@ class SongImport(QtCore.QObject): clean_song(self.manager, song) self.manager.save_object(song) self.set_defaults() + return True def print_song(self): """ diff --git a/openlp/plugins/songs/lib/songshowplusimport.py b/openlp/plugins/songs/lib/songshowplusimport.py index 28f07ea31..27620d11a 100644 --- a/openlp/plugins/songs/lib/songshowplusimport.py +++ b/openlp/plugins/songs/lib/songshowplusimport.py @@ -169,12 +169,11 @@ class SongShowPlusImport(SongImport): self.add_verse(unicode(data, u'cp1252'), verseTag) else: log.debug("Unrecognised blockKey: %s, data: %s" - %(blockKey, data)) + % (blockKey, data)) self.verse_order_list = self.sspVerseOrderList songData.close() - self.finish() - self.import_wizard.incrementProgressBar( - WizardStrings.ImportingType % file_name) + if not self.finish(): + self.log_error(file) def toOpenLPVerseTag(self, verseName, ignoreUnique=False): if verseName.find(" ") != -1: @@ -186,22 +185,19 @@ class SongShowPlusImport(SongImport): verseNumber = "1" verseType = verseType.lower() if verseType == "verse": - verseTag = "V" + verseTag = VerseType.Tags[VerseType.Verse] elif verseType == "chorus": - verseTag = "C" + verseTag = VerseType.Tags[VerseType.Chorus] elif verseType == "bridge": - verseTag = "B" + verseTag = VerseType.Tags[VerseType.Bridge] elif verseType == "pre-chorus": - verseTag = "P" - elif verseType == "bridge": - verseTag = "B" + verseTag = VerseType.Tags[VerseType.PreChorus] else: if not self.otherList.has_key(verseName): if ignoreUnique: return None self.otherCount = self.otherCount + 1 self.otherList[verseName] = str(self.otherCount) - verseTag = "O" + verseTag = VerseType.Tags[VerseType.Other] verseNumber = self.otherList[verseName] - verseTag = verseTag + verseNumber - return verseTag + return verseTag + verseNumber diff --git a/openlp/plugins/songs/lib/ui.py b/openlp/plugins/songs/lib/ui.py index 0a389087e..e9dfd0f13 100644 --- a/openlp/plugins/songs/lib/ui.py +++ b/openlp/plugins/songs/lib/ui.py @@ -38,8 +38,11 @@ class SongStrings(object): Authors = translate('OpenLP.Ui', 'Authors', 'Plural') AuthorUnknown = u'Author Unknown' # Used to populate the database. CopyrightSymbol = translate('OpenLP.Ui', '\xa9', 'Copyright symbol.') + NoXML = translate('OpenLP.Ui', 'Song does not contain any XML') SongBook = translate('OpenLP.Ui', 'Song Book', 'Singular') SongBooks = translate('OpenLP.Ui', 'Song Books', 'Plural') + SongIncomplete = translate('OpenLP.Ui','Title and/or verses not found') SongMaintenance = translate('OpenLP.Ui', 'Song Maintenance') Topic = translate('OpenLP.Ui', 'Topic', 'Singular') Topics = translate('OpenLP.Ui', 'Topics', 'Plural') + XMLSyntaxError = translate('OpenLP.Ui', 'XML syntax error') diff --git a/openlp/plugins/songs/lib/wowimport.py b/openlp/plugins/songs/lib/wowimport.py index e6495a2b9..661e6787d 100644 --- a/openlp/plugins/songs/lib/wowimport.py +++ b/openlp/plugins/songs/lib/wowimport.py @@ -105,11 +105,7 @@ class WowImport(SongImport): if isinstance(self.import_source, list): self.import_wizard.progressBar.setMaximum(len(self.import_source)) for file in self.import_source: - author = u'' - copyright = u'' file_name = os.path.split(file)[1] - self.import_wizard.incrementProgressBar( - WizardStrings.ImportingType % file_name, 0) # Get the song title self.title = file_name.rpartition(u'.')[0] songData = open(file, 'rb') @@ -129,7 +125,7 @@ class WowImport(SongImport): self.line_text = unicode( songData.read(ord(songData.read(1))), u'cp1252') songData.seek(1, os.SEEK_CUR) - if block_text != u'': + if block_text: block_text += u'\n' block_text += self.line_text self.lines_to_read -= 1 @@ -143,16 +139,14 @@ class WowImport(SongImport): self.add_verse(block_text, block_type) # Now to extract the author author_length = ord(songData.read(1)) - if author_length != 0: - author = unicode(songData.read(author_length), u'cp1252') + if author_length: + self.parse_author( + unicode(songData.read(author_length), u'cp1252')) # Finally the copyright copyright_length = ord(songData.read(1)) - if copyright_length != 0: - copyright = unicode( - songData.read(copyright_length), u'cp1252') - self.parse_author(author) - self.add_copyright(copyright) + if copyright_length: + self.add_copyright(unicode( + songData.read(copyright_length), u'cp1252')) songData.close() - self.finish() - self.import_wizard.incrementProgressBar( - WizardStrings.ImportingType % file_name) + if not self.finish(): + self.log_error(file)