summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2017-05-12 17:29:29 +0200
committerMatěj Cepl <mcepl@cepl.eu>2017-05-12 17:29:29 +0200
commitd4c2be0a9c7569996561eab85ac41bdc4eca2d8a (patch)
tree6f365f5eb8a8728ff20b1b12b345d6c035e12cd3
parent868db285d752309f53d0b7a261ce108576a41cd9 (diff)
downloadgenerate_references-d4c2be0a9c7569996561eab85ac41bdc4eca2d8a.tar.gz
Some more tests enabled.
Also, use re.VERBOSE. It *is* more readable.
-rwxr-xr-xgenerate_reference.py59
-rwxr-xr-xtests/test_generate_reference.py12
2 files changed, 50 insertions, 21 deletions
diff --git a/generate_reference.py b/generate_reference.py
index edc4703..e182205 100755
--- a/generate_reference.py
+++ b/generate_reference.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
import collections
import logging
@@ -91,15 +92,38 @@ TranslDict = collections.OrderedDict([
CZ_BOOKS = tuple(TranslDict.keys())
EN_BOOKS = tuple(TranslDict.values())
-BIBLE_REF_PARSE_RE = re.compile(r'(%s)?\s*([0-9]+),([0-9—.-]+)'
- % '|'.join(CZ_BOOKS))
-SPLIT_VERSE_RE = re.compile('[—.-]')
-
+BIBLE_REF_PARSE_RE = re.compile(r'''
+ (%s)? # Name of the Bible book (optional)
+ \s*([0-9]+), # chapter number separated by whitespace
+ ([0-9—.-]+) # verse number(s) separated by (optional) comma
+''' % '|'.join(CZ_BOOKS), flags=re.VERBOSE)
+ONLY_VERSE_PARSE_RE = re.compile(r'''
+ v\.\s+ # abbreviation of "verš" (verse)
+ ([0-9—.-]+) # verse number(s) separated by (optional) comma
+''', flags=re.VERBOSE)
+SPLIT_VERSE_RE = re.compile('''
+ (\d+)
+ [—.-]
+ (\d+)
+''', flags=re.VERBOSE)
+
+
+def verse_interval(verse):
+ match = SPLIT_VERSE_RE.search(verse)
+ logging.debug('match = %s', match)
+ if match:
+ matches = match.groups()
+ verse = int(match.group(1))
+ next_verse = int(match.group(2))
+ logging.debug('verse, next_verse = %s, %s', verse, next_verse)
+ return (verse, next_verse)
+ else:
+ return (int(verse),)
def parse_notes_test(instr):
logging.debug('%s\ninstr = %s', '-' * 30, instr)
matches = BIBLE_REF_PARSE_RE.findall(instr)
- logging.debug('matches = %s', matches)
+ logging.debug('BIBLE_REF_PARSE_RE matches = %s', matches)
out_list = []
book = None
@@ -109,16 +133,23 @@ def parse_notes_test(instr):
book = match[0]
elif book is None:
book = ''
- chapter = int(match[1])
- verse = match[2]
- if SPLIT_VERSE_RE.search(verse):
- verse_split = SPLIT_VERSE_RE.split(verse)
- verse = int(verse_split[0])
- next_verse = int(verse_split[1])
- logging.debug('verse, next_verse = %s, %s', verse, next_verse)
- out_list.append((book, chapter, verse, next_verse))
+ logging.debug('match[1] = %s', match[1])
+ if match[1]:
+ chapter = int(match[1].rstrip(','))
else:
- out_list.append((book, chapter, int(verse)))
+ chapter = 0
+ logging.debug('match[2] = %s', match[2])
+ verse = match[2]
+ out_list.append((book, chapter) + verse_interval(match[2]))
+ matches = ONLY_VERSE_PARSE_RE.findall(instr)
+ logging.debug('ONLY_VERSE_PARSE_RE matches = %s', matches)
+
+ for match in matches:
+ logging.debug('match = %s', match)
+ if match:
+ out = ('', 0) + verse_interval(match)
+ out_list.append(out)
+
return tuple(out_list)
diff --git a/tests/test_generate_reference.py b/tests/test_generate_reference.py
index 4207db2..6037d73 100755
--- a/tests/test_generate_reference.py
+++ b/tests/test_generate_reference.py
@@ -30,8 +30,8 @@ class TestNotesParsing(unittest.TestCase): # IGNORE:C0111
raise
def test_simple_reference(self):
- instr = 'Ž 93,2'
- exptuple = (('Ž', 93, 2),)
+ instr = 'Ž 93,28'
+ exptuple = (('Ž', 93, 28),)
self.do_test_expected(instr, exptuple)
def test_prvni_empty(self):
@@ -166,17 +166,15 @@ class TestNotesParsing(unittest.TestCase): # IGNORE:C0111
('Dt', 1, 10), ('Na', 3, 16))
self.do_test_expected(instr, exptuple)
- @unittest.skip('Not implented yet')
def test_p_suffix(self):
# TODO what does 'p' suffix means?
instr = 'v. 1p'
- exptuple = (('', 0, 1))
+ exptuple = (('', 0, 1),)
self.do_test_expected(instr, exptuple)
- @unittest.skip('Not implented yet')
def test_unknown_28(self):
- instr = 'v. 28; 2,3; 5,2; 12,2; [v h. je slsoḇ–r–ḵ vždyspojeno'
- exptuple = ()
+ instr = 'v. 28; 2,3; 5,2; 12,2; [v h. je slsoḇ–r–ḵ vždy spojeno'
+ exptuple = (('', 2, 3), ('', 5, 2), ('', 12, 2), ('', 0, 28))
self.do_test_expected(instr, exptuple)
@unittest.skip('Not implented yet')