def parse(self, query): # Fail if `query` is not a string. if not isinstance(query, basestring): raise TypeError( 'Non-string (%s, %s) passed to citations.parse()!' % (type(query), query)) # Fail if there are no non-white characters in `query`. tokens = query.strip().split() if len(tokens) == 0: raise ValueError( 'No non-white characters passed to citations.parse()!') # Try to parse a book out of the leading tokens in a 'greedy' # fashion. book, tokensConsumed = bible.parse(tokens) if book is None: raise ValueError('Unable to identify the book in citation "%s"!' % query) # If there is more than one token remaining, we have too many # tokens. remainingTokens = tokens[tokensConsumed:] if len(remainingTokens) > 1: raise ValueError('Extra tokens %s!' % remainingTokens[1:]) # If there is exactly remaining token, try to parse verses out # of it. verses = None if len(remainingTokens) == 1: remainingToken = remainingTokens[0] verses = addrs.parse(remainingToken) return Citation(book, verses)
def test_1_John(self): self.assertEquals(('1john', 2), bible.parse(['1', 'John']))
def test_Song_of_Songs(self): self.assertEquals(('songofsongs', 3), bible.parse(['Song', 'of', 'Songs']))
def test_John_3_16(self): self.assertEquals(('john', 1), bible.parse(['John', '3:16']))
def test_foo_bar_zod(self): self.assertEquals((None, 0), bible.parse(['foo', 'bar', 'zod']))