Fix unicode bible import error

More Song changes
This commit is contained in:
Tim Bentley 2009-06-04 21:06:32 +01:00
parent 5b58f7137f
commit 11a9d28bed
6 changed files with 116 additions and 73 deletions

View File

@ -15,7 +15,7 @@ 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 os
import os.path
import logging
from openlp.plugins.bibles.lib.bibleDBimpl import BibleDBImpl
@ -23,28 +23,28 @@ from openlp.core.lib import Receiver
from PyQt4 import QtCore
class BibleOSISImpl():
global log
global log
log=logging.getLogger(u'BibleOSISImpl')
log.info(u'BibleOSISImpl loaded')
def __init__(self, biblepath, bibledb):
self.bibledb = bibledb
self.booksOfBible = {} # books of the bible linked to bibleid {osis , name}
self.abbrevOfBible = {} # books of the bible linked to bibleid {osis ,Abbrev }
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(filepath, '..', 'resources','osisbooks.csv'))
filepath = os.path.abspath(os.path.join(filepath, '..', 'resources','osisbooks.csv'))
fbibles=open(filepath, 'r')
for line in fbibles:
p = line.split(",")
self.booksOfBible[p[0]] = p[1].replace('\n', '')
self.abbrevOfBible[p[0]] = p[2].replace('\n', '')
self.abbrevOfBible[p[0]] = p[2].replace('\n', '')
self.loadbible = True
QtCore.QObject.connect(Receiver().get_receiver(),QtCore.SIGNAL("openlpstopimport"),self.stop_import)
def stop_import(self):
self.loadbible= False
def load_data(self, osisfile, dialogobject=None):
osis=open(osisfile, 'r')
@ -61,27 +61,27 @@ class BibleOSISImpl():
pos = file.find(verseText)
if pos > -1: # we have a verse
epos= file.find(">", pos)
ref = file[pos+15:epos-1] # Book Reference
ref = file[pos+15:epos-1] # Book Reference
#lets find the bible text only
pos = epos + 1 # find start of text
epos = file.find("</verse>", pos) # end of text
text = file[pos : epos]
text = unicode(file[pos : epos], u'utf8')
#print pos, e, f[pos:e] # Found Basic Text
#remove tags of extra information
text = self.remove_block(u'<title',u'</title>', text)
text = self.remove_block(u'<note',u'</note>', text)
text = self.remove_block(u'<divineName',u'</divineName>', text)
text = self.remove_tag(u'<lb', text)
text = self.remove_tag(u'<q', text)
text = self.remove_tag(u'<l', text)
text = self.remove_tag(u'<lg', text)
text = self.remove_block(u'<note',u'</note>', text)
text = self.remove_block(u'<divineName',u'</divineName>', text)
text = self.remove_tag(u'<lb', text)
text = self.remove_tag(u'<q', text)
text = self.remove_tag(u'<l', text)
text = self.remove_tag(u'<lg', text)
# Strange tags where the end is not the same as the start
# The must be in this order as at least one bible has them
# crossing and the removal does not work.
# crossing and the removal does not work.
pos = text.find(u'<FI>')
while pos > -1:
epos = text.find(u'<Fi>', pos)
@ -90,7 +90,7 @@ class BibleOSISImpl():
pos = -1
else:
text = text[:pos] + text[epos + 4: ]
pos = text.find(u'<FI>')
pos = text.find(u'<FI>')
pos = text.find(u'<RF>')
while pos > -1:
@ -98,9 +98,9 @@ class BibleOSISImpl():
text = text[:pos] + text[epos + 4: ]
#print "X", pos, epos, text
pos = text.find(u'<RF>')
p = ref.split(u'.', 3) # split up the reference
#print p, ">>>", text
#print p, ">>>", text
if book_ptr != p[0]:
if book_ptr == None: # first time through
@ -113,14 +113,14 @@ class BibleOSISImpl():
book_ptr = p[0]
book = self.bibledb.create_book(self.booksOfBible[p[0]] , self.abbrevOfBible[p[0]], testament)
dialogobject.incrementProgressBar(self.booksOfBible[p[0]] )
Receiver().send_message("openlpprocessevents")
Receiver().send_message("openlpprocessevents")
count = 0
self.bibledb.add_verse(book.id, p[1], p[2], text)
count += 1
if count % 3 == 0: #Every 3 verses repaint the screen
Receiver().send_message("openlpprocessevents")
Receiver().send_message("openlpprocessevents")
count = 0
def remove_block(self, start_tag, end_tag, text):
"""
removes a block of text between two tags
@ -134,9 +134,9 @@ class BibleOSISImpl():
pos = -1
else:
text = text[:pos] + text[epos + len(end_tag): ]
pos = text.find(start_tag)
pos = text.find(start_tag)
return text
def remove_tag(self, start_tag, text):
"""
removes a single tag
@ -146,5 +146,5 @@ class BibleOSISImpl():
while pos > -1:
epos = text.find(u'/>', pos)
text = text[:pos] + text[epos + 2: ]
pos = text.find(start_tag)
pos = text.find(start_tag)
return text

