forked from openlp/openlp
Started moving Bible formats to a generic system.
This commit is contained in:
parent
6c7939bd71
commit
34ab7ea901
@ -85,7 +85,9 @@ class BibleCommon(object):
|
|||||||
"""
|
"""
|
||||||
An empty constructor... not sure why I'm here.
|
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):
|
def _get_web_text(self, urlstring, proxyurl):
|
||||||
"""
|
"""
|
||||||
|
@ -26,23 +26,24 @@
|
|||||||
import logging
|
import logging
|
||||||
import chardet
|
import chardet
|
||||||
|
|
||||||
from openlp.plugins.bibles.lib.common import BibleCommon
|
from openlp.plugins.bibles.lib.db import BibleDB
|
||||||
from openlp.core.lib import Receiver
|
from openlp.core.lib import Receiver
|
||||||
|
|
||||||
class BibleCSVImpl(BibleCommon):
|
log = logging.getLogger(__name__)
|
||||||
global log
|
|
||||||
log = logging.getLogger(u'BibleCSVImpl')
|
class CSVBible(BibleDB):
|
||||||
log.info(u'BibleCVSImpl loaded')
|
"""
|
||||||
def __init__(self, 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
|
Loads a Bible from a pair of CVS files passed in
|
||||||
This class assumes the files contain all the information and
|
This class assumes the files contain all the information and
|
||||||
a clean bible is being loaded.
|
a clean bible is being loaded.
|
||||||
"""
|
"""
|
||||||
self.bibledb = bibledb
|
log.info(__name__)
|
||||||
self.loadbible = True
|
BibleDB.__init__(self, **kwargs)
|
||||||
QtCore.QObject.connect(Receiver.get_receiver(),
|
|
||||||
QtCore.SIGNAL(u'openlpstopimport'), self.stop_import)
|
|
||||||
|
|
||||||
def stop_import(self):
|
def stop_import(self):
|
||||||
self.loadbible = False
|
self.loadbible = False
|
@ -27,17 +27,30 @@ import os
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from common import BibleCommon
|
from common import BibleCommon
|
||||||
from openlp.plugins.bibles.lib.models import *
|
from models import *
|
||||||
|
|
||||||
class BibleDBImpl(BibleCommon):
|
log = logging.getLogger(__name__)
|
||||||
global log
|
|
||||||
log = logging.getLogger(u'BibleDBImpl')
|
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')
|
log.info(u'BibleDBimpl loaded')
|
||||||
|
if u'biblepath' not in kwargs:
|
||||||
def __init__(self, biblepath, biblename, config):
|
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
|
# Connect to database
|
||||||
self.config = config
|
self.config = kwargs[u'config']
|
||||||
self.biblefile = os.path.join(biblepath, biblename + u'.sqlite')
|
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)
|
log.debug(u'Load bible %s on path %s', biblename, self.biblefile)
|
||||||
db_type = self.config.get_config(u'db type', u'sqlite')
|
db_type = self.config.get_config(u'db type', u'sqlite')
|
||||||
db_url = u''
|
db_url = u''
|
@ -171,7 +171,7 @@ class CWExtract(BibleCommon):
|
|||||||
bible[verse] = self._clean_text(verseText)
|
bible[verse] = self._clean_text(verseText)
|
||||||
return SearchResults(book_title, book_chapter, bible)
|
return SearchResults(book_title, book_chapter, bible)
|
||||||
|
|
||||||
class BibleHTTPImpl():
|
class HTTPBible(object):
|
||||||
global log
|
global log
|
||||||
log = logging.getLogger(u'BibleHTTPMgr')
|
log = logging.getLogger(u'BibleHTTPMgr')
|
||||||
log.info(u'BibleHTTP manager loaded')
|
log.info(u'BibleHTTP manager loaded')
|
@ -26,37 +26,59 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from bibleOpenSongimpl import BibleOpenSongImpl
|
from opensong import OpenSongBible
|
||||||
from bibleOSISimpl import BibleOSISImpl
|
from osis import OSISBible
|
||||||
from bibleCSVimpl import BibleCSVImpl
|
from csv import CSVBible
|
||||||
from bibleDBimpl import BibleDBImpl
|
from bibleDBimpl import BibleDBImpl
|
||||||
from bibleHTTPimpl import BibleHTTPImpl
|
from bibleHTTPimpl import BibleHTTPImpl
|
||||||
|
|
||||||
class BibleMode(object):
|
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
|
Full = 1
|
||||||
Partial = 2
|
Partial = 2
|
||||||
|
|
||||||
|
|
||||||
class BibleFormat(object):
|
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
|
Unknown = -1
|
||||||
OSIS = 0
|
OSIS = 0
|
||||||
CSV = 1
|
CSV = 1
|
||||||
OpenSong = 2
|
OpenSong = 2
|
||||||
WebDownload = 3
|
WebDownload = 3
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
def get_handler(class_, id):
|
def get_class(id):
|
||||||
if id == class_.OSIS:
|
"""
|
||||||
|
Return the appropriate imeplementation class.
|
||||||
|
"""
|
||||||
|
if id == BibleFormat.OSIS:
|
||||||
return BibleOSISImpl
|
return BibleOSISImpl
|
||||||
elif id == class_.CSV:
|
elif id == BibleFormat.CSV:
|
||||||
return BibleCSVImpl
|
return BibleCSVImpl
|
||||||
elif id == class_.OpenSong:
|
elif id == BibleFormat.OpenSong:
|
||||||
return BibleOpenSongImpl
|
return BibleOpenSongImpl
|
||||||
elif id == class_.WebDownload:
|
elif id == BibleFormat.WebDownload:
|
||||||
return BibleHTTPImpl
|
return BibleHTTPImpl
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def list():
|
||||||
|
return [
|
||||||
|
BibleFormat.OSIS,
|
||||||
|
BibleFormat.CSV,
|
||||||
|
BibleFormat.OpenSong,
|
||||||
|
BibleFormat.WebDownload
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class BibleManager(object):
|
class BibleManager(object):
|
||||||
"""
|
"""
|
||||||
@ -84,11 +106,11 @@ class BibleManager(object):
|
|||||||
self.bible_db_cache = None
|
self.bible_db_cache = None
|
||||||
# dict of bible http readers
|
# dict of bible http readers
|
||||||
self.bible_http_cache = None
|
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
|
#get proxy name for screen
|
||||||
self.proxyname = self.config.get_config(u'proxy name')
|
self.proxy_name = self.config.get_config(u'proxy name')
|
||||||
self.bibleSuffix = u'sqlite'
|
self.bible_suffix = u'sqlite'
|
||||||
self.dialogobject = None
|
self.import_wizard = None
|
||||||
self.reload_bibles()
|
self.reload_bibles()
|
||||||
self.media = None
|
self.media = None
|
||||||
|
|
||||||
@ -108,7 +130,7 @@ class BibleManager(object):
|
|||||||
for f in files:
|
for f in files:
|
||||||
nme = f.split(u'.')
|
nme = f.split(u'.')
|
||||||
bname = nme[0]
|
bname = nme[0]
|
||||||
self.bible_db_cache[bname] = BibleDBImpl(self.biblePath,
|
self.bible_db_cache[bname] = BibleDBImpl(self.bible_path,
|
||||||
bname, self.config)
|
bname, self.config)
|
||||||
# look to see if lazy load bible exists and get create getter.
|
# look to see if lazy load bible exists and get create getter.
|
||||||
biblesource = self.bible_db_cache[bname].get_meta(u'WEB')
|
biblesource = self.bible_db_cache[bname].get_meta(u'WEB')
|
||||||
@ -139,7 +161,7 @@ class BibleManager(object):
|
|||||||
self.book_abbreviations = {}
|
self.book_abbreviations = {}
|
||||||
filepath = os.path.split(os.path.abspath(__file__))[0]
|
filepath = os.path.split(os.path.abspath(__file__))[0]
|
||||||
filepath = os.path.abspath(os.path.join(
|
filepath = os.path.abspath(os.path.join(
|
||||||
filepath, u'..', u'resources',u'httpbooks.csv'))
|
filepath, u'..', u'resources', u'httpbooks.csv'))
|
||||||
fbibles = None
|
fbibles = None
|
||||||
try:
|
try:
|
||||||
fbibles = open(filepath, u'r')
|
fbibles = open(filepath, u'r')
|
||||||
@ -155,14 +177,14 @@ class BibleManager(object):
|
|||||||
fbibles.close()
|
fbibles.close()
|
||||||
log.debug(u'Bible Initialised')
|
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.
|
Sets the reference to the dialog with the progress bar on it.
|
||||||
|
|
||||||
``dialogobject``
|
``dialog``
|
||||||
The reference to the dialog.
|
The reference to the import wizard.
|
||||||
"""
|
"""
|
||||||
self.dialogobject = dialogobject
|
self.import_wizard = wizard
|
||||||
|
|
||||||
def import_bible(self, type, **kwargs):
|
def import_bible(self, type, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -171,7 +193,11 @@ class BibleManager(object):
|
|||||||
``type``
|
``type``
|
||||||
What type of Bible,
|
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,
|
def register_http_bible(self, biblename, biblesource, bibleid,
|
||||||
proxyurl=None, proxyid=None, proxypass=None):
|
proxyurl=None, proxyid=None, proxypass=None):
|
||||||
@ -202,7 +228,7 @@ class BibleManager(object):
|
|||||||
biblename, biblesource, bibleid, proxyurl, proxyid, proxypass)
|
biblename, biblesource, bibleid, proxyurl, proxyid, proxypass)
|
||||||
if self._is_new_bible(biblename):
|
if self._is_new_bible(biblename):
|
||||||
# Create new Bible
|
# Create new Bible
|
||||||
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
|
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
|
||||||
# Create Database
|
# Create Database
|
||||||
nbible.create_tables()
|
nbible.create_tables()
|
||||||
self.bible_db_cache[biblename] = nbible
|
self.bible_db_cache[biblename] = nbible
|
||||||
@ -239,7 +265,7 @@ class BibleManager(object):
|
|||||||
biblename, booksfile, versefile)
|
biblename, booksfile, versefile)
|
||||||
if self._is_new_bible(biblename):
|
if self._is_new_bible(biblename):
|
||||||
# Create new Bible
|
# Create new Bible
|
||||||
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
|
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
|
||||||
# Create database
|
# Create database
|
||||||
nbible.create_tables()
|
nbible.create_tables()
|
||||||
# Cache the database for use later
|
# Cache the database for use later
|
||||||
@ -261,13 +287,13 @@ class BibleManager(object):
|
|||||||
log.debug(u'register_OSIS_file_bible %s, %s', biblename, osisfile)
|
log.debug(u'register_OSIS_file_bible %s, %s', biblename, osisfile)
|
||||||
if self._is_new_bible(biblename):
|
if self._is_new_bible(biblename):
|
||||||
# Create new Bible
|
# Create new Bible
|
||||||
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
|
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
|
||||||
# Create Database
|
# Create Database
|
||||||
nbible.create_tables()
|
nbible.create_tables()
|
||||||
# Cache the database for use later
|
# Cache the database for use later
|
||||||
self.bible_db_cache[biblename] = nbible
|
self.bible_db_cache[biblename] = nbible
|
||||||
# Create the loader and pass in the database
|
# 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)
|
return bosis.load_data(osisfile, self.dialogobject)
|
||||||
else:
|
else:
|
||||||
log.debug(
|
log.debug(
|
||||||
@ -283,13 +309,13 @@ class BibleManager(object):
|
|||||||
log.debug(u'register_opensong_file_bible %s, %s', biblename, opensongfile)
|
log.debug(u'register_opensong_file_bible %s, %s', biblename, opensongfile)
|
||||||
if self._is_new_bible(biblename):
|
if self._is_new_bible(biblename):
|
||||||
# Create new Bible
|
# Create new Bible
|
||||||
nbible = BibleDBImpl(self.biblePath, biblename, self.config)
|
nbible = BibleDBImpl(self.bible_path, biblename, self.config)
|
||||||
# Create Database
|
# Create Database
|
||||||
nbible.create_tables()
|
nbible.create_tables()
|
||||||
# Cache the database for use later
|
# Cache the database for use later
|
||||||
self.bible_db_cache[biblename] = nbible
|
self.bible_db_cache[biblename] = nbible
|
||||||
# Create the loader and pass in the database
|
# 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)
|
bcsv.load_data(opensongfile, self.dialogobject)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
@ -34,7 +34,7 @@ from PyQt4 import QtCore
|
|||||||
|
|
||||||
from openlp.core.lib import Receiver
|
from openlp.core.lib import Receiver
|
||||||
|
|
||||||
class BibleOpenSongImpl():
|
class OpenSongBible(object):
|
||||||
"""
|
"""
|
||||||
OSIS Bible format importer class.
|
OSIS Bible format importer class.
|
||||||
"""
|
"""
|
||||||
@ -81,10 +81,10 @@ class BibleOpenSongImpl():
|
|||||||
detect_file = None
|
detect_file = None
|
||||||
try:
|
try:
|
||||||
detect_file = open(bible_file, u'r')
|
detect_file = open(bible_file, u'r')
|
||||||
details = chardet.detect(detect_file.read(2048))
|
details = chardet.detect(detect_file.read(3000))
|
||||||
except:
|
except:
|
||||||
log.exception(u'Failed to detect OpenSong file encoding')
|
log.exception(u'Failed to detect OpenSong file encoding')
|
||||||
return
|
return False
|
||||||
finally:
|
finally:
|
||||||
if detect_file:
|
if detect_file:
|
||||||
detect_file.close()
|
detect_file.close()
|
@ -34,7 +34,7 @@ from PyQt4 import QtCore
|
|||||||
|
|
||||||
from openlp.core.lib import Receiver
|
from openlp.core.lib import Receiver
|
||||||
|
|
||||||
class BibleOSISImpl():
|
class OSISBible(object):
|
||||||
"""
|
"""
|
||||||
OSIS Bible format importer class.
|
OSIS Bible format importer class.
|
||||||
"""
|
"""
|
Loading…
Reference in New Issue
Block a user