Skip to content

Commit

Permalink
Fixed some various issues that were introduced. Simplification of the…
Browse files Browse the repository at this point in the history
… resource
  • Loading branch information
PonteIneptique committed Dec 13, 2016
1 parent 2f1be20 commit 2154180
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 57 deletions.
47 changes: 36 additions & 11 deletions MyCapytain/resources/texts/locals/tei.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ def getTextualNode(self, subreference=None, simple=False):
start, end = citation_start.fill(passage=start), citation_end.fill(passage=end)
start, end = normalizeXpath(start.split("/")[2:]), normalizeXpath(end.split("/")[2:])

if isinstance(self.xml, etree._Element):
root = copyNode(self.xml)
xml = self.textObject.xml

if isinstance(xml, etree._Element):
root = copyNode(xml)
else:
root = copyNode(self.xml.getroot())
root = copyNode(xml.getroot())

root = passageLoop(self.xml, root, start, end)
root = passageLoop(xml, root, start, end)

if self.urn:
urn, subreference = URN("{}:{}".format(self.urn, subreference)), subreference
Expand All @@ -108,7 +110,10 @@ def _getSimplePassage(self, reference=None):
"""
if reference is None:
return __SimplePassage__(
self.resource, reference=None, urn=self.urn, citation=self.citation,
resource=self.resource,
reference=None,
urn=self.urn,
citation=self.citation,
text=self
)

Expand Down Expand Up @@ -138,8 +143,6 @@ def textObject(self):
text = None
if isinstance(self, Text):
text = self
elif hasattr(self, "__text__") and self.__text__ is not None:
text = self.__text__
return text

def getReffs(self, level=1, subreference=None):
Expand Down Expand Up @@ -282,7 +285,7 @@ class __SimplePassage__(__SharedMethods__, encodings.TEIResource, text.Passage):
:param text: Text containing the passage
:type text: Text
"""
def __init__(self, resource, reference, citation, urn=None, text=None):
def __init__(self, resource, reference, citation, text, urn=None):
super(__SimplePassage__, self).__init__(
resource=resource,
citation=citation,
Expand Down Expand Up @@ -334,10 +337,10 @@ def getReffs(self, level=1, subreference=None):
:rtype: List.basestring
:returns: List of levels
"""
level = self.depth + level
level += self.depth
if not subreference:
subreference = self.reference
return __SharedMethods__.getValidReff(self, level, reference=subreference)
return self.textObject.getValidReff(level, reference=subreference)

def getTextualNode(self, subreference=None):
""" Special GetPassage implementation for SimplePassage (Simple is True by default)
Expand All @@ -347,7 +350,7 @@ def getTextualNode(self, subreference=None):
"""
if not isinstance(subreference, Reference):
subreference = Reference(subreference)
return __SharedMethods__.getTextualNode(self, subreference)
return self.textObject.getTextualNode(subreference)

@property
def nextId(self):
Expand Down Expand Up @@ -405,6 +408,14 @@ def siblingsId(self):
self.__prevnext__ = (_prev, _next)
return self.__prevnext__

@property
def textObject(self):
""" Text Object. Required for NextPrev
:rtype: Text
"""
return self.__text__


class Text(__SharedMethods__, encodings.TEIResource, text.CitableText):
""" Implementation of CTS tools for local files
Expand Down Expand Up @@ -519,6 +530,8 @@ def __init__(self, reference, urn=None, citation=None, resource=None, text=None)

@property
def reference(self):
""" Reference of the object
"""
return self.__reference__

@property
Expand Down Expand Up @@ -622,11 +635,15 @@ def siblingsId(self):

@property
def next(self):
""" Next Passage (Interactive Passage)
"""
if self.nextId is not None:
return __SharedMethods__.getTextualNode(self.__text__, self.nextId)

@property
def prev(self):
""" Previous Passage (Interactive Passage)
"""
if self.prevId is not None:
return __SharedMethods__.getTextualNode(self.__text__, self.prevId)

Expand All @@ -636,3 +653,11 @@ def getTextualNode(self, subreference=None, *args, **kwargs):
X = __SharedMethods__.getTextualNode(self, subreference)
X.__text__ = self.__text__
return X

@property
def textObject(self):
""" Text Object. Required for NextPrev
:rtype: Text
"""
return self.__text__
1 change: 1 addition & 0 deletions tests/resolvers/cts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_getPassage_full(self):
children = list(passage.getReffs())

# We check the passage is able to perform further requests and is well instantiated
print(children)
self.assertEqual(
children[0], '1',
"Resource should be string identifiers"
Expand Down
109 changes: 67 additions & 42 deletions tests/resources/commonTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
from MyCapytain.common.utils import Mimetypes
from MyCapytain.resources.texts.locals.tei import Text
from io import open
import functools


def call_with_simple(f):
""" Simple decorator tool to run a test with both simple = True and simple = False
:param f:
:return:
"""
@functools.wraps(f)
def decorator(*args):
for arg_tuple in [True, False]:
f(*args, arg_tuple)
return decorator


class CapitainsXmlTextTest(TestCase):
Expand Down Expand Up @@ -229,15 +243,17 @@ def test_urn(self):
with self.assertRaises(TypeError):
self.TEI.urn = 2

def test_get_passage(self):
a = self.TEI.getTextualNode(["1", "pr", "2"])
@call_with_simple
def test_get_passage(self, simple):
a = self.TEI.getTextualNode(["1", "pr", "2"], simple=simple)
self.assertEqual(a.export(output=Mimetypes.PLAINTEXT), "tum, ut de illis queri non possit quisquis de se bene ")
# With reference
a = self.TEI.getTextualNode(Reference("2.5.5"))
a = self.TEI.getTextualNode(Reference("2.5.5"), simple=simple)
self.assertEqual(a.export(output=Mimetypes.PLAINTEXT), "Saepe domi non es, cum sis quoque, saepe negaris: ")

def test_get_passage_autoparse(self):
a = self.TEI.getTextualNode(Reference("2.5.5"))
@call_with_simple
def test_get_passage_autoparse(self, simple):
a = self.TEI.getTextualNode(Reference("2.5.5"), simple=simple)
self.assertEqual(
a.export(output=Mimetypes.PLAINTEXT), "Saepe domi non es, cum sis quoque, saepe negaris: ",
"Text are automatically parsed in GetPassage hypercontext = False"
Expand Down Expand Up @@ -347,10 +363,11 @@ def test_get_Passage_context_no_double_slash(self):
"Ensure passage finding with context is fully TEI / Capitains compliant (Different level range Passage)"
)

def test_get_passage_with_list(self):
@call_with_simple
def test_get_passage_with_list(self, simple):
""" In range, passage in between could be removed from the original text by error
"""
simple = self.TEI.getTextualNode(["1", "pr", "2"])
simple = self.TEI.getTextualNode(["1", "pr", "2"], simple=simple)
self.assertEqual(
simple.export(output=Mimetypes.PLAINTEXT).strip(),
"tum, ut de illis queri non possit quisquis de se bene",
Expand All @@ -363,11 +380,12 @@ def test_type_accepted_reference_validreff(self):
with self.assertRaises(TypeError):
self.TEI.getValidReff(reference=["1", "pr", "2", "5"])

def test_citation_length_error(self):
@call_with_simple
def test_citation_length_error(self, simple):
""" In range, passage in between could be removed from the original text by error
"""
with self.assertRaises(ReferenceError):
self.TEI.getTextualNode(["1", "pr", "2", "5"])
self.TEI.getTextualNode(["1", "pr", "2", "5"], simple=simple)

def test_ensure_passage_is_not_removed(self):
""" In range, passage in between could be removed from the original text by error
Expand Down Expand Up @@ -414,8 +432,9 @@ def test_get_passage_hypercontext_complex_xpath(self):
"Ensure passage finding with context is fully TEI / Capitains compliant (Different level range Passage)"
)

def test_Text_text_function(self):
simple = self.seneca.getTextualNode(Reference("1"))
@call_with_simple
def test_Text_text_function(self, simple):
simple = self.seneca.getTextualNode(Reference("1"), simple=simple)
str_simple = simple.tostring(encoding=str)
text = Text(
resource=str_simple,
Expand Down Expand Up @@ -498,42 +517,45 @@ def __init__(self, *args, **kwargs):
else:
self.run = lambda self, *args, **kwargs: None

def test_urn(self):
@call_with_simple
def test_urn(self, simple):
""" Test URN and ids getters/setters """

a = self.TEI.getTextualNode(["2", "40", "8"])
#self.assertEqual(
# str(a.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2",
# "Passage should have a URN parameter"
#)
a = self.TEI.getTextualNode(["2", "40", "8"], simple=simple)
self.assertEqual(
str(a.reference), "2.40.8",
"Passage should have a URN parameter"
)

def test_next(self):
@call_with_simple
def test_next(self, simple):
""" Test next property """
# Normal passage checking
# self.TEI.parse()
p = self.TEI.getTextualNode(["1", "pr", "1"])
p = self.TEI.getTextualNode(["1", "pr", "1"], simple=simple)
self.assertEqual(str(p.next.reference), "1.pr.2")

# End of lowest level passage checking but not end of parent level
p = self.TEI.getTextualNode(["1", "pr", "22"])
p = self.TEI.getTextualNode(["1", "pr", "22"], simple=simple)
self.assertEqual(str(p.next.reference), "1.1.1")

# End of lowest level passage and end of parent level
p = self.TEI.getTextualNode(["1", "39", "8"])
p = self.TEI.getTextualNode(["1", "39", "8"], simple=simple)
self.assertEqual(str(p.next.reference), "2.pr.sa")

# Last line should always be None
p = self.TEI.getTextualNode(["2", "40", "8"])
p = self.TEI.getTextualNode(["2", "40", "8"], simple=simple)
self.assertIsNone(p.next)
p = self.TEI.getTextualNode(["2", "40"])
p = self.TEI.getTextualNode(["2", "40"], simple=simple)
self.assertIsNone(p.next)
p = self.TEI.getTextualNode(["2"])
p = self.TEI.getTextualNode(["2"], simple=simple)
self.assertIsNone(p.next)

def test_children(self):
@call_with_simple
def test_children(self, simple):
""" Test children property """
# Normal children checking
p = self.TEI.getTextualNode(["1", "pr"])
p = self.TEI.getTextualNode(["1", "pr"], simple=simple)
self.assertEqual(
[
x for x in p.children if str(x.reference) == "1.pr.1"
Expand All @@ -542,14 +564,15 @@ def test_children(self):
""" Ensure that children are text objects and retain there capacities """
)

p = self.TEI.getTextualNode(["1", "pr", "1"])
p = self.TEI.getTextualNode(["1", "pr", "1"], simple=simple)
self.assertEqual(len(list(p.children)), 0)

def test_first(self):
@call_with_simple
def test_first(self, simple):
""" Test first property """
# Test when there is one
# self.TEI.parse()
p = self.TEI.getTextualNode(["1", "1"])
p = self.TEI.getTextualNode(["1", "1"], simple=simple)
self.assertEqual(
str(p.firstId), "1.1.1",
"Property first Id should be the reference of the first item"
Expand All @@ -559,17 +582,18 @@ def test_first(self):
"First should be a passage with passage capacities"
)
# #And failing when no first
p = self.TEI.getTextualNode(["1", "1", "1"])
p = self.TEI.getTextualNode(["1", "1", "1"], simple=simple)
self.assertEqual(
p.lastId, None,
"Property such as ID should not raise error when there is no child"
)

def test_last(self):
@call_with_simple
def test_last(self, simple):
""" Test last property """
# self.TEI.parse()
# Test when there is one
p = self.TEI.getTextualNode(["1", "pr"])
p = self.TEI.getTextualNode(["1", "pr"], simple=simple)
self.assertEqual(p.last.export(
output=Mimetypes.PLAINTEXT), "An ideo tantum veneras, ut exires? ",
".last should be a passage with passage capacities"
Expand All @@ -579,37 +603,38 @@ def test_last(self):
"Property lastId should be the reference of the last item"
)
# #And failing when no last
p = self.TEI.getTextualNode(["1", "pr", "1"])
p = self.TEI.getTextualNode(["1", "pr", "1"], simple=simple)
self.assertEqual(
p.lastId, None,
"Property such as ID should not raise error when there is no child"
)

def test_prev(self):
@call_with_simple
def test_prev(self, simple):
""" Test prev property """
# self.TEI.parse()
# Normal passage checking
p = self.TEI.getTextualNode(["2", "40", "8"])
p = self.TEI.getTextualNode(["2", "40", "8"], simple=simple)
self.assertEqual(str(p.prev.reference), "2.40.7")
p = self.TEI.getTextualNode(["2", "40"])
p = self.TEI.getTextualNode(["2", "40"], simple=simple)
self.assertEqual(str(p.prev.reference), "2.39")
p = self.TEI.getTextualNode(["2"])
p = self.TEI.getTextualNode(["2"], simple=simple)
self.assertEqual(str(p.prev.reference), "1")

# test failing passage
p = self.TEI.getTextualNode(["1", "pr", "1"])
p = self.TEI.getTextualNode(["1", "pr", "1"], simple=simple)
self.assertEqual(p.prev, None)
p = self.TEI.getTextualNode(["1", "pr"])
p = self.TEI.getTextualNode(["1", "pr"], simple=simple)
self.assertEqual(p.prev, None)
p = self.TEI.getTextualNode(["1"])
p = self.TEI.getTextualNode(["1"], simple=simple)
self.assertEqual(p.prev, None)

# First child should get to parent's prev last child
p = self.TEI.getTextualNode(["1", "1", "1"])
p = self.TEI.getTextualNode(["1", "1", "1"], simple=simple)
self.assertEqual(str(p.prev.reference), "1.pr.22")

# Beginning of lowest level passage and beginning of parent level
p = self.TEI.getTextualNode(["2", "pr", "sa"])
p = self.TEI.getTextualNode(["2", "pr", "sa"], simple=simple)
self.assertEqual(str(p.prev.reference), "1.39.8")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

import xmlunittest
from lxml import etree
from lxml import objectify

from MyCapytain.common.utils import xmlparser
import MyCapytain.common.reference
import MyCapytain.errors
import MyCapytain.resources.texts.encodings
import MyCapytain.resources.texts.locals.tei
from tests.resources.commonTests import CapitainsXmlTextTest, CapitainsXmlPassageTests, CapitainsXMLRangePassageTests

P = objectify.makeparser()
def objectifiedParser(file):
return objectify.parse(file, parser=P)

objectifiedParser = lambda x: xmlparser(x, objectify=False)


class TestLocalXMLTextImplementation(CapitainsXmlTextTest, unittest.TestCase, xmlunittest.XmlTestMixin):
Expand Down

0 comments on commit 2154180

Please sign in to comment.