View File

@ -70,6 +70,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
def initialise(self):
self.loadAuthors()
self.loadTopics()
self.loadBooks()
def loadAuthors(self):
authors = self.songmanager.get_authors()
@ -78,11 +79,17 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
self.AuthorsSelectionComboItem.addItem(author.display_name)
def loadTopics(self):
topics= self.songmanager.get_topics()
topics = self.songmanager.get_topics()
self.SongTopicCombo.clear()
for topic in topics:
self.SongTopicCombo.addItem(topic.name)
def loadBooks(self):
books = self.songmanager.get_books()
self.SongbookCombo.clear()
for book in books:
self.SongbookCombo.addItem(book.name)
def loadSong(self, id):
self.song = self.songmanager.get_song(id)
self.TitleEditItem.setText(self.song.title)
@ -126,6 +133,7 @@ class EditSongForm(QtGui.QDialog, Ui_EditSongDialog):
"""
self.song_book_form.load_form()
self.song_book_form.exec_()
self.loadBooks()
def onAddVerseButtonClicked(self):
self.verse_form.setVerse('')

View File

@ -19,10 +19,6 @@ class Ui_SongBookDialog(object):
self.DialogLayout.setMargin(8)
self.DialogLayout.setObjectName("DialogLayout")
# self.BookSongListView = QtGui.QTableWidget(SongBookDialog)
# self.BookSongListView.setObjectName("BookSongListView")
# self.BookSongListView.setColumnCount(0)
# self.BookSongListView.setRowCount(0)
self.BookSongListView = QtGui.QListView()
self.BookSongListView.setAlternatingRowColors(True)
self.BookSongListData = TextListData()

View File

@ -18,19 +18,18 @@ Place, Suite 330, Boston, MA 02111-1307 USA
"""
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QDialog
from PyQt4.QtCore import pyqtSignature
from songbookdialog import Ui_SongBookDialog
from openlp.plugins.songs.lib.classes import Book
class SongBookForm(QDialog, Ui_SongBookDialog):
class SongBookForm(QtGui.QDialog, Ui_SongBookDialog):
"""
Class documentation goes here.
"""
def __init__(self,songmanager, parent = None):
def __init__(self, songmanager, parent = None):
"""
Constructor
"""
QDialog.__init__(self, parent)
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.songmanager = songmanager
self.currentRow = 0
@ -42,82 +41,85 @@ class SongBookForm(QDialog, Ui_SongBookDialog):
QtCore.SIGNAL('pressed()'), self.onClearButtonClick)
QtCore.QObject.connect(self.AddUpdateButton,
QtCore.SIGNAL('pressed()'), self.onAddUpdateButtonClick)
# QtCore.QObject.connect(self.DisplayEdit,
# QtCore.SIGNAL('pressed()'), self.onDisplayEditLostFocus)
# QtCore.QObject.connect(self.SongBookListView,
# QtCore.SIGNAL(u'clicked(QModelIndex)'), self.onSongBookListViewItemClicked)
QtCore.QObject.connect(self.NameEdit,
QtCore.SIGNAL('lostFocus()'), self.onBookNameEditLostFocus)
QtCore.QObject.connect(self.BookSongListView,
QtCore.SIGNAL(u'clicked(QModelIndex)'), self.onBooksListViewItemClicked)
def load_form(self):
"""
Refresh the screen and rest fields
"""
self.SongBookListData.resetStore()
self.BookSongListData.resetStore()
self.onClearButtonClick() # tidy up screen
SongBooks = self.songmanager.get_SongBooks()
for SongBook in SongBooks:
self.SongBookListData.addRow(SongBook.id,SongBook.display_name)
row_count = self.SongBookListData.rowCount(None)
Books = self.songmanager.get_books()
for Book in Books:
self.BookSongListData.addRow(Book.id,Book.name)
row_count = self.BookSongListData.rowCount(None)
if self.currentRow > row_count:
# in case we have delete the last row of the table
self.currentRow = row_count
row = self.SongBookListData.createIndex(self.currentRow, 0)
row = self.BookSongListData.createIndex(self.currentRow, 0)
if row.isValid():
self.SongBookListView.selectionModel().setCurrentIndex(row, QtGui.QItemSelectionModel.SelectCurrent)
self.BookSongListView.selectionModel().setCurrentIndex(row,
QtGui.QItemSelectionModel.SelectCurrent)
self._validate_form()
def onDeleteButtonClick(self):
"""
Delete the SongBook is the SongBook is not attached to any songs
Delete the Book is the Book is not attached to any songs
"""
self.songmanager.delete_SongBook(self.SongBook.id)
self.onClearButtonClick()
self.songmanager.delete_book(self.Book.id)
self.load_form()
def onDisplayEditLostFocus(self):
def onBookNameEditLostFocus(self):
self._validate_form()
def onAddUpdateButtonClick(self):
"""
Sent New or update details to the database
"""
if self.SongBook == None:
self.SongBook = SongBook()
self.SongBook.display_name = unicode(self.DisplayEdit.displayText())
self.songmanager.save_SongBook(self.SongBook)
if self.Book == None:
self.Book = Book()
self.Book.name = unicode(self.NameEdit.displayText())
self.Book.publisher = unicode(self.PublisherEdit.displayText())
self.songmanager.save_book(self.Book)
self.onClearButtonClick()
self.load_form()
self._validate_form()
def onClearButtonClick(self):
"""
Tidy up screen if clear button pressed
"""
self.DisplayEdit.setText(u'')
self.NameEdit.setText(u'')
self.PublisherEdit.setText(u'')
self.MessageLabel.setText(u'')
self.DeleteButton.setEnabled(False)
self.SongBook = None
self.AddUpdateButton.setEnabled(True)
self.Book = None
self._validate_form()
def onSongBookListViewItemClicked(self, index):
def onBooksListViewItemClicked(self, index):
"""
An SongBook has been selected display it
If the SongBook is attached to a Song prevent delete
An Book has been selected display it
If the Book is attached to a Song prevent delete
"""
self.currentRow = index.row()
id = int(self.SongBookListData.getId(index))
self.SongBook = self.songmanager.get_SongBook(id)
id = int(self.BookSongListData.getId(index))
self.Book = self.songmanager.get_book(id)
self.DisplayEdit.setText(self.SongBook.display_name)
if len(self.SongBook.songs) > 0:
self.MessageLabel.setText("SongBook in use 'Delete' is disabled")
self.NameEdit.setText(self.Book.name)
self.PublisherEdit.setText(self.Book.publisher)
if len(self.Book.songs) > 0:
self.MessageLabel.setText("Book in use 'Delete' is disabled")
self.DeleteButton.setEnabled(False)
else:
self.MessageLabel.setText("SongBook is not used")
self.MessageLabel.setText("Book is not used")
self.DeleteButton.setEnabled(True)
self._validate_form()
def _validate_form(self):
if len(self.DisplayEdit.displayText()) == 0: # We need at lease a display name
if len(self.NameEdit.displayText()) == 0: # We need at lease a display name
self.AddUpdateButton.setEnabled(False)
else:
self.AddUpdateButton.setEnabled(True)

