forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenSongXMLBible.py
executable file
·624 lines (547 loc) · 32 KB
/
OpenSongXMLBible.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# OpenSongXMLBible.py
#
# Module handling OpenSong XML Bibles
#
# Copyright (C) 2013-2019 Robert Hunt
# Author: Robert Hunt <Freely.Given.org@gmail.com>
# License: See gpl-3.0.txt
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
"""
Module reading and loading OpenSong XML Bibles.
Details of the XML file format can be found at
http://www.opensong.org/home/file-formats
As of Jan 2018 it said:
Bible File Format Specification
As of OpenSong v1.6.2
Copyright 2003-2010 Sean Lickfold.
Released under the terms of the GPL.
File Structure
OpenSong Scripture files are standard XML files with the following layout:
<?xml version="1.0" encoding="[W3C encoding code]"?>
<bible>
<b n="[book name]">
<c n="#">
<v n="#"></v>
</c>
</b>
</bible>
Notes
Encoding
Any W3C encoding can be used. Generally western languages use ISO-8859-1. The best option, however, is to use UTF-8 encoding, to preserve any special characters.
Book Name
Book names are the full-length name (e.g. “Genesis”) They can be in the language that the Scripture file is saved in (e.g. in German Genesis would be “1. Mose”). Whatever the book names are is how they will be displayed in the index of the Scripture Lookup dialog.
Chapter and Verse Numbers
The ”#” sign refers to the chapter and verse number of a given book chapter and verse.
Sample:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bible>
<b n="Genesis">
<c n="1">
<v n="1">In the beginning God created the heaven and the earth.</v>
<v n="2">And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.</v>
"""
from gettext import gettext as _
LastModifiedDate = '2019-02-04' # by RJH
ShortProgName = "OpenSongBible"
ProgName = "OpenSong XML Bible format handler"
ProgVersion = '0.39'
ProgNameVersion = '{} v{}'.format( ShortProgName, ProgVersion )
ProgNameVersionDate = '{} {} {}'.format( ProgNameVersion, _("last modified"), LastModifiedDate )
debuggingThisModule = False
import logging, os, zipfile
from xml.etree.ElementTree import ElementTree
import BibleOrgSysGlobals
from InternalBibleInternals import BOS_ADDED_NESTING_MARKERS
from BibleOrganisationalSystems import BibleOrganisationalSystem
from BibleBooksNames import BibleBooksNamesSystems
from Bible import Bible, BibleBook
from USFM3Markers import OFTEN_IGNORED_USFM_HEADER_MARKERS, USFM_ALL_INTRODUCTION_MARKERS, \
USFM_PRECHAPTER_MARKERS, USFM_BIBLE_PARAGRAPH_MARKERS
from MLWriter import MLWriter
filenameEndingsToIgnore = ('.ZIP.GO', '.ZIP.DATA',) # Must be UPPERCASE
extensionsToIgnore = ( 'ASC', 'BAK', 'BAK2', 'BAK3', 'BAK4', 'BBLX', 'BC', 'CCT', 'CSS', 'DOC', 'DTS', 'HTM','HTML',
'JAR', 'LDS', 'LOG', 'MYBIBLE', 'NT','NTX', 'ODT', 'ONT','ONTX', 'OSIS', 'OT','OTX', 'PDB',
'SAV', 'SAVE', 'STY', 'SSF', 'TXT', 'USFM', 'USX', 'VRS', 'YET', 'XML', 'ZIP', ) # Must be UPPERCASE and NOT begin with a dot
BibleBooksNames = None
def OpenSongXMLBibleFileCheck( givenFolderName, strictCheck=True, autoLoad=False, autoLoadBooks=False ):
"""
Given a folder, search for OpenSong XML Bible files or folders in the folder and in the next level down.
Returns False if an error is found.
if autoLoad is false (default)
returns None, or the number found.
if autoLoad is true and exactly one OpenSong Bible is found,
returns the loaded OpenSongXMLBible object.
"""
if BibleOrgSysGlobals.verbosityLevel > 2: print( "OpenSongXMLBibleFileCheck( {}, {}, {}, {} )".format( givenFolderName, strictCheck, autoLoad, autoLoadBooks ) )
if BibleOrgSysGlobals.debugFlag: assert givenFolderName and isinstance( givenFolderName, str )
if BibleOrgSysGlobals.debugFlag: assert autoLoad in (True,False,)
if BibleOrgSysGlobals.debugFlag: assert autoLoadBooks in (True,False,)
# Check that the given folder is readable
if not os.access( givenFolderName, os.R_OK ):
logging.critical( _("OpenSongXMLBibleFileCheck: Given {!r} folder is unreadable").format( givenFolderName ) )
return False
if not os.path.isdir( givenFolderName ):
logging.critical( _("OpenSongXMLBibleFileCheck: Given {!r} path is not a folder").format( givenFolderName ) )
return False
# Find all the files and folders in this folder
if BibleOrgSysGlobals.verbosityLevel > 3: print( " OpenSongXMLBibleFileCheck: Looking for files in given {}".format( givenFolderName ) )
foundFolders, foundFiles = [], []
for something in os.listdir( givenFolderName ):
somepath = os.path.join( givenFolderName, something )
if os.path.isdir( somepath ):
if something in BibleOrgSysGlobals.COMMONLY_IGNORED_FOLDERS:
continue # don't visit these directories
foundFolders.append( something )
elif os.path.isfile( somepath ):
somethingUpper = something.upper()
somethingUpperProper, somethingUpperExt = os.path.splitext( somethingUpper )
ignore = False
for ending in filenameEndingsToIgnore:
if somethingUpper.endswith( ending): ignore=True; break
if ignore: continue
if not somethingUpperExt[1:] in extensionsToIgnore: # Compare without the first dot
foundFiles.append( something )
#print( 'osx1', foundFiles )
# See if there's an OpenSong project here in this folder
numFound = 0
looksHopeful = False
lastFilenameFound = None
for thisFilename in sorted( foundFiles ):
if strictCheck or BibleOrgSysGlobals.strictCheckingFlag:
firstLines = BibleOrgSysGlobals.peekIntoFile( thisFilename, givenFolderName, numLines=2 )
#print( 'osx1b', firstLines )
if not firstLines or len(firstLines)<2: continue
if not ( firstLines[0].startswith( '<?xml version="1.0"' ) or firstLines[0].startswith( "<?xml version='1.0'" ) ) \
and not ( firstLines[0].startswith( '\ufeff<?xml version="1.0"' ) or firstLines[0].startswith( "\ufeff<?xml version='1.0'" ) ): # same but with BOM
if BibleOrgSysGlobals.verbosityLevel > 3: print( "OSB (unexpected) first line was {!r} in {}".format( firstLines, thisFilename ) )
continue
if not firstLines[1].startswith( '<bible>' ):
continue
lastFilenameFound = thisFilename
numFound += 1
if numFound:
if BibleOrgSysGlobals.verbosityLevel > 2: print( "OpenSongXMLBibleFileCheck got", numFound, givenFolderName, lastFilenameFound )
if numFound == 1 and (autoLoad or autoLoadBooks):
osb = OpenSongXMLBible( givenFolderName, lastFilenameFound )
if autoLoadBooks: osb.load() # Load and process the file
return osb
return numFound
elif looksHopeful and BibleOrgSysGlobals.verbosityLevel > 2: print( " Looked hopeful but no actual files found" )
# Look one level down
numFound = 0
foundProjects = []
for thisFolderName in sorted( foundFolders ):
tryFolderName = os.path.join( givenFolderName, thisFolderName+'/' )
if BibleOrgSysGlobals.verbosityLevel > 3: print( " OpenSongXMLBibleFileCheck: Looking for files in {}".format( tryFolderName ) )
foundSubfolders, foundSubfiles = [], []
for something in os.listdir( tryFolderName ):
somepath = os.path.join( givenFolderName, thisFolderName, something )
if os.path.isdir( somepath ): foundSubfolders.append( something )
elif os.path.isfile( somepath ):
somethingUpper = something.upper()
somethingUpperProper, somethingUpperExt = os.path.splitext( somethingUpper )
ignore = False
for ending in filenameEndingsToIgnore:
if somethingUpper.endswith( ending): ignore=True; break
if ignore: continue
if not somethingUpperExt[1:] in extensionsToIgnore: # Compare without the first dot
foundSubfiles.append( something )
#print( 'osx2', foundSubfiles )
# See if there's an OS project here in this folder
for thisFilename in sorted( foundSubfiles ):
if strictCheck or BibleOrgSysGlobals.strictCheckingFlag:
firstLines = BibleOrgSysGlobals.peekIntoFile( thisFilename, tryFolderName, numLines=2 )
if not firstLines or len(firstLines)<2: continue
if not ( firstLines[0].startswith( '<?xml version="1.0"' ) or firstLines[0].startswith( "<?xml version='1.0'" ) ) \
and not ( firstLines[0].startswith( '\ufeff<?xml version="1.0"' ) or firstLines[0].startswith( "\ufeff<?xml version='1.0'" ) ): # same but with BOM
if BibleOrgSysGlobals.verbosityLevel > 3: print( "OSB (unexpected) first line was {!r} in {}".format( firstLines, thisFilename ) )
continue
if not firstLines[1].startswith( '<bible>' ):
continue
foundProjects.append( (tryFolderName, thisFilename,) )
lastFilenameFound = thisFilename
numFound += 1
if numFound:
if BibleOrgSysGlobals.verbosityLevel > 2: print( "OpenSongXMLBibleFileCheck foundProjects", numFound, foundProjects )
if numFound == 1 and (autoLoad or autoLoadBooks):
if BibleOrgSysGlobals.debugFlag: assert len(foundProjects) == 1
osb = OpenSongXMLBible( foundProjects[0][0], foundProjects[0][1] ) # Folder and filename
if autoLoadBooks: osb.load() # Load and process the file
return osb
return numFound
# end of OpenSongXMLBibleFileCheck
def createOpenSongXML( BibleObject, outputFolder=None, controlDict=None, validationSchema=None ):
"""
Using settings from the given control file,
converts the USFM information to a UTF-8 OpenSong XML file.
This format is roughly documented at http://de.wikipedia.org/wiki/OpenSong_XML
but more fields can be discovered by looking at downloaded files.
"""
if BibleOrgSysGlobals.verbosityLevel > 1: print( "Running createOpenSongXML…" )
if BibleOrgSysGlobals.debugFlag: assert BibleObject.books
ignoredMarkers, unhandledMarkers, unhandledBooks = set(), set(), []
def writeOpenSongBook( writerObject, BBB, bkData ):
"""Writes a book to the OpenSong XML writerObject."""
#print( 'BIBLEBOOK', [('bnumber',BibleOrgSysGlobals.BibleBooksCodes.getReferenceNumber(BBB)), ('bname',BibleOrgSysGlobals.BibleBooksCodes.getEnglishName_NR(BBB)), ('bsname',BibleOrgSysGlobals.BibleBooksCodes.getOSISAbbreviation(BBB))] )
OSISAbbrev = BibleOrgSysGlobals.BibleBooksCodes.getOSISAbbreviation( BBB )
if not OSISAbbrev:
logging.warning( "toOpenSong: Can't write {} OpenSong book because no OSIS code available".format( BBB ) )
unhandledBooks.append( BBB )
return
writerObject.writeLineOpen( 'b', ('n',bkData.getAssumedBookNames()[0]) )
haveOpenChapter, startedFlag, gotVP, accumulator = False, False, None, ""
C, V = '-1', '-1' # So first/id line starts at -1:0
for processedBibleEntry in bkData._processedLines: # Process internal Bible data lines
marker, text, extras = processedBibleEntry.getMarker(), processedBibleEntry.getCleanText(), processedBibleEntry.getExtras()
#print( marker, repr(text) )
#if text: assert text[0] != ' '
if '¬' in marker or marker in BOS_ADDED_NESTING_MARKERS: continue # Just ignore added markers -- not needed here
if marker in USFM_PRECHAPTER_MARKERS:
if debuggingThisModule or BibleOrgSysGlobals.debugFlag or BibleOrgSysGlobals.strictCheckingFlag:
assert C=='-1' or marker=='rem' or marker.startswith('mte')
V = str( int(V) + 1 )
if marker in OFTEN_IGNORED_USFM_HEADER_MARKERS or marker in ('ie',): # Just ignore these lines
ignoredMarkers.add( marker )
elif marker == 'c':
if accumulator:
writerObject.writeLineOpenClose ( 'v', accumulator, ('n',verseNumberString) )
accumulator = ''
if haveOpenChapter:
writerObject.writeLineClose ( 'c' )
C, V = text, '0'
writerObject.writeLineOpen ( 'c', ('n',text) )
haveOpenChapter = True
elif marker in ('c#',): # These are the markers that we can safely ignore for this export
ignoredMarkers.add( marker )
elif marker == 'vp#': # This precedes a v field and has the verse number to be printed
gotVP = text # Just remember it for now
elif marker == 'v':
V = text
if gotVP: # this is the verse number to be published
text = gotVP
gotVP = None
startedFlag = True
if accumulator:
writerObject.writeLineOpenClose ( 'v', accumulator, ('n',verseNumberString) )
accumulator = ''
#print( "Text {!r}".format( text ) )
if not text: logging.warning( "createOpenSongXML: Missing text for v" ); continue
verseNumberString = text.replace('<','').replace('>','').replace('"','') # Used below but remove anything that'll cause a big XML problem later
elif marker in ('mt1','mt2','mt3','mt4', 'mte1','mte2','mte3','mte4', 'ms1','ms2','ms3','ms4', ) \
or marker in USFM_ALL_INTRODUCTION_MARKERS \
or marker in ('s1','s2','s3','s4', 'r','sr','mr', 'd','sp','cd', 'cl','lit', ):
ignoredMarkers.add( marker )
elif marker in USFM_BIBLE_PARAGRAPH_MARKERS:
if BibleOrgSysGlobals.debugFlag: assert not text and not extras
ignoredMarkers.add( marker )
elif marker in ('b', 'nb', 'ib', ):
if BibleOrgSysGlobals.debugFlag: assert not text and not extras
ignoredMarkers.add( marker )
elif marker in ('v~', 'p~',):
if BibleOrgSysGlobals.debugFlag: assert text or extras
if not text: # this is an empty (untranslated) verse
text = '- - -' # but we'll put in a filler
if startedFlag: accumulator += (' ' if accumulator else '') + BibleOrgSysGlobals.makeSafeXML( text )
else:
if text:
logging.warning( "toOpenSong: lost text in {} field in {} {}:{} {!r}".format( marker, BBB, C, V, text ) )
#if BibleOrgSysGlobals.debugFlag: halt
if extras:
logging.warning( "toOpenSong: lost extras in {} field in {} {}:{}".format( marker, BBB, C, V ) )
#if BibleOrgSysGlobals.debugFlag: halt
unhandledMarkers.add( marker )
if extras and marker not in ('v~','p~',) and marker not in ignoredMarkers:
logging.critical( "toOpenSong: extras not handled for {} at {} {}:{}".format( marker, BBB, C, V ) )
if accumulator:
writerObject.writeLineOpenClose ( 'v', accumulator, ('n',verseNumberString) )
if haveOpenChapter:
writerObject.writeLineClose ( 'c' )
writerObject.writeLineClose( 'b' )
# end of createOpenSongXML.writeOpenSongBook
# Set-up our Bible reference system
if 'PublicationCode' not in controlDict or controlDict['PublicationCode'] == 'GENERIC':
BOS = BibleObject.genericBOS
BRL = BibleObject.genericBRL
else:
BOS = BibleOrganisationalSystem( controlDict['PublicationCode'] )
BRL = BibleReferenceList( BOS, BibleObject=None )
if BibleOrgSysGlobals.verbosityLevel > 2: print( _(" Exporting to OpenSong format…") )
try: osOFn = controlDict['OpenSongOutputFilename']
except KeyError: osOFn = 'Bible.osong'
filename = BibleOrgSysGlobals.makeSafeFilename( osOFn )
xw = MLWriter( filename, outputFolder )
xw.setHumanReadable()
xw.start()
xw.writeLineOpen( 'Bible' )
for BBB,bookData in BibleObject.books.items():
writeOpenSongBook( xw, BBB, bookData )
xw.writeLineClose( 'Bible' )
xw.close()
if ignoredMarkers:
logging.info( "createOpenSongXML: Ignored markers were {}".format( ignoredMarkers ) )
if BibleOrgSysGlobals.verbosityLevel > 2:
print( " " + _("WARNING: Ignored createOpenSongXML markers were {}").format( ignoredMarkers ) )
if unhandledMarkers:
logging.warning( "createOpenSongXML: Unhandled markers were {}".format( unhandledMarkers ) )
if BibleOrgSysGlobals.verbosityLevel > 1:
print( " " + _("WARNING: Unhandled toOpenSong markers were {}").format( unhandledMarkers ) )
if unhandledBooks:
logging.warning( "createOpenSongXML: Unhandled books were {}".format( unhandledBooks ) )
if BibleOrgSysGlobals.verbosityLevel > 1:
print( " " + _("WARNING: Unhandled createOpenSongXML books were {}").format( unhandledBooks ) )
# Now create a zipped version
filepath = os.path.join( outputFolder, filename )
if BibleOrgSysGlobals.verbosityLevel > 2: print( " Zipping {} OpenSong file…".format( filename ) )
zf = zipfile.ZipFile( filepath+'.zip', 'w', compression=zipfile.ZIP_DEFLATED )
zf.write( filepath, filename )
zf.close()
if validationSchema: return xw.validate( validationSchema )
if BibleOrgSysGlobals.verbosityLevel > 0 and BibleOrgSysGlobals.maxProcesses > 1:
print( " createOpenSongXML finished successfully." )
return True
# end of createOpenSongXML
class OpenSongXMLBible( Bible ):
"""
Class for reading, validating, and converting OpenSong Bible XML.
"""
treeTag = 'bible'
bookTag = 'b'
chapterTag = 'c'
verseTag = 'v'
def __init__( self, sourceFolder, givenName, encoding='utf-8' ):
"""
Constructor: just sets up the XML Bible file converter object.
"""
# Setup and initialise the base class first
if BibleOrgSysGlobals.debugFlag: print( "OpenSongXMLBible( {}, {}, {} )".format( sourceFolder, givenName, encoding ) )
Bible.__init__( self )
self.objectNameString = 'OpenSong XML Bible object'
self.objectTypeString = 'OpenSong'
# Now we can set our object variables
self.sourceFolder, self.givenName, self.encoding = sourceFolder, givenName, encoding
self.sourceFilepath = os.path.join( self.sourceFolder, self.givenName )
self.XMLTree = None # Will hold the XML data
# Get the data tables that we need for proper checking
#self.ISOLanguages = ISO_639_3_Languages().loadData()
self.genericBOS = BibleOrganisationalSystem( 'GENERIC-KJV-66-ENG' )
# Do a preliminary check on the readability of our file
if not os.access( self.sourceFilepath, os.R_OK ):
print( "OpenSongXMLBible: File {!r} is unreadable".format( self.sourceFilepath ) )
self.name = self.givenName
#if self.name is None:
#pass
# end of OpenSongXMLBible.__init__
def load( self ):
"""
Load a single source XML file and load book elements.
"""
if BibleOrgSysGlobals.verbosityLevel > 2: print( _("Loading {}…").format( self.sourceFilepath ) )
self.XMLTree = ElementTree().parse( self.sourceFilepath )
if BibleOrgSysGlobals.debugFlag: assert len( self.XMLTree ) # Fail here if we didn't load anything at all
# Find the main (bible) container
if self.XMLTree.tag == OpenSongXMLBible.treeTag:
location = "XML file"
BibleOrgSysGlobals.checkXMLNoText( self.XMLTree, location, '4f6h' )
BibleOrgSysGlobals.checkXMLNoTail( self.XMLTree, location, '1wk8' )
name = shortName = None
for attrib,value in self.XMLTree.items():
if attrib=="n":
name = value
elif attrib=="sn":
shortName = value
else: logging.warning( "Unprocessed {!r} attribute ({}) in main element".format( attrib, value ) )
# Find the submain (book) containers
for element in self.XMLTree:
if element.tag == OpenSongXMLBible.bookTag:
sublocation = "book in " + location
BibleOrgSysGlobals.checkXMLNoText( element, sublocation, 'g3g5' )
BibleOrgSysGlobals.checkXMLNoTail( element, sublocation, 'd3f6' )
self.__validateAndExtractBook( element )
elif element.tag == 'OT':
pass
elif element.tag == 'NT':
pass
else: logging.error( "Expected to find {!r} but got {!r}".format( OpenSongXMLBible.bookTag, element.tag ) )
else: logging.error( "Expected to load {!r} but got {!r}".format( OpenSongXMLBible.treeTag, self.XMLTree.tag ) )
self.doPostLoadProcessing()
# end of OpenSongXMLBible.load
def __validateAndExtractBook( self, book ):
"""
Check/validate and extract book data from the given XML book record
finding chapter subelements.
"""
global BibleBooksNames
if BibleOrgSysGlobals.verbosityLevel > 3: print( _("Validating OpenSong XML book…") )
# Process the div attributes first
BBB = bookName = None
for attrib,value in book.items():
if attrib=="n":
bookName = value
else: logging.warning( "Unprocessed {!r} attribute ({}) in book element".format( attrib, value ) )
if bookName:
BBB = self.genericBOS.getBBBFromText( bookName ) # Booknames are usually in English
if not BBB: # wasn't English
if BibleBooksNames is None:
BibleBooksNames = BibleBooksNamesSystems().loadData()
BBB = BibleBooksNames.getBBBFromText( bookName ) # Try non-English booknames
#print( "bookName", bookName, BBB )
if BBB:
if BibleOrgSysGlobals.verbosityLevel > 2: print( _("Validating {} {}…").format( BBB, bookName ) )
thisBook = BibleBook( self, BBB )
thisBook.objectNameString = 'OpenSong XML Bible Book object'
thisBook.objectTypeString = 'OpenSong'
#thisBook.sourceFilepath = self.sourceFilepath
USFMAbbreviation = BibleOrgSysGlobals.BibleBooksCodes.getUSFMAbbreviation( BBB )
thisBook.addLine( 'id', '{} imported by {}'.format( USFMAbbreviation.upper(), ProgNameVersion ) )
thisBook.addLine( 'h', bookName )
thisBook.addLine( 'mt1', bookName )
for element in book:
if element.tag == OpenSongXMLBible.chapterTag:
sublocation = "chapter in {}".format( BBB )
BibleOrgSysGlobals.checkXMLNoText( element, sublocation, 'j3jd' )
BibleOrgSysGlobals.checkXMLNoTail( element, sublocation, 'al1d' )
self.__validateAndExtractChapter( BBB, thisBook, element )
else: logging.error( "Expected to find {!r} but got {!r}".format( OpenSongXMLBible.chapterTag, element.tag ) )
if BibleOrgSysGlobals.verbosityLevel > 2: print( " Saving {} into results…".format( BBB ) )
self.stashBook( thisBook )
else: logging.error( _("OpenSong load doesn't recognize book name: {!r}").format( bookName ) ) # no BBB
else: logging.error( _("OpenSong load can't find a book name") ) # no bookName
# end of OpenSongXMLBible.__validateAndExtractBook
def __validateAndExtractChapter( self, BBB, thisBook, chapter ):
"""
Check/validate and extract chapter data from the given XML book record
finding and saving chapter numbers and
finding and saving verse elements.
"""
if BibleOrgSysGlobals.verbosityLevel > 3: print( _("Validating XML chapter…") )
# Process the div attributes first
chapterNumber = numVerses = None
for attrib,value in chapter.items():
if attrib=="n":
chapterNumber = value
elif attrib=="VERSES":
numVerses = value
else: logging.warning( "Unprocessed {!r} attribute ({}) in chapter element".format( attrib, value ) )
if chapterNumber:
#print( BBB, 'c', chapterNumber )
chapterNumber = chapterNumber.replace( 'of Solomon ', '' ) # Fix a mistake in the Chinese_SU module
thisBook.addLine( 'c', chapterNumber )
else: logging.error( "Missing 'n' attribute in chapter element for {}".format( BBB ) )
for element in chapter:
if element.tag == OpenSongXMLBible.verseTag:
sublocation = "verse in {} {}".format( BBB, chapterNumber )
BibleOrgSysGlobals.checkXMLNoTail( element, sublocation, 'l5ks' )
verseNumber = toVerseNumber = None
for attrib,value in element.items():
if attrib=="n":
verseNumber = value
elif attrib=="t":
toVerseNumber = value
else: logging.warning( "Unprocessed {!r} attribute ({}) in verse element".format( attrib, value ) )
if BibleOrgSysGlobals.debugFlag: assert verseNumber
#thisBook.addLine( 'v', verseNumber )
vText = element.text if element.text else ''
for subelement in element:
sub2location = "{} in {}".format( subelement.tag, sublocation )
BibleOrgSysGlobals.checkXMLNoAttributes( subelement, sub2location, 'ks03' )
BibleOrgSysGlobals.checkXMLNoSubelements( subelement, sub2location, 'ks05' )
if subelement.tag == 'i':
vText += '\\it {}\\it*{}'.format( subelement.text, subelement.tail )
else: logging.error( "Expected to find 'i' but got {!r}".format( subelement.tag ) )
vText += element.tail if element.tail else ''
if not vText:
logging.warning( "{} {}:{} has no text".format( BBB, chapterNumber, verseNumber ) )
#print( 'vText1', vText )
if vText: # This is the main text of the verse (follows the verse milestone)
#print( "{} {}:{} {!r}".format( BBB, chapterNumber, verseNumber, vText ) )
if '\n' in vText: # This is how they represent poety
#print( "vText", repr(vText), repr(element.text) )
for j, textBit in enumerate( vText.split( '\n' ) ):
if j==0:
thisBook.addLine( 'q1', '' )
thisBook.addLine( 'v', verseNumber + ' ' + textBit )
else: thisBook.addLine( 'q1', textBit )
else: # Just one verse line
thisBook.addLine( 'v', verseNumber + ' ' + vText )
#print( 'vText2', vText )
else: logging.error( "Expected to find {!r} but got {!r}".format( OpenSongXMLBible.verseTag, element.tag ) )
# end of OpenSongXMLBible.__validateAndExtractChapter
# end of OpenSongXMLBible class
def demo():
"""
Main program to handle command line parameters and then run what they want.
"""
if BibleOrgSysGlobals.verbosityLevel > 0: print( ProgNameVersion )
#testFolder = "Tests/DataFilesForTests/VerseViewXML/" # These are very similar
testFolder = "../../../../../Data/Work/Bibles/OpenSong Bibles/"
single1 = ( "KJV.xmm", )
single2 = ( "BIBLIA warszawska", )
good = ( "KJV.xmm", "AMP.xmm", "Chinese_SU.xmm", "Contemporary English Version.xmm", "ESV", "MKJV", \
"MSG.xmm", "NASB.xmm", "NIV", "NKJV.xmm", "NLT", "telugu.xmm", )
nonEnglish = ( "BIBLIA warszawska", "Chinese Union Version Simplified.txt", "hun_karoli", "KNV_HU", "LBLA.xmm", \
"Nowe Przymierze", "NVI.xmm", "NVI_PT", "PRT-IBS.xmm", "RV1960", "SVL.xmm", "UJPROT_HU", "vdc", \
"Vietnamese Bible.xmm", )
bad = ( "EPS99", )
allOfThem = good + nonEnglish + bad
if 1: # demo the file checking code -- first with the whole folder and then with only one folder
resultA1 = OpenSongXMLBibleFileCheck( testFolder )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestA1", resultA1 )
resultA2 = OpenSongXMLBibleFileCheck( testFolder, autoLoad=True )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestA2", resultA2 )
resultA3 = OpenSongXMLBibleFileCheck( testFolder, autoLoadBooks=True )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestA3", resultA3 )
testSubfolder = os.path.join( testFolder, 'nrsv_update/' )
resultB1 = OpenSongXMLBibleFileCheck( testSubfolder )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestB1", resultB1 )
resultB2 = OpenSongXMLBibleFileCheck( testSubfolder, autoLoad=True )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestB2", resultB2 )
resultB3 = OpenSongXMLBibleFileCheck( testSubfolder, autoLoadBooks=True )
if BibleOrgSysGlobals.verbosityLevel > 0: print( "TestB3", resultB3 )
if 0:
for j, testFilename in enumerate( allOfThem ):
if BibleOrgSysGlobals.verbosityLevel > 0: print( "\n\nOpnSng B{}/ {}".format( j+1, testFilename ) )
testFilepath = os.path.join( testFolder, testFilename )
# Demonstrate the OpenSong XML Bible class
if BibleOrgSysGlobals.verbosityLevel > 1: print( "Demonstrating the OpenSong XML Bible class…" )
if BibleOrgSysGlobals.verbosityLevel > 0: print( " Test filepath is {!r}".format( testFilepath ) )
xb = OpenSongXMLBible( testFolder, testFilename )
xb.load() # Load and process the XML
if BibleOrgSysGlobals.verbosityLevel > 0: print( xb ) # Just print a summary
#print( xb.books['JDE']._processedLines )
if 1: # Test verse lookup
import VerseReferences
for reference in ( ('OT','GEN','1','1'), ('OT','GEN','1','3'), ('OT','PSA','3','0'), ('OT','PSA','3','1'), \
('OT','DAN','1','21'),
('NT','MAT','3','5'), ('NT','JDE','1','4'), ('NT','REV','22','21'), \
('DC','BAR','1','1'), ('DC','MA1','1','1'), ('DC','MA2','1','1',), ):
(t, b, c, v) = reference
if t=='OT' and len(xb)==27: continue # Don't bother with OT references if it's only a NT
if t=='NT' and len(xb)==39: continue # Don't bother with NT references if it's only a OT
if t=='DC' and len(xb)<=66: continue # Don't bother with DC references if it's too small
svk = VerseReferences.SimpleVerseKey( b, c, v )
#print( svk, ob.getVerseDataList( reference ) )
if BibleOrgSysGlobals.verbosityLevel > 1: print( reference, svk.getShortText(), xb.getVerseText( svk ) )
if BibleOrgSysGlobals.debugFlag and debuggingThisModule and not xb: halt # if no books
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = BibleOrgSysGlobals.setup( ProgName, ProgVersion )
BibleOrgSysGlobals.addStandardOptionsAndProcess( parser )
demo()
BibleOrgSysGlobals.closedown( ProgName, ProgVersion )
# end of OpenSongXMLBible.py