From a976d6b59ac54754c670175b182a5daa3f1af1ff Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Sun, 17 Jan 2010 18:48:45 +0200 Subject: [PATCH] A few fixes after renaming some functions. --- openlp/plugins/bibles/lib/csv.py | 5 +- openlp/plugins/bibles/lib/db.py | 11 +++-- openlp/plugins/bibles/lib/http.py | 3 +- openlp/plugins/bibles/lib/manager.py | 69 ++++++++++++++------------- openlp/plugins/bibles/lib/opensong.py | 25 ++++++---- openlp/plugins/bibles/lib/osis.py | 5 +- 6 files changed, 68 insertions(+), 50 deletions(-) diff --git a/openlp/plugins/bibles/lib/csv.py b/openlp/plugins/bibles/lib/csv.py index ac53f46cc..cd3b7efeb 100644 --- a/openlp/plugins/bibles/lib/csv.py +++ b/openlp/plugins/bibles/lib/csv.py @@ -106,7 +106,7 @@ class CSVBible(BibleDB): self.wizard.incrementProgressBar( u'Importing %s %s' % book.name) self.commit() - self.add_verse(book.id, p[1], p[2], p3) + self.create_verse(book.id, p[1], p[2], p3) Receiver.send_message(u'process_events') self.commit() except: @@ -119,4 +119,5 @@ class CSVBible(BibleDB): self.wizard.incrementProgressBar(u'Import canceled!') return False else: - return success \ No newline at end of file + return success + diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index cf272d6e9..6de5217bb 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -25,6 +25,7 @@ import os import logging +import chardet from sqlalchemy import or_ from PyQt4 import QtCore @@ -84,13 +85,14 @@ class BibleDB(): self.metadata, self.session = init_models(db_url) self.metadata.create_all(checkfirst=True) - def register(self): + def register(self, wizard): """ This method basically just initialialises the database. It is called from the Bible Manager when a Bible is imported. Descendant classes may want to override this method to supply their own custom initialisation as well. """ + self.wizard = wizard self.create_tables() return self.name @@ -100,7 +102,7 @@ class BibleDB(): def create_tables(self): log.debug(u'createTables') - self.save_meta(u'dbversion', u'2') + self.create_meta(u'dbversion', u'2') self.create_testament(u'Old Testament') self.create_testament(u'New Testament') self.create_testament(u'Apocrypha') @@ -111,7 +113,7 @@ class BibleDB(): self.commit() def create_book(self, name, abbrev, testament=1): - log.debug(u'create_book %s,%s', bookname, bookabbrev) + log.debug(u'create_book %s,%s', name, abbrev) book = Book.populate(name=name, abbreviation=abbrev, testament_id=testament) self.session.add(book) @@ -132,6 +134,9 @@ class BibleDB(): self.commit() def create_verse(self, book_id, chapter, verse, text): + if not isinstance(text, unicode): + details = chardet.detect(text) + text = unicode(text, details[u'encoding']) verse = Verse.populate( book_id=book_id, chapter=chapter, diff --git a/openlp/plugins/bibles/lib/http.py b/openlp/plugins/bibles/lib/http.py index 7f1c835f8..5c3ac53b0 100644 --- a/openlp/plugins/bibles/lib/http.py +++ b/openlp/plugins/bibles/lib/http.py @@ -232,4 +232,5 @@ class HTTPBible(BibleDB): ev = BGExtract(self.proxyurl) return ev.get_bible_chapter(self.bibleid, book, chapter) except: - log.exception("Failed to get bible chapter") \ No newline at end of file + log.exception("Failed to get bible chapter") + diff --git a/openlp/plugins/bibles/lib/manager.py b/openlp/plugins/bibles/lib/manager.py index 9a667d916..1a9d01d9d 100644 --- a/openlp/plugins/bibles/lib/manager.py +++ b/openlp/plugins/bibles/lib/manager.py @@ -61,13 +61,13 @@ class BibleFormat(object): Return the appropriate imeplementation class. """ if id == BibleFormat.OSIS: - return BibleOSISImpl + return OSISBible elif id == BibleFormat.CSV: - return BibleCSVImpl + return CSVBible elif id == BibleFormat.OpenSong: - return BibleOpenSongImpl + return OpenSongBible elif id == BibleFormat.WebDownload: - return BibleHTTPImpl + return HTTPBible else: return None @@ -161,10 +161,13 @@ class BibleManager(object): try: fbibles = open(filepath, u'r') for line in fbibles: - p = line.split(u',') - self.book_abbreviations[p[0]] = p[1].replace(u'\n', '') - self.book_testaments[p[0]] = p[2].replace(u'\n', '') - self.book_chapters.append({u'book':p[0], u'total':p[3].replace(u'\n', '')}) + parts = line.split(u',') + self.book_abbreviations[parts[0]] = parts[1].replace(u'\n', '') + self.book_testaments[parts[0]] = parts[2].replace(u'\n', '') + self.book_chapters.append({ + u'book': parts[0], + u'total': parts[3].replace(u'\n', '') + }) except: log.exception(u'Failed to load bible') finally: @@ -192,10 +195,10 @@ class BibleManager(object): Keyword arguments to send to the actualy importer class. """ class_ = BibleFormat.get_class(type) - kwargs[u'path'] = self.path - kwargs[u'config'] = self.config + kwargs['path'] = self.path + kwargs['config'] = self.config importer = class_(**kwargs) - name = importer.register() + name = importer.register(self.import_wizard) self.db_cache[name] = importer return importer.do_import() @@ -231,7 +234,7 @@ class BibleManager(object): nbible = BibleDBImpl(self.bible_path, biblename, self.config) # Create Database nbible.create_tables() - self.bible_db_cache[biblename] = nbible + self.db_cache[biblename] = nbible nhttp = BibleHTTPImpl() nhttp.set_bible_source(biblesource) self.bible_http_cache[biblename] = nhttp @@ -366,16 +369,16 @@ class BibleManager(object): log.debug(u'get_book_verse_count %s,%s,%s', bible, book, chapter) web, bible = self.is_bible_web(bible) if web: - count = self.bible_db_cache[bible].get_max_bible_book_verses( + count = self.db_cache[bible].get_max_bible_book_verses( book, chapter) if count == 0: # Make sure the first chapter has been downloaded self.get_verse_text(bible, book, chapter, chapter, 1, 1) - count = self.bible_db_cache[bible].get_max_bible_book_verses( + count = self.db_cache[bible].get_max_bible_book_verses( book, chapter) return count else: - return self.bible_db_cache[bible].get_max_bible_book_verses( + return self.db_cache[bible].get_max_bible_book_verses( book, chapter) def get_verses(self, bible, versetext): @@ -386,7 +389,7 @@ class BibleManager(object): log.debug(u'get_verses_from_text %s,%s', bible, versetext) reflist = parse_reference(versetext) web, bible = self.is_bible_web(bible) - return self.bible_db_cache[bible].get_verses(reflist) + return self.db_cache[bible].get_verses(reflist) def save_meta_data(self, bible, version, copyright, permissions): """ @@ -394,9 +397,9 @@ class BibleManager(object): """ log.debug(u'save_meta data %s,%s, %s,%s', bible, version, copyright, permissions) - self.bible_db_cache[bible].save_meta(u'Version', version) - self.bible_db_cache[bible].save_meta(u'Copyright', copyright) - self.bible_db_cache[bible].save_meta(u'Permissions', permissions) + self.db_cache[bible].create_meta(u'Version', version) + self.db_cache[bible].create_meta(u'Copyright', copyright) + self.db_cache[bible].create_meta(u'Permissions', permissions) def get_meta_data(self, bible, key): """ @@ -404,7 +407,7 @@ class BibleManager(object): """ log.debug(u'get_meta %s,%s', bible, key) web, bible = self.is_bible_web(bible) - return self.bible_db_cache[bible].get_meta(key) + return self.db_cache[bible].get_meta(key) def get_verse_text(self, bible, bookname, schapter, echapter, sverse, everse=0): @@ -425,8 +428,8 @@ class BibleManager(object): # check to see if book/chapter exists fow HTTP bibles and load cache # if necessary web, bible = self.is_bible_web(bible) - if self.bible_http_cache[bible]: - book = self.bible_db_cache[bible].get_bible_book(bookname) + if self.http_cache[bible]: + book = self.db_cache[bible].get_bible_book(bookname) if book is None: log.debug(u'get_verse_text : new book') for chapter in range(schapter, echapter + 1): @@ -434,7 +437,7 @@ class BibleManager(object): unicode(self.media.trUtf8('Downloading %s: %s')) % (bookname, chapter)) search_results = \ - self.bible_http_cache[bible].get_bible_chapter( + self.http_cache[bible].get_bible_chapter( bible, bookname, chapter) if search_results.has_verselist() : ## We have found a book of the bible lets check to see @@ -443,33 +446,33 @@ class BibleManager(object): ## to request ac and get Acts back. bookname = search_results.get_book() # check to see if book/chapter exists - book = self.bible_db_cache[bible].get_bible_book( + book = self.db_cache[bible].get_bible_book( bookname) if book is None: ## Then create book, chapter and text - book = self.bible_db_cache[bible].create_book( + book = self.db_cache[bible].create_book( bookname, self.book_abbreviations[bookname], self.book_testaments[bookname]) log.debug(u'New http book %s, %s, %s', book, book.id, book.name) - self.bible_db_cache[bible].create_chapter( + self.db_cache[bible].create_chapter( book.id, search_results.get_chapter(), search_results.get_verselist()) else: ## Book exists check chapter and texts only. - v = self.bible_db_cache[bible].get_bible_chapter( + v = self.db_cache[bible].get_bible_chapter( book.id, chapter) if v is None: self.media.setQuickMessage( unicode(self.media.trUtf8('%Downloading %s: %s'))\ % (bookname, chapter)) - self.bible_db_cache[bible].create_chapter( + self.db_cache[bible].create_chapter( book.id, chapter, search_results.get_verselist()) else: log.debug(u'get_verse_text : old book') for chapter in range(schapter, echapter + 1): - v = self.bible_db_cache[bible].get_bible_chapter( + v = self.db_cache[bible].get_bible_chapter( book.id, chapter) if v is None: try: @@ -477,17 +480,17 @@ class BibleManager(object): unicode(self.media.trUtf8('Downloading %s: %s')) % (bookname, chapter)) search_results = \ - self.bible_http_cache[bible].get_bible_chapter( + self.http_cache[bible].get_bible_chapter( bible, bookname, chapter) if search_results.has_verselist(): - self.bible_db_cache[bible].create_chapter( + self.db_cache[bible].create_chapter( book.id, search_results.get_chapter(), search_results.get_verselist()) except: log.exception(u'Problem getting scripture online') #Now get verses from database if schapter == echapter: - text = self.bible_db_cache[bible].get_bible_text(bookname, + text = self.db_cache[bible].get_bible_text(bookname, schapter, sverse, everse) else: for i in range (schapter, echapter + 1): @@ -501,7 +504,7 @@ class BibleManager(object): start = 1 end = self.get_book_verse_count(bible, bookname, i) - txt = self.bible_db_cache[bible].get_bible_text( + txt = self.db_cache[bible].get_bible_text( bookname, i, start, end) text.extend(txt) return text diff --git a/openlp/plugins/bibles/lib/opensong.py b/openlp/plugins/bibles/lib/opensong.py index 93820be48..64eeeb0f9 100644 --- a/openlp/plugins/bibles/lib/opensong.py +++ b/openlp/plugins/bibles/lib/opensong.py @@ -49,11 +49,11 @@ class OpenSongBible(BibleDB): """ log.debug(__name__) BibleDB.__init__(self, **kwargs) - if u'filename' not in kwargs: + if 'filename' not in kwargs: raise KeyError(u'You have to supply a file name to import from.') - self.filename = kwargs[u'filename'] - QtCore.QObject.connect(Receiver.get_receiver(), - QtCore.SIGNAL(u'openlpstopimport'), self.stop_import) + self.filename = kwargs['filename'] + #QtCore.QObject.connect(Receiver.get_receiver(), + # QtCore.SIGNAL(u'openlpstopimport'), self.stop_import) def stop_import(self): """ @@ -67,10 +67,12 @@ class OpenSongBible(BibleDB): Loads a Bible from file. """ log.debug(u'Starting OpenSong import from "%s"' % self.filename) + self.filename = unicode(self.filename, u'utf-8') + self.wizard.incrementProgressBar(u'Preparing for import...') detect_file = None try: detect_file = open(self.filename, u'r') - details = chardet.detect(detect_file.read(3000)) + details = chardet.detect(detect_file.read()) except: log.exception(u'Failed to detect OpenSong file encoding') return False @@ -94,11 +96,16 @@ class OpenSongBible(BibleDB): for verse in chapter.v: if self.stop_import: break - self.add_verse(db_book.id, chapter.attrib[u'n'], - verse.attrib[u'n'], verse.text) + self.create_verse( + db_book.id, + int(chapter.attrib[u'n']), + int(verse.attrib[u'n']), + verse.text + ) Receiver.send_message(u'process_events') - self.wizard.incrementProgressBar(u'Importing %s %s' % \ - (dbbook.name, str(chapter.attrib[u'n']))) + self.wizard.incrementProgressBar( + QtCore.QString('Importing %s %s' % \ + (db_book.name, chapter.attrib[u'n']))) self.commit() except: log.exception(u'Loading bible from OpenSong file failed') diff --git a/openlp/plugins/bibles/lib/osis.py b/openlp/plugins/bibles/lib/osis.py index 4f18b27c5..a7e4ba374 100644 --- a/openlp/plugins/bibles/lib/osis.py +++ b/openlp/plugins/bibles/lib/osis.py @@ -160,7 +160,7 @@ class OSISBible(BibleDB): .replace(u'', u'').replace(u'', u'')\ .replace(u'', u'') verse_text = self.spaces_regex.sub(u' ', verse_text) - self.add_verse(db_book.id, chapter, verse, verse_text) + self.create_verse(db_book.id, chapter, verse, verse_text) Receiver.send_message(u'process_events') self.commit() self.wizard.incrementProgressBar(u'Finishing import...') @@ -174,4 +174,5 @@ class OSISBible(BibleDB): self.wizard.incrementProgressBar(u'Import canceled!') return False else: - return success \ No newline at end of file + return success +