Started moving Bible formats to a generic system.

This commit is contained in:
Raoul Snyman 2009-12-28 01:54:00 +02:00
parent 6c7939bd71
commit 34ab7ea901
7 changed files with 92 additions and 50 deletions

View File

@ -85,7 +85,9 @@ class BibleCommon(object):
"""
An empty constructor... not sure why I'm here.
"""
pass
self.loadbible = True
QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlpstopimport'), self.stop_import)
def _get_web_text(self, urlstring, proxyurl):
"""

View File

@ -26,23 +26,24 @@
import logging
import chardet
from openlp.plugins.bibles.lib.common import BibleCommon
from openlp.plugins.bibles.lib.db import BibleDB
from openlp.core.lib import Receiver
class BibleCSVImpl(BibleCommon):
global log
log = logging.getLogger(u'BibleCSVImpl')
log.info(u'BibleCVSImpl loaded')
def __init__(self, bibledb):
log = logging.getLogger(__name__)
class CSVBible(BibleDB):
"""
This class provides a specialisation for importing of CSV Bibles.
"""
def __init__(self, **kwargs):
"""
Loads a Bible from a pair of CVS files passed in
This class assumes the files contain all the information and
a clean bible is being loaded.
"""
self.bibledb = bibledb
self.loadbible = True
QtCore.QObject.connect(Receiver.get_receiver(),
QtCore.SIGNAL(u'openlpstopimport'), self.stop_import)
log.info(__name__)
BibleDB.__init__(self, **kwargs)
def stop_import(self):
self.loadbible = False

View File

@ -27,17 +27,30 @@ import os
import logging
from common import BibleCommon
from openlp.plugins.bibles.lib.models import *
from models import *
class BibleDBImpl(BibleCommon):
global log
log = logging.getLogger(u'BibleDBImpl')
log.info(u'BibleDBimpl loaded')
log = logging.getLogger(__name__)
def __init__(self, biblepath, biblename, config):
class BibleDB(BibleCommon):
"""
This class represents a database-bound Bible. It is used as a base class
for all the custom importers, so that the can implement their own import
methods, but benefit from the database methods in here via inheritance,
rather than depending on yet another object.
"""
def __init__(self, **kwargs):
log.info(u'BibleDBimpl loaded')
if u'biblepath' not in kwargs:
raise KeyError(u'Missing keyword argument "path".')
if u'biblename' not in kwargs:
raise KeyError(u'Missing keyword argument "name".')
if u'config' not in kwargs:
raise KeyError(u'Missing keyword argument "config".')
# Connect to database
self.config = config
self.biblefile = os.path.join(biblepath, biblename + u'.sqlite')
self.config = kwargs[u'config']
self.db_file = os.path.join(kwargs[u'path'],
u'%s.sqlite' % kwargs[u'biblename'])
log.debug(u'Load bible %s on path %s', biblename, self.biblefile)
db_type = self.config.get_config(u'db type', u'sqlite')
db_url = u''

View File

@ -171,7 +171,7 @@ class CWExtract(BibleCommon):
bible[verse] = self._clean_text(verseText)
return SearchResults(book_title, book_chapter, bible)
class BibleHTTPImpl():
class HTTPBible(object):
global log
log = logging.getLogger(u'BibleHTTPMgr')
log.info(u'BibleHTTP manager loaded')

View File

@ -26,37 +26,59 @@
import logging
import os
from bibleOpenSongimpl import BibleOpenSongImpl
from bibleOSISimpl import BibleOSISImpl
from bibleCSVimpl import BibleCSVImpl
from opensong import OpenSongBible
from osis import OSISBible
from csv import CSVBible
from bibleDBimpl import BibleDBImpl
from bibleHTTPimpl import BibleHTTPImpl
class BibleMode(object):
"""
This is basically an enumeration class which specifies the mode of a Bible.
Mode refers to whether or not a Bible in OpenLP is a full Bible or needs to
be downloaded from the Internet on an as-needed basis.
"""
Full = 1
Partial = 2
class BibleFormat(object):
"""
This is a special enumeration class that holds the various types of Bibles,
plus a few helper functions to facilitate generic handling of Bible types
for importing.
"""
Unknown = -1
OSIS = 0
CSV = 1
OpenSong = 2
WebDownload = 3
@classmethod
def get_handler(class_, id):
if id == class_.OSIS:
@staticmethod
def get_class(id):
"""
Return the appropriate imeplementation class.
"""
if id == BibleFormat.OSIS:
return BibleOSISImpl
elif id == class_.CSV:
elif id == BibleFormat.CSV:
return BibleCSVImpl
elif id == class_.OpenSong:
elif id == BibleFormat.OpenSong:
return BibleOpenSongImpl
elif id == class_.WebDownload:
elif id == BibleFormat.WebDownload:
return BibleHTTPImpl
else:
return None
@staticmethod
def list():
return [
BibleFormat.OSIS,
BibleFormat.CSV,
BibleFormat.OpenSong,
BibleFormat.WebDownload
]
class BibleManager(object):
"""
@ -84,11 +106,11 @@ class BibleManager(object):
self.bible_db_cache = None
# dict of bible http readers
self.bible_http_cache = None
self.biblePath = self.config.get_data_path()
self.bible_path = self.config.get_data_path()
#get proxy name for screen
self.proxyname = self.config.get_config(u'proxy name')
self.bibleSuffix = u'sqlite'
self.dialogobject = None
self.proxy_name = self.config.get_config(u'proxy name')
self.bible_suffix = u'sqlite'
self.import_wizard = None
self.reload_bibles()
self.media = None
@ -108,7 +130,7 @@ class BibleManager(object):
for f in files:
nme = f.split(u'.')
bname = nme[0]
self.bible_db_cache[bname] = BibleDBImpl(self.biblePath,
self.bible_db_cache[bname] = BibleDBImpl(self.bible_path,
bname, self.config)
# look to see if lazy load bible exists and get create getter.
biblesource = self.bible_db_cache[bname].get_meta(u'WEB')
@ -139,7 +161,7 @@ class BibleManager(object):
self.book_abbreviations = {}
filepath = os.path.split(os.path.abspath(__file__))[0]
filepath = os.path.abspath(os.path.join(
filepath, u'..', u'resources',u'httpbooks.csv'))
filepath, u'..', u'resources', u'httpbooks.csv'))
fbibles = None
try:
fbibles = open(filepath, u'r')
@ -155,14 +177,14 @@ class BibleManager(object):
fbibles.close()
log.debug(u'Bible Initialised')
def set_process_dialog(self, dialogobject):
def set_process_dialog(self, wizard):
"""
Sets the reference to the dialog with the progress bar on it.
``dialogobject``
The reference to the dialog.
``dialog``
The reference to the import wizard.
"""
self.dialogobject = dialogobject
self.import_wizard = wizard
def import_bible(self, type, **kwargs):
"""
@ -171,7 +193,11 @@ class BibleManager(object):
``type``
What type of Bible,
"""
pass
impl_class = BibleFormat.get_handler(type)
bible_impl = impl_class(**kwargs)
bible_name = bible_impl.register()
BibleDBImpl(self.biblePath, bible_name, self.config)
bible_impl.do_import()
def register_http_bible(self, biblename, biblesource, bibleid,
proxyurl=None, proxyid=None, proxypass=None):
@ -202,7 +228,7 @@ class BibleManager(object):
biblename, biblesource, bibleid, proxyurl, proxyid, proxypass)
if self._is_new_bible(biblename):
# Create new Bible
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
# Create Database
nbible.create_tables()
self.bible_db_cache[biblename] = nbible
@ -239,7 +265,7 @@ class BibleManager(object):
biblename, booksfile, versefile)
if self._is_new_bible(biblename):
# Create new Bible
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
# Create database
nbible.create_tables()
# Cache the database for use later
@ -261,13 +287,13 @@ class BibleManager(object):
log.debug(u'register_OSIS_file_bible %s, %s', biblename, osisfile)
if self._is_new_bible(biblename):
# Create new Bible
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
# Create Database
nbible.create_tables()
# Cache the database for use later
self.bible_db_cache[biblename] = nbible
# Create the loader and pass in the database
bosis = BibleOSISImpl(self.biblePath, nbible)
bosis = BibleOSISImpl(self.bible_path, nbible)
return bosis.load_data(osisfile, self.dialogobject)
else:
log.debug(
@ -283,13 +309,13 @@ class BibleManager(object):
log.debug(u'register_opensong_file_bible %s, %s', biblename, opensongfile)
if self._is_new_bible(biblename):
# Create new Bible
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
# Create Database
nbible.create_tables()
# Cache the database for use later
self.bible_db_cache[biblename] = nbible
# Create the loader and pass in the database
bcsv = BibleOpenSongImpl(self.biblePath, nbible)
bcsv = BibleOpenSongImpl(self.bible_path, nbible)
bcsv.load_data(opensongfile, self.dialogobject)
return True
else:

View File

@ -34,7 +34,7 @@ from PyQt4 import QtCore
from openlp.core.lib import Receiver
class BibleOpenSongImpl():
class OpenSongBible(object):
"""
OSIS Bible format importer class.
"""
@ -81,10 +81,10 @@ class BibleOpenSongImpl():
detect_file = None
try:
detect_file = open(bible_file, u'r')
details = chardet.detect(detect_file.read(2048))
details = chardet.detect(detect_file.read(3000))
except:
log.exception(u'Failed to detect OpenSong file encoding')
return
return False
finally:
if detect_file:
detect_file.close()

View File

@ -34,7 +34,7 @@ from PyQt4 import QtCore
from openlp.core.lib import Receiver
class BibleOSISImpl():
class OSISBible(object):
"""
OSIS Bible format importer class.
"""