-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9735b79
commit 51f2ea6
Showing
11 changed files
with
186 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +0,0 @@ | ||
from MyCapytain.resolvers.prototypes import Resolver | ||
from MyCapytain.resources.texts.api.cts import Text, Passage | ||
from MyCapytain.retrievers.cts5 import CTS | ||
|
||
|
||
class HttpCTSResolver(Resolver): | ||
def __init__(self, endpoint): | ||
if not isinstance(endpoint, CTS): | ||
raise TypeError("Endpoint should be a CTS Endpoint object") | ||
self.__endpoint__ = endpoint | ||
|
||
@property | ||
def endpoint(self): | ||
""" CTS Endpoint of the resolver | ||
:return: CTS Endpoint | ||
:rtype: CTS | ||
""" | ||
return self.__endpoint__ | ||
|
||
def getPassage(self, textId, subreference=None, prevnext=False, metadata=False): | ||
""" Retrieve a text node from the API | ||
:param textId: Text Identifier | ||
:param subreference: Passage Reference | ||
:param prevnext: Retrieve graph representing previous and next passage | ||
:param metadata: Retrieve metadata about the passage and the text | ||
:return: Passage | ||
:rtype: Passage | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
if metadata or prevnext: | ||
return text.getPassagePlus(reference=subreference) | ||
else: | ||
return text.getPassage(reference=subreference) | ||
|
||
def getSiblings(self, textId, subreference): | ||
""" Retrieve the siblings of a textual node | ||
:param textId: Text Identifier | ||
:param subreference: Passage Reference | ||
:return: (str, str) | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
return text.getPrevNextUrn(subreference) | ||
|
||
def getSiblings(self, textId, subreference): | ||
""" Retrieve the siblings of a textual node | ||
:param textId: Text Identifier | ||
:param subreference: Passage Reference | ||
:return: (str, str) | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
return text.getPrevNextUrn(subreference) | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from MyCapytain.resolvers.prototypes import Resolver | ||
from MyCapytain.resources.texts.api.cts import Text, Passage | ||
from MyCapytain.resources.collections.cts import TextInventory | ||
from MyCapytain.retrievers.cts5 import CTS | ||
|
||
|
||
class HttpCTSResolver(Resolver): | ||
def __init__(self, endpoint): | ||
if not isinstance(endpoint, CTS): | ||
raise TypeError("Endpoint should be a CTS Endpoint object") | ||
self.__endpoint__ = endpoint | ||
|
||
@property | ||
def endpoint(self): | ||
""" CTS Endpoint of the resolver | ||
:return: CTS Endpoint | ||
:rtype: CTS | ||
""" | ||
return self.__endpoint__ | ||
|
||
def getPassage(self, textId, subreference=None, prevnext=False, metadata=False): | ||
""" Retrieve a text node from the API | ||
:param textId: Text Identifier | ||
:param subreference: Passage Reference | ||
:param prevnext: Retrieve graph representing previous and next passage | ||
:param metadata: Retrieve metadata about the passage and the text | ||
:return: Passage | ||
:rtype: Passage | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
if metadata or prevnext: | ||
return text.getPassagePlus(reference=subreference) | ||
else: | ||
return text.getPassage(reference=subreference) | ||
|
||
def getSiblings(self, textId, subreference): | ||
""" Retrieve the siblings of a textual node | ||
:param textId: Text Identifier | ||
:param subreference: Passage Reference | ||
:return: (str, str) | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
return text.getPrevNextUrn(subreference) | ||
|
||
def getReffs(self, textId, level=1, subreference=None): | ||
""" Retrieve the siblings of a textual node | ||
:param textId: Text Identifier | ||
:param level: Depth for retrieval | ||
:param subreference: Passage Reference | ||
:return: (str, str) | ||
""" | ||
text = Text( | ||
urn=textId, | ||
retriever=self.endpoint | ||
) | ||
return text.getReffs(level, subreference) | ||
|
||
def getMetadata(self, objectId=None, **filters): | ||
""" Request metadata about a text or a collection | ||
:param textId: Object Identifier to filter on | ||
:param filters: Kwargs parameters. URN and Inv are available | ||
:return: Collection | ||
""" | ||
if objectId is not None: | ||
filters["urn"] = objectId | ||
|
||
ti = TextInventory() | ||
ti.parse(self.endpoint.getCapabilities(**filters)) | ||
return ti |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from MyCapytain.resolvers.cts.api import HttpCTSResolver | ||
from MyCapytain.retrievers.cts5 import CTS | ||
from MyCapytain.common.utils import xmlparser, Mimetypes, NS | ||
from MyCapytain.resources.prototypes.text import Passage | ||
|
||
from unittest import TestCase | ||
from mock import MagicMock | ||
|
||
|
||
with open("tests/testing_data/cts/getpassage.xml") as f: | ||
GET_PASSAGE = xmlparser(f) | ||
with open("tests/testing_data/cts/getpassageplus.xml") as f: | ||
GET_PASSAGE_PLUS = xmlparser(f) | ||
with open("tests/testing_data/cts/getprevnexturn.xml") as f: | ||
NEXT_PREV = xmlparser(f) | ||
with open("tests/testing_data/cts/getValidReff.1.1.xml") as f: | ||
GET_VALID_REFF = xmlparser(f) | ||
with open("tests/testing_data/cts/getCapabilities.xml") as f: | ||
GET_CAPABILITIES = xmlparser(f) | ||
|
||
|
||
class TestHttpCTSResolver(TestCase): | ||
def setUp(self): | ||
self.resolver = HttpCTSResolver(CTS("http://localhost")) | ||
self.resolver.endpoint.getPassagePlus = MagicMock(return_value=GET_PASSAGE_PLUS) | ||
self.resolver.endpoint.getPassage = MagicMock(return_value=GET_PASSAGE) | ||
self.resolver.endpoint.getPrevNextUrn = MagicMock(return_value=NEXT_PREV) | ||
self.resolver.endpoint.getValidReff = MagicMock(return_value=GET_VALID_REFF) | ||
self.resolver.endpoint.getCapabilities = MagicMock(return_value=GET_CAPABILITIES) | ||
|
||
def test_getPassage_full(self): | ||
""" Test that we can get a full text """ | ||
passage = self.resolver.getPassage("urn:cts:latinLit:phi1294.phi002.perseus-lat2") | ||
|
||
# We check we made a reroute to GetPassage request | ||
self.resolver.endpoint.getPassage.assert_called_with( | ||
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2" | ||
) | ||
self.assertIsInstance( | ||
passage, Passage, | ||
"GetPassage should always return passages objects" | ||
) | ||
|
||
children = list(passage.getReffs()) | ||
|
||
# We check the passage is able to perform further requests and is well instantiated | ||
self.resolver.endpoint.getValidReff.assert_called_with( | ||
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2", | ||
level=1 | ||
) | ||
self.assertEqual( | ||
children[0], 'urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1', | ||
"Resource should be string identifiers" | ||
) | ||
|
||
self.assertIn( | ||
"Hic est quem legis ille, quem requiris,", passage.export(output=Mimetypes.PLAINTEXT), | ||
"Export Text should work correctly" | ||
) | ||
|
||
self.assertEqual( | ||
passage.export(output=Mimetypes.ETREE).xpath(".//tei:l[@n='1']/text()", namespaces=NS, magic_string=False), | ||
["Hic est quem legis ille, quem requiris, "], | ||
"Export to Etree should give an Etree or Etree like object" | ||
) | ||
|
||
def test_getPassage_subreference(self): | ||
""" Test that we can get a subreference text passage""" | ||
passage = self.resolver.getPassage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.1") | ||
|
||
# We check we made a reroute to GetPassage request | ||
self.resolver.endpoint.getPassage.assert_called_with( | ||
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1" | ||
) | ||
self.assertIsInstance( | ||
passage, Passage, | ||
"GetPassage should always return passages objects" | ||
) | ||
|
||
children = list(passage.getReffs()) | ||
|
||
# We check the passage is able to perform further requests and is well instantiated | ||
self.resolver.endpoint.getValidReff.assert_called_with( | ||
urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1", | ||
level=1 | ||
) | ||
self.assertEqual( | ||
children[0], 'urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.1.1', | ||
"Resource should be string identifiers" | ||
) | ||
|
||
self.assertIn( | ||
"Hic est quem legis ille, quem requiris,", passage.export(output=Mimetypes.PLAINTEXT), | ||
"Export Text should work correctly" | ||
) | ||
|
||
self.assertEqual( | ||
passage.export(output=Mimetypes.ETREE).xpath(".//tei:l[@n='1']/text()", namespaces=NS, magic_string=False), | ||
["Hic est quem legis ille, quem requiris, "], | ||
"Export to Etree should give an Etree or Etree like object" | ||
) | ||
|
Empty file.
File renamed without changes.
File renamed without changes.