Skip to content

Commit

Permalink
Merge pull request #42 from Capitains/issue-40
Browse files Browse the repository at this point in the history
0.0.6
  • Loading branch information
PonteIneptique committed Oct 13, 2015
2 parents 69d61b7 + 9e319f7 commit aa53bfc
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 10 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__ = "0.0.5"
__version__ = "0.0.6"
__all__ = ["common", "endpoints", "resources"]
2 changes: 1 addition & 1 deletion MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re


REFSDECL_SPLITTER = re.compile("/+[a-zA-Z0-9:\[\]@=\\\{\$'\"\.]+")
REFSDECL_SPLITTER = re.compile("/+[a-zA-Z0-9:\[\]@=\\\{\$'\"\.\s]+")
REFSDECL_REPLACER = re.compile("\$[0-9]+")
SUBREFERENCE = re.compile("(\w*)\[{0,1}([0-9]*)\]{0,1}", re.UNICODE)
REFERENCE_REPLACER = re.compile("(@[a-zA-Z0-9:]+){1}(=){1}([\\\$'\"?0-9]{3,6})")
Expand Down
23 changes: 18 additions & 5 deletions MyCapytain/resources/texts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@
import MyCapytain.resources.texts.tei


class RefsDeclError(Exception):
""" Error issued when an the refsDecl does not succeed in xpath (no results)
"""
pass


class Text(text.Text):
""" Implementation of CTS tools for local files
:param urn: A URN identifier
:type urn: MyCapytain.common.reference.URN
:param resource: A resource
Expand Down Expand Up @@ -49,7 +55,14 @@ def __init__(self, urn=None, citation=None, resource=None):

self.__findCRefPattern(self.xml)

xml = self.xml.xpath(self.citation.scope, namespaces=NS)[0]
try:
xml = self.xml.xpath(self.citation.scope, namespaces=NS)[0]
except IndexError:
msg = "Main citation scope does not result in any result ({0})".format(self.citation.scope)
raise RefsDeclError(msg)
except Exception as E:
raise E

self._passages = Passage(resource=xml, citation=self.citation, urn=self.urn, id=None)

def __findCRefPattern(self, xml):
Expand Down Expand Up @@ -89,7 +102,7 @@ def getPassage(self, reference):
:param reference: Identifier of the subreference / passages
:type reference: List, MyCapytain.common.reference.Reference
:rtype: Passage
:rtype: Passage
:returns: Asked passage
"""
if isinstance(reference, MyCapytain.common.reference.Reference):
Expand All @@ -115,8 +128,8 @@ def getPassagePlus(self, reference):
return text.PassagePlus(P, P.prev.id, P.next.id)

def getValidReff(self, level=1, reference=None):
""" Retrieve valid passages directly
""" Retrieve valid passages directly
:param level: Depth required. If not set, should retrieve first encountered level (1 based)
:type level: Int
:param reference: Subreference (optional)
Expand Down
7 changes: 7 additions & 0 deletions tests/common/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ def test_updateRefsdecl(self):
)
self.assertEqual(c.refsDecl, "/TEI/text/body/div/div[@n=\"$1\"]//l[@n=\"$2\"]")

def test_updateRefsdecl_and(self):
c = Citation(
name="line",
refsDecl="/TEI/text/body/div/div[@n=\"?\" and @subtype='edition']/l[@n=\"?\"]"
)
self.assertEqual(c.refsDecl, "/TEI/text/body/div/div[@n=\"?\" and @subtype='edition']/l[@n=\"?\"]")

def test_updateScopeXpath(self):
c = Citation(
name="line",
Expand Down
12 changes: 12 additions & 0 deletions tests/endpoints/test_cts5.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ def test_request_CTS_getPassage_arguments(self):
}
)

def test_call_with_default(self):
inv = CTS("http://domainname.com/rest/cts", inventory="annotsrc")
with patch('requests.get') as patched_get:
inv.getPassage(urn="urn")
patched_get.assert_called_once_with(
"http://domainname.com/rest/cts", params={
"inv": "annotsrc",
"request": "GetPassage",
"urn": "urn"
}
)

def test_request_CTS_getPassagePlus_arguments(self):
""" Tests that methods getPassagePlus maps correctly to request"""
with patch('requests.get') as patched_get:
Expand Down
4 changes: 4 additions & 0 deletions tests/resources/texts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def testFindCitation(self):
with self.assertRaises(KeyError):
self.assertEqual(self.TEI.getValidReff(reference=MyCapytain.common.reference.Reference("2.hellno"),level=3), [])

