Skip to content

Commit

Permalink
Added support for range GetValidReff
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Apr 15, 2016
1 parent 150861d commit b67260e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 20 deletions.
2 changes: 1 addition & 1 deletion MyCapytain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"""

__name__ = "MyCapytain"
__version__ = "1.0.0"
__version__ = "1.0.1"
__all__ = ["common", "retrievers", "resources"]
41 changes: 26 additions & 15 deletions MyCapytain/resources/texts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,47 +194,56 @@ def _getPassageContext(self, reference):
resource=root, parent=self, citation=self.citation
)

def getValidReff(self, level=1, reference=None):
def getValidReff(self, level=None, reference=None, _debug=False):
""" Retrieve valid passages directly
:param level: Depth required. If not set, should retrieve first encountered level (1 based)
:type level: int
:param reference: Passage Reference
:type reference: Reference
:param _debug: Check on passages duplicates
:type _debug: bool
:returns: List of levels
:rtype: list(basestring, str)
:
.. note:: GetValidReff works for now as a loop using Passage, subinstances of Text, to retrieve the valid
informations. Maybe something is more powerfull ?
"""
depth = 0
xml = self.xml
_range = False
if reference:
if isinstance(reference, Reference):
if reference.end is None:
passages = [reference.list]
depth = len(passages[0])
else:
xml = self.getPassage(reference=reference)
a, b = reference.start.list, reference.end.list
passages = [[]]
common = []
for index in range(0, len(reference.start.list)):
if index == (len(common) - 1):
common.append(reference.start.list[index])
else:
break

passages = [common]
depth = len(common)
if not level:
level = len(reference.start.list) + 1

else:
raise TypeError()

depth = len(passages[0])
else:
passages = [[]]

if not level:
level = 1
if level <= len(passages[0]) and reference is not None:
level = len(passages[0]) + 1
if level > len(self.citation):
return []

nodes = [None for i in range(depth, level)]
nodes = [None] * (level - depth)

citations = [citation for citation in self.citation]

Expand All @@ -261,11 +270,13 @@ def getValidReff(self, level=1, reference=None):
raise KeyError(msg)

passages = [".".join(passage) for passage in passages]
duplicates = set([n for n in passages if passages.count(n) > 1])
if len(duplicates) > 0:
message = ", ".join(duplicates)
warnings.warn(message, DuplicateReference)
del duplicates

if _debug:
duplicates = set([n for n in passages if passages.count(n) > 1])
if len(duplicates) > 0:
message = ", ".join(duplicates)
warnings.warn(message, DuplicateReference)
del duplicates

return passages

Expand Down Expand Up @@ -706,7 +717,7 @@ def text(self, exclude=None):
:Example:
>>> P = Passage(resource='<l n="8">Ibis <note>hello<a>b</a></note> ab excusso missus in astra <hi>sago.</hi> </l>')
>>> P.text == "Ibis hello b ab excusso missus in astra sago. "
>>> P.text(exclude=["note"]) == "Ibis hello b ab excusso missus in astra sago. "
>>> P.text(exclude=["note"]) == "Ibis ab excusso missus in astra sago. "
"""
Expand Down
34 changes: 30 additions & 4 deletions tests/resources/texts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,39 @@ def testValidReffs(self):
"Level should be autocorrected to len(citation) + 1 even if level == len(citation)"
)

self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.1-2.2")),
[
'2.1.1', '2.1.2', '2.1.3', '2.1.4', '2.1.5', '2.1.6', '2.1.7', '2.1.8', '2.1.9', '2.1.10', '2.1.11',
'2.1.12', '2.2.1', '2.2.2', '2.2.3', '2.2.4', '2.2.5', '2.2.6'
],
"It could be possible to ask for range reffs children")

self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.1-2.2"), level=2),
['2.1', '2.2'],
"It could be possible to ask for range References reference at the same level in between milestone")

self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("1.38-2.2"), level=2),
['1.38', '1.39', '2.pr', '2.1', '2.2'],
"It could be possible to ask for range References reference at the same level in between milestone across higher levels")

self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("1.1.1-1.1.4"), level=3),
['1.1.1', '1.1.2', '1.1.3', '1.1.4'],
"It could be possible to ask for range reffs in between at the same level cross higher level")

# Test when already too deep
self.assertEqual(self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.1.1"), level=3), [])
self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.1.1"), level=3),
[],
"Asking for a level too deep should return nothing"
)

# Test wrong citation
with self.assertRaises(KeyError):
self.assertEqual(
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.hellno"), level=3), [])
self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.hellno"), level=3)

def test_nested_dict(self):
""" Check the nested dict export of a local.Text object """
Expand All @@ -150,7 +176,7 @@ def test_warning(self):
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
for i in [1, 2, 3]:
text.getValidReff(level=i)
text.getValidReff(level=i, _debug=True)

self.assertEqual(len(w), 3, "There should be warning on each level")
self.assertEqual(issubclass(w[-1].category, MyCapytain.errors.DuplicateReference), True,
Expand Down

0 comments on commit b67260e

Please sign in to comment.