View File

@ -52,6 +52,7 @@ class TopicsForm(QtGui.QDialog, Ui_TopicsDialog):
"""
Refresh the screen and rest fields
"""
print "topics load form start"
self.TopicsListData.resetStore()
self.onClearButtonClick() # tidy up screen
Topics = self.songmanager.get_topics()
@ -66,12 +67,14 @@ class TopicsForm(QtGui.QDialog, Ui_TopicsDialog):
self.TopicsListView.selectionModel().setCurrentIndex(row,
QtGui.QItemSelectionModel.SelectCurrent)
self._validate_form()
print "topics load form end"
def onDeleteButtonClick(self):
"""
Delete the Topic is the Topic is not attached to any songs
"""
self.songmanager.delete_topic(self.Topic.id)
self.onClearButtonClick()
self.load_form()
def onTopicNameEditLostFocus(self):

View File

@ -23,7 +23,7 @@ import sys
from sqlalchemy import asc, desc
from openlp.plugins.songs.lib.models import init_models, metadata, session, \
engine, songs_table, Song, Author, Topic
engine, songs_table, Song, Author, Topic, Book
import logging
@ -143,7 +143,6 @@ class SongManager():
return True
except:
log.error("Errow thrown %s", sys.exc_info()[1])
print "Errow thrown ", sys.exc_info()[1]
return False
def get_topics(self):
@ -180,5 +179,40 @@ class SongManager():
return True
except:
log.error("Errow thrown %s", sys.exc_info()[1])
print "Errow thrown ", sys.exc_info()[1]
return False
def get_books(self):
"""
Returns a list of all the Books
"""
return self.session.query(Book).order_by(Book.name).all()
def get_book(self, id):
"""
Details of the Books
"""
return self.session.query(Book).get(id)
def save_book(self, book):
"""
Save the Book
"""
try:
self.session.add(book)
self.session.commit()
return True
except:
return False
def delete_book(self, bookid):
"""
Delete the Book
"""
book = self.get_book(bookid)
try:
self.session.delete(book)
self.session.commit()
return True
except:
log.error("Errow thrown %s", sys.exc_info()[1])
return False