def test_wrong_main_scope(self):
with open("tests/testing_data/texts/sample2.xml", "rb") as file:
with self.assertRaises(MyCapytain.resources.texts.local.RefsDeclError):
text = MyCapytain.resources.texts.local.Text(resource=file)

def test_reffs(self):
""" Check that every level is returned trough reffs property """
Expand Down
27 changes: 24 additions & 3 deletions tests/resources/texts/test_tei.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from MyCapytain.common.utils import xmlparser
from MyCapytain.resources.texts.tei import *
from MyCapytain.common.reference import Reference


class TestTEICitation(unittest.TestCase):
Expand Down Expand Up @@ -33,7 +34,7 @@ def test_ingest_multiple(self):
<tei:tei xmlns:tei="http://www.tei-c.org/ns/1.0">
<tei:cRefPattern n="line"
matchPattern="(\\w+).(\\w+).(\\w+)"
replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n='$1']/tei:div[@n='$2']/tei:l[@n='$3'])">
replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n='$1' and @type='section']/tei:div[@n='$2']/tei:l[@n='$3'])">
<tei:p>This pointer pattern extracts line</tei:p>
</tei:cRefPattern>
<tei:cRefPattern n="poem"
Expand Down Expand Up @@ -61,7 +62,11 @@ def test_ingest_multiple(self):
)
self.assertEqual(
str(a.child.child),
"""<tei:cRefPattern n="line" matchPattern="(\\w+)\.(\\w+)\.(\\w+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n=\'$1\']/tei:div[@n=\'$2\']/tei:l[@n=\'$3\'])"><tei:p>This pointer pattern extracts line</tei:p></tei:cRefPattern>"""
"""<tei:cRefPattern n="line" matchPattern="(\\w+)\.(\\w+)\.(\\w+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n=\'$1\' and @type=\'section\']/tei:div[@n=\'$2\']/tei:l[@n=\'$3\'])"><tei:p>This pointer pattern extracts line</tei:p></tei:cRefPattern>"""
)
self.assertEqual(
a.child.child.fill(Reference("1.2.3")),
"/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n=\'1\' and @type=\'section\']/tei:div[@n=\'2\']/tei:l[@n=\'3\']"
)

def test_ingest_single(self):
Expand All @@ -81,12 +86,28 @@ def test_ingest_single(self):
"""<tei:cRefPattern n="line" matchPattern="(\\w+)\.(\\w+)\.(\\w+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div/tei:div[@n=\'$1\']/tei:div[@n=\'$2\']/tei:l[@n=\'$3\'])"><tei:p>This pointer pattern extracts line</tei:p></tei:cRefPattern>"""
)

def test_ingest_single_and(self):
text = xmlparser("""
<tei:tei xmlns:tei="http://www.tei-c.org/ns/1.0">
<tei:cRefPattern n="section" matchPattern="(.+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div[@type='edition']/tei:div[@n='$1' and @type='section'])" />
</tei:tei>
""".replace("\n", "").replace("\s+", " "))
citation = Citation.ingest(text)
self.maxDiff = None
self.assertEqual(
str(citation),
"""<tei:cRefPattern n="section" matchPattern="(\\w+)" replacementPattern="#xpath(/tei:TEI/tei:text/tei:body/tei:div[@type='edition']/tei:div[@n=\'$1\' and @type='section'])"><tei:p>This pointer pattern extracts section</tei:p></tei:cRefPattern>"""
)
self.assertEqual(citation.scope, "/tei:TEI/tei:text/tei:body/tei:div[@type='edition']")
self.assertEqual(citation.xpath, "/tei:div[@n='?' and @type='section']")
self.assertEqual(citation.fill("1"), "/tei:TEI/tei:text/tei:body/tei:div[@type='edition']/tei:div[@n=\'1\' and @type='section']")


class TestTEIPassage(unittest.TestCase):
def test_text(self):
""" Test text attribute """
P = Passage(resource=xmlparser('<l n="8">Ibis <note>hello<a>b</a></note> ab excusso missus in astra <hi>sago.</hi> </l>'))
# Without exclusion
# Without exclusion0
self.assertEqual(P.text(), "Ibis hello b ab excusso missus in astra sago. ")
# With Exclusion
self.assertEqual(P.text(exclude=["note"]), "Ibis ab excusso missus in astra sago. ")
Expand Down

0 comments on commit aa53bfc

Please sign in to comment.