2009-09-08 19:58:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-01-01 16:33:41 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2009-09-08 19:58:05 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2019-02-14 15:09:09 +00:00
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2009-07-13 20:08:43 +00:00
|
|
|
|
2014-08-24 14:40:45 +00:00
|
|
|
from lxml import etree
|
2009-07-13 20:08:43 +00:00
|
|
|
|
2016-08-07 10:15:43 +00:00
|
|
|
from openlp.plugins.bibles.lib.bibleimport import BibleImport
|
2008-11-19 18:13:22 +00:00
|
|
|
|
2018-10-02 04:39:42 +00:00
|
|
|
|
2016-08-08 20:02:18 +00:00
|
|
|
NS = {'ns': 'http://www.bibletechnologies.net/2003/OSIS/namespace'}
|
2016-08-07 10:15:43 +00:00
|
|
|
# Tags we don't use and can remove the content
|
2016-08-10 19:08:09 +00:00
|
|
|
REMOVABLE_ELEMENTS = (
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}note',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}milestone',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}title',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}abbr',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}catchWord',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}index',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}rdg',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}rdgGroup',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}figure'
|
|
|
|
)
|
2016-08-07 10:15:43 +00:00
|
|
|
# Tags we don't use but need to keep the content
|
2016-08-10 19:08:09 +00:00
|
|
|
REMOVABLE_TAGS = (
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}p',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}l',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}lg',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}q',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}a',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}w',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}divineName',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}foreign',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}hi',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}inscription',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}mentioned',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}name',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}reference',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}seg',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}transChange',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}salute',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}signed',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}closer',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}speech',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}speaker',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}list',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}item',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}table',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}head',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}row',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}cell',
|
|
|
|
'{http://www.bibletechnologies.net/2003/OSIS/namespace}caption'
|
|
|
|
)
|
2013-04-18 17:45:14 +00:00
|
|
|
|
2016-08-28 21:03:04 +00:00
|
|
|
# Precompile a few xpath-querys
|
2016-09-02 14:23:20 +00:00
|
|
|
verse_in_chapter = etree.XPath('//ns:chapter[1]/ns:verse', namespaces=NS)
|
|
|
|
text_in_verse = etree.XPath('//ns:verse[1]/text()', namespaces=NS)
|
2016-08-28 21:03:04 +00:00
|
|
|
|
|
|
|
|
2016-08-07 10:15:43 +00:00
|
|
|
class OSISBible(BibleImport):
|
2009-07-13 20:08:43 +00:00
|
|
|
"""
|
2011-02-24 14:43:24 +00:00
|
|
|
`OSIS <http://www.bibletechnologies.net/>`_ Bible format importer class.
|
2009-07-13 20:08:43 +00:00
|
|
|
"""
|
2016-08-28 21:03:04 +00:00
|
|
|
def process_books(self, bible_data):
|
|
|
|
"""
|
2016-09-02 14:23:20 +00:00
|
|
|
Extract and create the bible books from the parsed xml
|
2016-08-28 21:03:04 +00:00
|
|
|
|
2016-09-02 14:23:20 +00:00
|
|
|
:param bible_data: parsed xml
|
|
|
|
:return: None
|
2016-08-28 21:03:04 +00:00
|
|
|
"""
|
|
|
|
# Find books in the bible
|
|
|
|
bible_books = bible_data.xpath("//ns:div[@type='book']", namespaces=NS)
|
2016-09-02 14:23:20 +00:00
|
|
|
no_of_books = len(bible_books)
|
2016-08-28 21:03:04 +00:00
|
|
|
for book in bible_books:
|
|
|
|
if self.stop_import_flag:
|
|
|
|
break
|
|
|
|
# Remove div-tags in the book
|
|
|
|
etree.strip_tags(book, '{http://www.bibletechnologies.net/2003/OSIS/namespace}div')
|
|
|
|
db_book = self.find_and_create_book(book.get('osisID'), no_of_books, self.language_id)
|
2016-09-02 14:23:20 +00:00
|
|
|
self.process_chapters(db_book, book)
|
2016-08-28 21:03:04 +00:00
|
|
|
self.session.commit()
|
|
|
|
|
2016-09-02 14:23:20 +00:00
|
|
|
def process_chapters(self, book, chapters):
|
2016-08-28 21:03:04 +00:00
|
|
|
"""
|
2016-09-02 14:23:20 +00:00
|
|
|
Extract the chapters, and do some initial processing of the verses
|
2016-08-28 21:03:04 +00:00
|
|
|
|
2016-09-02 14:23:20 +00:00
|
|
|
:param book: An OpenLP bible database book object
|
|
|
|
:param chapters: parsed chapters
|
|
|
|
:return: None
|
2016-08-28 21:03:04 +00:00
|
|
|
"""
|
|
|
|
# Find out if chapter-tags contains the verses, or if it is used as milestone/anchor
|
2016-09-02 14:23:20 +00:00
|
|
|
if verse_in_chapter(chapters):
|
2016-08-28 21:03:04 +00:00
|
|
|
# The chapter tags contains the verses
|
|
|
|
for chapter in chapters:
|
2016-09-02 14:23:20 +00:00
|
|
|
chapter_number = int(chapter.get("osisID").split('.')[1])
|
|
|
|
self.set_current_chapter(book.name, chapter_number)
|
2016-08-28 21:03:04 +00:00
|
|
|
# Find out if verse-tags contains the text, or if it is used as milestone/anchor
|
2016-09-02 14:23:20 +00:00
|
|
|
if not text_in_verse(chapter):
|
2016-08-28 21:03:04 +00:00
|
|
|
# verse-tags are used as milestone
|
|
|
|
for verse in chapter:
|
|
|
|
# If this tag marks the start of a verse, the verse text is between this tag and
|
|
|
|
# the next tag, which the "tail" attribute gives us.
|
2016-09-02 14:23:20 +00:00
|
|
|
self.process_verse(book, chapter_number, verse, use_milestones=True)
|
2016-08-28 21:03:04 +00:00
|
|
|
else:
|
|
|
|
# Verse-tags contains the text
|
|
|
|
for verse in chapter:
|
2016-09-02 14:23:20 +00:00
|
|
|
self.process_verse(book, chapter_number, verse)
|
2016-08-28 21:03:04 +00:00
|
|
|
else:
|
|
|
|
# The chapter tags is used as milestones. For now we assume verses is also milestones
|
|
|
|
chapter_number = 0
|
|
|
|
for element in chapters:
|
|
|
|
if element.tag == '{http://www.bibletechnologies.net/2003/OSIS/namespace}chapter' \
|
|
|
|
and element.get('sID'):
|
2016-09-02 14:23:20 +00:00
|
|
|
chapter_number = int(element.get("osisID").split('.')[1])
|
|
|
|
self.set_current_chapter(book.name, chapter_number)
|
|
|
|
elif element.tag == '{http://www.bibletechnologies.net/2003/OSIS/namespace}verse':
|
2016-08-28 21:03:04 +00:00
|
|
|
# If this tag marks the start of a verse, the verse text is between this tag and
|
|
|
|
# the next tag, which the "tail" attribute gives us.
|
2016-09-02 14:23:20 +00:00
|
|
|
self.process_verse(book, chapter_number, element, use_milestones=True)
|
|
|
|
|
|
|
|
def process_verse(self, book, chapter_number, element, use_milestones=False):
|
|
|
|
"""
|
|
|
|
Process a verse element
|
|
|
|
:param book: A database Book object
|
|
|
|
:param chapter_number: The chapter number to add the verses to (int)
|
|
|
|
:param element: The verse element to process. (etree element type)
|
|
|
|
:param use_milestones: set to True to process a 'milestone' verse. Defaults to False
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
osis_id = element.get("osisID")
|
|
|
|
if not osis_id:
|
|
|
|
return None
|
|
|
|
verse_number = int(osis_id.split('.')[2])
|
|
|
|
verse_text = ''
|
|
|
|
if use_milestones and element.get('sID'):
|
|
|
|
verse_text = element.tail
|
|
|
|
elif not use_milestones:
|
|
|
|
verse_text = element.text
|
|
|
|
if verse_text:
|
|
|
|
self.create_verse(book.id, chapter_number, verse_number, verse_text.strip())
|
2016-08-28 21:03:04 +00:00
|
|
|
|
2011-05-26 19:13:11 +00:00
|
|
|
def do_import(self, bible_name=None):
|
2009-07-13 20:08:43 +00:00
|
|
|
"""
|
|
|
|
Loads a Bible from file.
|
|
|
|
"""
|
2017-10-10 19:09:20 +00:00
|
|
|
self.log_debug('Starting OSIS import from "{name}"'.format(name=self.file_path))
|
|
|
|
self.validate_xml_file(self.file_path, '{http://www.bibletechnologies.net/2003/osis/namespace}osis')
|
|
|
|
bible = self.parse_xml(self.file_path, elements=REMOVABLE_ELEMENTS, tags=REMOVABLE_TAGS)
|
2016-08-28 21:03:04 +00:00
|
|
|
if bible is None:
|
2009-12-27 22:19:39 +00:00
|
|
|
return False
|
2016-08-28 21:03:04 +00:00
|
|
|
# Find bible language
|
|
|
|
language = bible.xpath("//ns:osisText/@xml:lang", namespaces=NS)
|
2017-10-10 19:09:20 +00:00
|
|
|
self.language_id = self.get_language_id(language[0] if language else None, bible_name=str(self.file_path))
|
2016-08-28 21:03:04 +00:00
|
|
|
if not self.language_id:
|
|
|
|
return False
|
|
|
|
self.process_books(bible)
|
2016-08-29 16:11:09 +00:00
|
|
|
return True
|