forked from Freely-Given-org/BibleOrgSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerseReferences.py
executable file
·170 lines (140 loc) · 6.18 KB
/
VerseReferences.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# VerseReferences.py
# Last modified: 2013-06-24 (also update ProgVersion below)
#
# Module handling Bible verse references
#
# Copyright (C) 2013 Robert Hunt
# Author: Robert Hunt <robert316@users.sourceforge.net>
# 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 for creating and manipulating our internal Bible verse references.
This module recognises and handles only our internal Bible references.
OSIS Bible references are of the form bookAbbreviation.chapterNumber.verseNumber
e.g., Gen.1.1 or Exod.20.10 or 2Chr.7.6 or Jude.1.2
Note that the book abbreviation is not a constant length
and may also start with a digit
and that the same separator is repeated.
However, the native Bible reference string format in this system is more tightly defined
e.g., GEN_1:1 or EXO_20:10 or CH2_7:6 or JDE_1:2b
We can see that
1/ The Bible book code is always 3-characters, starting with a letter
All letters are UPPERCASE
2/ We use an underline character as the book / chapter separator
3/ We use a colon as the chapter / verse separator
4/ We treat all chapter and verse fields as strings
5/ Verse numbers can include a lowercase letter suffix a..d
representing very approximate portions of a verse
a = first half of a verse
b = second half of a verse
c = final third of a verse
d = final quarter of a verse
Internally, we represent it as a Bible reference tuple (BBB,C,V,S,) where
BBB is the three-character UPPERCASE reference abbreviation
C is the chapter number string (There are some examples of letters being used for chapter "numbers")
V is the verse number string
S is the single lowercase letter suffix (see above)
OSIS defines reference ranges
e.g., Gen.1.1-Gen.1.2 or Gen.1.1-Gen.2.3 (inclusive)
Our ranges are slightly different (also inclusive)
e.g., Gen_1:1-Gen_1:2 but Gen_1:1–Gen_2:3
i.e., using a hyphen for a verse span but en-dash (–) for a span that crosses chapters or books.
OXES is different again and tends to remove the second (redundant) book identifier
e.g., Gen.1.1-1.2 (if I remember correctly)
"""
ProgName = "Bible verse reference handler"
ProgVersion = "0.01"
ProgNameVersion = "{} v{}".format( ProgName, ProgVersion )
debuggingThisModule = False
import os, logging
#from gettext import gettext as _
import Globals
class SimpleVerseKey():
"""
Handles individual verse references (no ranges, etc. allowed) in the BCVS form
where B is the BBB reference code
C is the chapter number string
V is the verse number string
S is the optional suffix string
The name or organisational system of the work is not specified
so we can only check that BBB is a valid abbreviation
and no checking is done on the validity of the CV values.
"""
def __init__( self, BBB, C, V, S=None ):
if S is None: S = ''
if isinstance( C, int ): C = str( C )
if isinstance( V, int ): V = str( V )
assert( isinstance( BBB, str ) and len(BBB) == 3 )
assert( isinstance( C, str ) and 1<=len(C)<=3 )
assert( isinstance( V, str ) and 1<=len(V)<=3 )
assert( isinstance( S, str ) and len(S)<3 )
assert( BBB in Globals.BibleBooksCodes )
for checkChar in ( ' -,.:' ):
assert( checkChar not in BBB )
assert( checkChar not in C )
assert( checkChar not in V )
assert( checkChar not in S )
self.BBB, self.C, self.V, self.S = BBB, C, V, S
# end of verseKey.__init__
def __eq__( self, other ):
if type( other ) is type( self ): return self.__dict__ == other.__dict__
return False
def __ne__(self, other): return not self.__eq__(other)
def __str__( self ): return "SimpleVerseKey object: {}".format( self.getShortText() )
def __len__( self ): return 4
def __getitem__( self, keyIndex ):
if keyIndex==0: return self.BBB
elif keyIndex==1: return self.C
elif keyIndex==2: return self.V
elif keyIndex==3: return self.S
else: raise IndexError
def getBBB( self ): return self.BBB
def getChapterNumber( self ): return self.C
def getChapterNumberStr( self ): return self.C
def getVerseNumber( self ): return self.V
def getVerseNumberStr( self ): return self.V
def getVerseSuffix( self ): return self.S
def getChapterNumberInt( self ):
try: return( int( self.C ) )
except: return -1
def getVerseNumberInt( self ):
try: return( int( self.V ) )
except: return -1
def getShortText( self ):
return "{} {}:{}{}".format( self.BBB, self.C, self.V, self.S )
def getOSISBookAbbreviation( self ):
return Globals.BibleBooksCodes.getOSISAbbreviation( self.BBB )
def getOSISReference( self ):
return "{}.{}.{}".format( self.getOSISBookAbbreviation(), self.C, self.V )
# end of class SimpleVerseKey
def demo():
"""
Short program to demonstrate/test the above class(es).
"""
if Globals.verbosityLevel > 0: print( ProgNameVersion )
vK = SimpleVerseKey( 'GEN', '1', '1' )
print( vK, "and", vK.getOSISReference() )
print( vK == SimpleVerseKey( 'GEN', '1', '1' ), "then", vK == SimpleVerseKey( 'EXO', '1', '1' ) )
# end of demo
if __name__ == '__main__':
# Configure basic set-up
parser = Globals.setup( ProgName, ProgVersion )
Globals.addStandardOptionsAndProcess( parser )
demo()
Globals.closedown( ProgName, ProgVersion )
# end of VerseReferences.py