def getNumVersesList(self, BBB, allowAlternatives=False): """ Returns a list containing an integer for each chapter indicating the number of verses. The length of the list is the number of chapters in the book. """ if BibleOrgSysGlobals.debugFlag and debuggingThisModule: print(exp("getNumVersesList( {} )").format(BBB)) assert len(BBB) == 3 if not allowAlternatives: return BibleVersificationSystem.getNumVersesList(self, BBB) # Well, we are allowed alternatives, but try the given BBB first anyway bookVersesList = None try: bookVersesList = BibleVersificationSystem.getNumVersesList( self, BBB) except KeyError: # BBB doesn't exist in this BOS -- try an alternative # Next line will raise an error if no alternatives (coz returns None) for altBBB in BibleOrgSysGlobals.BibleBooksCodes.getPossibleAlternativeBooksCodes( BBB): try: bookVersesList = BibleVersificationSystem.getNumVersesList( self, altBBB) break except KeyError: continue # BBB doesn't exist in this BOS -- try an alternative if bookVersesList is not None: print("Changed {} to {} in {!r} versification scheme".format( BBB, altBBB, BibleVersificationSystem.getVersificationSystemName(self))) return bookVersesList
def isValidBCVRef( self, referenceTuple, referenceString, wantErrorMessages=False ): """ Returns True/False indicating if the given reference is valid in this system. """ BBB, C, V, S = referenceTuple if BibleBookOrderSystem.containsBook( self, BBB ): return BibleVersificationSystem.isValidBCVRef( self, referenceTuple, referenceString, wantErrorMessages ) elif wantErrorMessages: logging.error( _("{} {}:{} is invalid book for reference '{}' in {} versification system for {}").format(BBB,C,V,referenceString, self.getBookOrderSystemName(),self.getOrganizationalSystemName()) ) return False
def isValidBCVRef(self, referenceTuple, referenceString, extended=False): """ Returns True/False indicating if the given reference is valid in this system. Extended flag allows chapter and verse numbers of zero. """ #print( "BibleOrganizationalSystem.isValidBCVRef( {}, {}, {}, {} )".format( referenceTuple, referenceString, extended ) ) BBB, C, V, S = referenceTuple if BBB is None or not BBB: return False assert (len(BBB) == 3) if C and not C.isdigit( ): # Should be no suffix on C (although it can be blank if the reference is for a whole book) print( "BibleOrganizationalSystem.isValidBCVRef( {}, {}, {} ) expected C to be digits" .format(referenceTuple, referenceString, extended)) assert ( not V or V.isdigit() ) # Should be no suffix on V (although it can be blank if the reference is for a whole chapter) assert (not S or len(S) == 1 and S.isalpha() ) # Suffix should be only one lower-case letter if anything if BBB and BibleBookOrderSystem.containsBook(self, BBB): return BibleVersificationSystem.isValidBCVRef(self, referenceTuple, referenceString, extended=extended) logging.error( _("{} {}:{} is invalid book for reference '{}' in {} versification system for {}" ).format(BBB, C, V, referenceString, self.getBookOrderSystemName(), self.getOrganizationalSystemName())) return False
def __makeAbsoluteVerseList( self ): """ Make up a list of four-tuples containing BBB, chapterNumber, firstVerseNumber, lastVerseNumber """ accumulatedCount = 0 for BBB in self.getBookList(): #print( BBB, BibleVersificationSystem.getNumVersesList( self, BBB ) ) for j,numVerses in enumerate( BibleVersificationSystem.getNumVersesList( self, BBB ) ): #print( BBB, j, numVerses ) BibleOrganizationalSystem.__absoluteVerseDict[(BBB,j+1)] = (accumulatedCount+1,accumulatedCount+numVerses) accumulatedCount += numVerses
def __makeAbsoluteVerseList( self ): """ Make up a list of four-tuples containing BBB, chapterNumber, firstVerseNumber, lastVerseNumber """ accumulatedCount = 0 for BBB in self.getBookList(): #print( BBB, BibleVersificationSystem.getNumVersesList( self, BBB ) ) for j,numVerses in enumerate( BibleVersificationSystem.getNumVersesList( self, BBB ) ): #print( BBB, j, numVerses ) BibleOrganisationalSystem.__absoluteVerseDict[(BBB,j+1)] = (accumulatedCount+1,accumulatedCount+numVerses) accumulatedCount += numVerses
def __init__( self, systemName ): """ Constructor: """ self.__boss = BibleOrganizationalSystems().loadData() # Doesn't reload the XML unnecessarily :) result = self.__boss.getOrganizationalSystem( systemName ) if result is None: self.__dataDict = self.__systemName = None return # else: self.__dataDict = result self.__systemName = systemName #print( self.__dataDict ) # Now initialize the inherited classes value1 = self.getOrganizationalSystemValue( 'bookOrderSystem' ) if value1: BibleBookOrderSystem.__init__( self, value1 ) value2 = self.getOrganizationalSystemValue( 'versificationSystem' ) if value2: BibleVersificationSystem.__init__( self, value2 ) value3 = self.getOrganizationalSystemValue( 'punctuationSystem' ) if value3: BiblePunctuationSystem.__init__( self, value3 ) value4 = self.getOrganizationalSystemValue( 'booksNamesSystem' ) if value4: BibleBooksNamesSystem.__init__( self, value4, BibleBookOrderSystem.getBookList(self) ) # Does one extra step To create the input abbreviations
def isValidBCVRef( self, referenceTuple, referenceString, extended=False ): """ Returns True/False indicating if the given reference is valid in this system. Extended flag allows chapter and verse numbers of zero. """ #print( "BibleOrganizationalSystem.isValidBCVRef( {}, {}, {}, {} )".format( referenceTuple, referenceString, extended ) ) BBB, C, V, S = referenceTuple if BBB is None or not BBB: return False assert( len(BBB) == 3 ) if C and not C.isdigit(): # Should be no suffix on C (although it can be blank if the reference is for a whole book) print( "BibleOrganizationalSystem.isValidBCVRef( {}, {}, {} ) expected C to be digits".format( referenceTuple, referenceString, extended ) ) assert( not V or V.isdigit() ) # Should be no suffix on V (although it can be blank if the reference is for a whole chapter) assert( not S or len(S)==1 and S.isalpha() ) # Suffix should be only one lower-case letter if anything if BBB and BibleBookOrderSystem.containsBook( self, BBB ): return BibleVersificationSystem.isValidBCVRef( self, referenceTuple, referenceString, extended=extended ) logging.error( _("{} {}:{} is invalid book for reference '{}' in {} versification system for {}").format(BBB,C,V,referenceString, self.getBookOrderSystemName(),self.getOrganizationalSystemName()) ) return False
def createEmptyUSFMBooks( folderPath, currentBBB, requestDict ): """ Create empty USFM books or CV shells in the given folderPath as requested by the dictionary parameters: Books: 'OT' Fill: 'Versification' Versification: 'KJV' Version: 'KJV1611' """ from BibleVersificationSystems import BibleVersificationSystem from InternalBible import OT39_BOOKLIST, NT27_BOOKLIST from InternalBibleInternals import BOS_ALL_ADDED_MARKERS from USFMBible import USFMBible if BibleOrgSysGlobals.debugFlag and debuggingThisModule: print( exp("createEmptyUSFMBooks( {}, {}, {} )").format( folderPath, currentBBB, requestDict ) ) versificationObject = BibleVersificationSystem( requestDict['Versification'] ) \ if requestDict['Fill']=='Versification' else None print( 'versificationObject', versificationObject ) if versificationObject is not None: getNumChapters, getNumVerses = versificationObject.getNumChapters, versificationObject.getNumVerses if requestDict['Fill'] == 'Version': #ALL_CHAR_MARKERS = BibleOrgSysGlobals.USFMMarkers.getCharacterMarkersList( expandNumberableMarkers=True ) uB = USFMBible( requestDict['Version'] ) # Get the Bible object print( "Fill Bible1", uB ) uB.preload() print( "Fill Bible2", uB ) #uB.loadBooks() #print( "Fill Bible3", uB ) if requestDict['Books'] == 'None': booklist = [] elif requestDict['Books'] == 'Current': booklist = [ currentBBB ] elif requestDict['Books'] == 'All': booklist = OT39_BOOKLIST + NT27_BOOKLIST elif requestDict['Books'] == 'OT': booklist = OT39_BOOKLIST elif requestDict['Books'] == 'NT': booklist = NT27_BOOKLIST else: halt # programming error count = 0 skippedBooklist = [] for BBB in booklist: if requestDict['Fill'] == 'Versification' \ and versificationObject is not None \ and BBB not in versificationObject: skippedBooklist.append( BBB ) continue #if requestDict['Fill'] == 'Version' \ #and uB is not None \ #and BBB not in uB: #skippedBooklist.append( BBB ) #continue USFMAbbreviation = BibleOrgSysGlobals.BibleBooksCodes.getUSFMAbbreviation( BBB ) USFMNumber = BibleOrgSysGlobals.BibleBooksCodes.getUSFMNumber( BBB ) if requestDict['Fill'] == 'None': bookText = '' elif requestDict['Fill'] == 'Basic': bookText = '\\id {} Empty book created by {}\n'.format( USFMAbbreviation.upper(), APP_NAME_VERSION ) bookText += '\\ide UTF-8\n' bookText += '\\h Bookname\n' bookText += '\\mt Book Title\n' bookText += '\\c 1\n' elif requestDict['Fill'] == 'Versification': bookText = createEmptyUSFMBookText( BBB, getNumChapters, getNumVerses ) elif requestDict['Fill'] == 'Version': try: uB.loadBook( BBB ) except FileNotFoundError: skippedBooklist.append( BBB ) continue uBB = uB[BBB] # Get the Bible book object bookText = '' for verseDataEntry in uBB._processedLines: pseudoMarker, cleanText = verseDataEntry.getMarker(), verseDataEntry.getCleanText() #print( BBB, pseudoMarker, repr(cleanText) ) if '¬' in pseudoMarker or pseudoMarker in BOS_ALL_ADDED_MARKERS or pseudoMarker in ('c#','vp#',): continue # Just ignore added markers -- not needed here #if pseudoMarker in ('v','f','fr','x','xo',): # These fields should always end with a space but the processing will have removed them #pseudoMarker += ' ' # Append a space since it didn't have one #if pseudoMarker in ALL_CHAR_MARKERS: # Character markers to be closed #print( "CHAR MARKER" ) #pass ##if (USFM[-2]=='\\' or USFM[-3]=='\\') and USFM[-1]!=' ': #if bookText[-1] != ' ': #bookText += ' ' # Separate markers by a space e.g., \p\bk Revelation #if BibleOrgSysGlobals.debugFlag: print( "toUSFM: Added space to {!r} before {!r}".format( bookText[-2], pseudoMarker ) ) #adjValue += '\\{}*'.format( pseudoMarker ) # Do a close marker #elif pseudoMarker in ('f','x',): inField = pseudoMarker # Remember these so we can close them later #elif pseudoMarker in ('fr','fq','ft','xo',): USFM += ' ' # These go on the same line just separated by spaces and don't get closed if bookText: bookText += '\n' # paragraph markers go on a new line if not cleanText: bookText += '\\{}'.format( pseudoMarker ) elif pseudoMarker == 'c': bookText += '\\c {}'.format( cleanText ) elif pseudoMarker == 'v': bookText += '\\v {} '.format( cleanText ) else: bookText += '\\{} '.format( pseudoMarker ) #print( pseudoMarker, USFM[-200:] ) else: halt # programming error # Write the actual file filename = '{}-{}.USFM'.format( USFMNumber, USFMAbbreviation ) with open( os.path.join( folderPath, filename ), mode='wt', encoding='utf-8' ) as theFile: theFile.write( bookText ) count += 1 print( len(skippedBooklist), "books skipped:", skippedBooklist ) # Should warn the user here print( count, "books created" )
def __init__( self, systemName ): """ Constructor: """ def getOrganizationalSystemValue( valueName ): """ Gets a value for the system. """ def getMoreBasicTypes(): """ Returns a list of more basic (original) types. """ ix = allowedTypes.index( self.__dataDict["type"] ) return allowedTypes[ix+1:] # end of getMoreBasicTypes #print( "q0", valueName ) if valueName in self.__dataDict: return self.__dataDict[valueName] # else maybe we can find the value in a derived text #print( "q1", self.getOrganizationalSystemName() ) for tryType in getMoreBasicTypes(): if 'usesText' in self.__dataDict: for trySystemName in self.__dataDict['usesText']: #print( "q2", "{} is trying usesText of {}".format(self.__systemName,trySystemName) ) result = self.__boss.getOrganizationalSystemValue( trySystemName, valueName ) #print( " result is", result ) if result is not None: return result if 'derivedFrom' in self.__dataDict: trySystemName = self.__dataDict['derivedFrom'] if isinstance( trySystemName, str ): if BibleOrgSysGlobals.debugFlag: print( "trySystemName for 'derivedFrom' is a string: {!r}".format( trySystemName ) ) elif isinstance( trySystemName, list ): #print( "trySystemName for 'derivedFrom' is a list: {!r}".format( trySystemName ) ) trySystemName = trySystemName[0] # Take the first string from the list #print( "q3", "{} is trying derivedFrom of {}".format(self.__systemName,trySystemName) ) result = self.__boss.getOrganizationalSystemValue( trySystemName, valueName ) #print( " result is", result ) if result is not None: return result # else we couldn't find it anywhere logging.error( _("{} Bible Organizational System has no {} specified (b)").format(self.__systemName,valueName) ) # end of getOrganizationalSystemValue if BibleOrgSysGlobals.verbosityLevel > 2: print( "Loading {!r} system".format( systemName ) ) assert( systemName and isinstance( systemName, str ) ) self.__boss = BibleOrganizationalSystems().loadData() # Doesn't reload the XML unnecessarily :) result = self.__boss.getOrganizationalSystem( systemName ) if result is None: self.__dataDict = self.__systemName = None del self return # else: self.__dataDict = result self.__systemName = systemName #print( self.__dataDict ) # Now initialize the inherited classes bookOrderSystemName = self.getOrganizationalSystemValue( 'bookOrderSystem' ) versificationSystemName = self.getOrganizationalSystemValue( 'versificationSystem' ) punctuationSystemName = self.getOrganizationalSystemValue( 'punctuationSystem' ) booksNamesSystemName = self.getOrganizationalSystemValue( 'booksNamesSystem' ) if BibleOrgSysGlobals.debugFlag: print( "Got organisation bits: BOS={}, VS={}, PS={}, BNS={}".format( bookOrderSystemName, versificationSystemName, punctuationSystemName, booksNamesSystemName ) ) if bookOrderSystemName and bookOrderSystemName!='None' and bookOrderSystemName!='Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} book order system".format( bookOrderSystemName ) ) BibleBookOrderSystem.__init__( self, bookOrderSystemName ) if versificationSystemName and versificationSystemName!='None' and versificationSystemName!='Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} versification system".format( versificationSystemName ) ) BibleVersificationSystem.__init__( self, versificationSystemName ) if punctuationSystemName and punctuationSystemName!='None' and punctuationSystemName!='Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} punctuation system".format( punctuationSystemName ) ) BiblePunctuationSystem.__init__( self, punctuationSystemName ) if booksNamesSystemName and booksNamesSystemName!='None' and booksNamesSystemName!='Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} books name system".format( booksNamesSystemName ) ) BibleBooksNamesSystem.__init__( self, booksNamesSystemName, getOrganizationalSystemValue( "includesBooks" ) ) # Does one extra step To create the input abbreviations # Do some cross-checking myBooks = getOrganizationalSystemValue( "includesBooks" ) if myBooks is not None: for BBB in myBooks: if not BibleBookOrderSystem.containsBook( self, BBB ): logging.error( _("Book {!r} is included in {} system but missing from {} book order system").format( BBB, self.__systemName, BibleBookOrderSystem.getBookOrderSystemName( self ) ) )
def __init__(self, systemName): """ Constructor: """ def getOrganizationalSystemValue(valueName): """ Gets a value for the system. """ def getMoreBasicTypes(): """ Returns a list of more basic (original) types. """ ix = allowedTypes.index(self.__dataDict["type"]) return allowedTypes[ix + 1:] # end of getMoreBasicTypes #print( "q0", valueName ) if valueName in self.__dataDict: return self.__dataDict[valueName] # else maybe we can find the value in a derived text #print( "q1", self.getOrganizationalSystemName() ) for tryType in getMoreBasicTypes(): if 'usesText' in self.__dataDict: for trySystemName in self.__dataDict['usesText']: #print( "q2", "{} is trying usesText of {}".format(self.__systemName,trySystemName) ) result = self.__boss.getOrganizationalSystemValue( trySystemName, valueName) #print( " result is", result ) if result is not None: return result if 'derivedFrom' in self.__dataDict: trySystemName = self.__dataDict['derivedFrom'] if isinstance(trySystemName, str): if BibleOrgSysGlobals.debugFlag: print( "trySystemName for 'derivedFrom' is a string: {!r}" .format(trySystemName)) elif isinstance(trySystemName, list): #print( "trySystemName for 'derivedFrom' is a list: {!r}".format( trySystemName ) ) trySystemName = trySystemName[ 0] # Take the first string from the list #print( "q3", "{} is trying derivedFrom of {}".format(self.__systemName,trySystemName) ) result = self.__boss.getOrganizationalSystemValue( trySystemName, valueName) #print( " result is", result ) if result is not None: return result # else we couldn't find it anywhere logging.error( _("{} Bible Organizational System has no {} specified (b)"). format(self.__systemName, valueName)) # end of getOrganizationalSystemValue if BibleOrgSysGlobals.verbosityLevel > 2: print("Loading {!r} system".format(systemName)) assert systemName and isinstance(systemName, str) self.__boss = BibleOrganizationalSystems().loadData( ) # Doesn't reload the XML unnecessarily :) result = self.__boss.getOrganizationalSystem(systemName) if result is None: self.__dataDict = self.__systemName = None del self return # else: self.__dataDict = result self.__systemName = systemName #print( self.__dataDict ) # Now initialize the inherited classes bookOrderSystemName = self.getOrganizationalSystemValue( 'bookOrderSystem') versificationSystemName = self.getOrganizationalSystemValue( 'versificationSystem') punctuationSystemName = self.getOrganizationalSystemValue( 'punctuationSystem') booksNamesSystemName = self.getOrganizationalSystemValue( 'booksNamesSystem') if BibleOrgSysGlobals.debugFlag: print("Got organisation bits: BOS={}, VS={}, PS={}, BNS={}".format( bookOrderSystemName, versificationSystemName, punctuationSystemName, booksNamesSystemName)) if bookOrderSystemName and bookOrderSystemName != 'None' and bookOrderSystemName != 'Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} book order system".format(bookOrderSystemName)) BibleBookOrderSystem.__init__(self, bookOrderSystemName) if versificationSystemName and versificationSystemName != 'None' and versificationSystemName != 'Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print("Uses {!r} versification system".format( versificationSystemName)) BibleVersificationSystem.__init__(self, versificationSystemName) if punctuationSystemName and punctuationSystemName != 'None' and punctuationSystemName != 'Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print("Uses {!r} punctuation system".format( punctuationSystemName)) BiblePunctuationSystem.__init__(self, punctuationSystemName) if booksNamesSystemName and booksNamesSystemName != 'None' and booksNamesSystemName != 'Unknown': if BibleOrgSysGlobals.verbosityLevel > 2: print( "Uses {!r} books name system".format(booksNamesSystemName)) BibleBooksNamesSystem.__init__( self, booksNamesSystemName, getOrganizationalSystemValue('includesBooks') ) # Does one extra step To create the input abbreviations # Do some cross-checking myBooks = getOrganizationalSystemValue('includesBooks') if myBooks is not None: for BBB in myBooks: if not BibleBookOrderSystem.containsBook(self, BBB): logging.error( _("Book {!r} is included in {} system but missing from {} book order system" ).format( BBB, self.__systemName, BibleBookOrderSystem.getBookOrderSystemName( self)))