Skip to content

Commit

Permalink
Metadata test + Better DTS Export Support
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Dec 5, 2016
1 parent a8a9049 commit 9454273
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 108 deletions.
5 changes: 4 additions & 1 deletion MyCapytain/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ class Mimetypes:
class JSON:
Std = "application/text"
CTS = "application/ld+json:CTS"
DTS = "application/ld+json:DTS"

class DTS:
Std = "application/ld+json:DTS"
NoParents = "application/ld+json:DTS/NoParents"

class XML:
Std = "text/xml"
Expand Down
2 changes: 2 additions & 0 deletions MyCapytain/resolvers/cts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ def getMetadata(self, objectId=None, **filters):

ti = TextInventory()
ti.parse(self.endpoint.getCapabilities(**filters))
if objectId:
return [x for x in [ti] + ti.descendants if x.id == objectId][0]
return ti
22 changes: 4 additions & 18 deletions MyCapytain/resources/collections/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def export(self, output=Mimetypes.PYTHON.ETREE, domain="", **kwargs):
if isinstance(parent, cts.CTSCollection) and hasattr(parent, "metadata"):
complete_metadata = complete_metadata + parent.metadata
return text.CitableText(urn=self.urn, citation=self.citation, metadata=complete_metadata, **kwargs)
return self.default_export(output, domain)
return super(Text, self).export(output, domain)

def __findCitations(self, xml, xpath="ti:citation"):
""" Find citation in current xml. Used as a loop for self.xmlparser()
Expand Down Expand Up @@ -356,7 +356,7 @@ def export(self, output=Mimetypes.PYTHON.ETREE, domain=""):
"""
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)
return super(Work, self).export(output, domain)

def parse(self, resource):
""" Parse a resource
Expand Down Expand Up @@ -443,7 +443,7 @@ def export(self, output=Mimetypes.PYTHON.ETREE, domain=""):
"""
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)
return super(TextGroup, self).export(output, domain)

def parse(self, resource):
""" Parse a resource
Expand Down Expand Up @@ -477,20 +477,6 @@ class TextInventory(cts.TextInventory):
.. automethod:: __str__
"""

def export(self, output=Mimetypes.PYTHON.ETREE, domain=""):
""" Create a {format} version of the Work
:param output: Format to be chosen (Only XML for now)
:type output: basestring
:param domain: Domain to prefix IDs
:type domain: str
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)

def __init__(self, **kwargs):
super(TextInventory, self).__init__(**kwargs)

Expand Down Expand Up @@ -523,7 +509,7 @@ def export(self, output=Mimetypes.PYTHON.ETREE, domain=""):
"""
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)
return super(TextInventory, self).export(output, domain)

def parse(self, resource):
""" Parse a resource
Expand Down
7 changes: 7 additions & 0 deletions MyCapytain/resources/prototypes/cts/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class Text(CTSCollection):

DC_TITLE = "label"

@property
def TEXT_URI(self):
return RDF_PREFIX["cts"] + self.subtype

def __init__(self, resource=None, urn=None, parents=None, subtype="Edition"):
super(Text, self).__init__()
self.resource = None
Expand Down Expand Up @@ -235,6 +239,7 @@ class Work(CTSCollection):
"""

DC_TITLE_KEY = "title"
TYPE_URI = RDF_PREFIX["cts"] + "Work"

def __init__(self, resource=None, urn=None, parents=None):
super(Work, self).__init__()
Expand Down Expand Up @@ -326,6 +331,7 @@ class TextGroup(CTSCollection):
:type parents: Tuple.<TextInventory>
"""
DC_TITLE_KEY = "groupname"
TYPE_URI = RDF_PREFIX["cts"] + "TextGroup"

@property
def members(self):
Expand Down Expand Up @@ -397,6 +403,7 @@ class TextInventory(CTSCollection):
:param id: Identifier of the TextInventory
:type id: str
"""
TYPE_URI = RDF_PREFIX["cts"] + "TextInventory"

@property
def members(self):
Expand Down
61 changes: 43 additions & 18 deletions MyCapytain/resources/prototypes/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Collection(object):
:type DC_TITLE_KEY: str
"""
DC_TITLE_KEY = None
TYPE_URI = "http://w3id.org/dts-ontology/collection"

@property
def title(self):
Expand All @@ -33,7 +34,8 @@ def __init__(self):
self.metadata = Metadata()
self.__id__ = None
self.properties = {
RDF_PREFIX["dts"]+":model": "http://w3id.org/dts-ontology/collection"
RDF_PREFIX["dts"]+"model": "http://w3id.org/dts-ontology/collection",
RDF_PREFIX["rdf"]+"type": self.TYPE_URI
}
self.parents = []

Expand Down Expand Up @@ -80,7 +82,7 @@ def readableDescendants(self):
"""
return [member for member in self.descendants if member.readable]

def default_export(self, output=Mimetypes.JSON.DTS, domain=""):
def default_export(self, output=Mimetypes.JSON.DTS.Std, domain=""):
""" Export the collection item in the Mimetype required
:param output: Mimetype to export to (Uses MyCapytain.common.utils.Mimetypes)
Expand All @@ -89,21 +91,7 @@ def default_export(self, output=Mimetypes.JSON.DTS, domain=""):
:type domain: str
:return: Object using a different representation
"""
if output == Mimetypes.JSON.DTS:
if self.title:
m = self.metadata + self.title
else:
m = self.metadata
o = {
"@id": domain+self.id,
RDF_PREFIX["dts"] + "description": m.export(Mimetypes.JSON.DTS),
RDF_PREFIX["dts"] + "properties" : self.properties
}
if len(self.members):
o[RDF_PREFIX["dts"] + "members"] = [
member.export(Mimetypes.JSON.DTS, domain) for member in self.members
]
return o
raise NotImplementedError

def export(self, output=None, domain=""):
""" Export the collection item in the Mimetype required.
Expand All @@ -116,4 +104,41 @@ def export(self, output=None, domain=""):
:type domain: str
:return: Object using a different representation
"""
return self.default_export(output, domain)
if output == Mimetypes.JSON.DTS.Std or output == Mimetypes.JSON.DTS.NoParents:
identifier = self.id
if self.id is None:
identifier = ""
if self.title:
m = self.metadata + self.title
else:
m = self.metadata
o = {
"@id": domain+identifier,
RDF_PREFIX["dts"] + "description": m.export(Mimetypes.JSON.DTS.Std),
RDF_PREFIX["dts"] + "properties": self.properties,
RDF_PREFIX["dts"] + "capabilities": {
RDF_PREFIX["dts"] + "ordered": False,
RDF_PREFIX["dts"] + "supportsRole": False,
RDF_PREFIX["dts"] + "static": True,
RDF_PREFIX["dts"] + "navigation": {
RDF_PREFIX["dts"] + "parents": [],
RDF_PREFIX["dts"] + "siblings": {}
}
},
}
if len(self.members):
o[RDF_PREFIX["dts"] + "members"] = [
member.export(Mimetypes.JSON.DTS.NoParents, domain) for member in self.members
]
if output != Mimetypes.JSON.DTS.NoParents and len(self.parents):
o[RDF_PREFIX["dts"] + "capabilities"]\
[RDF_PREFIX["dts"] + "navigation"]\
[RDF_PREFIX["dts"] + "parents"] = [
{
"@id": domain+(parent.id or ""),
RDF_PREFIX["rdf"] + "type": parent.TYPE_URI,
RDF_PREFIX["dts"] + "model": "http://w3id.org/dts-ontology/collection",
}
for parent in self.parents
]
return o
49 changes: 49 additions & 0 deletions tests/resolvers/cts/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
GET_VALID_REFF = xmlparser(f)
with open("tests/testing_data/cts/getCapabilities.xml") as f:
GET_CAPABILITIES = xmlparser(f)
with open("tests/testing_data/cts/getCapabilities1294002.xml") as f:
GET_CAPABILITIES_FILTERED = xmlparser(f)


class TestHttpCTSResolver(TestCase):
Expand Down Expand Up @@ -319,9 +321,56 @@ def test_getMetadata_full(self):
).xpath("//ti:edition[@urn='urn:cts:latinLit:phi1294.phi002.perseus-lat2']", namespaces=NS)), 1,
"There should be one node in exported format corresponding to lat2"
)
self.assertCountEqual(
[x["@id"] for x in metadata.export(output=Mimetypes.JSON.DTS.Std)["http://w3id.org/dts-ontology/members"]],
["urn:cts:latinLit:phi1294", "urn:cts:latinLit:phi0959", "urn:cts:greekLit:tlg0003", "urn:cts:latinLit:phi1276"],
"There should be 4 Members in DTS JSON"
)

def test_getMetadata_subset(self):
""" Checks retrieval of Metadata information """
self.resolver.endpoint.getCapabilities = MagicMock(return_value=GET_CAPABILITIES_FILTERED)
metadata = self.resolver.getMetadata(objectId="urn:cts:latinLit:phi1294.phi002")
self.resolver.endpoint.getCapabilities.assert_called_with(urn="urn:cts:latinLit:phi1294.phi002")
self.assertIsInstance(
metadata, Collection,
"Resolver should return a collection object"
)
self.assertIsInstance(
metadata.members[0], Text,
"Members of Inventory should be TextGroups"
)
self.assertEqual(
len(metadata.descendants), 2,
"There should be as many descendants as there is edition, translation"
)
self.assertEqual(
len(metadata.readableDescendants), 2,
"There should be 1 edition + 1 translation in readableDescendants"
)
self.assertEqual(
len([x for x in metadata.readableDescendants if isinstance(x, Text)]), 2,
"There should be 1 edition + 1 translation in readableDescendants"
)
self.assertEqual(
len(metadata.export(
output=Mimetypes.PYTHON.ETREE
).xpath("//ti:edition[@urn='urn:cts:latinLit:phi1294.phi002.perseus-lat2']", namespaces=NS)), 1,
"There should be one node in exported format corresponding to lat2"
)
self.assertCountEqual(
[x["@id"] for x in metadata.export(output=Mimetypes.JSON.DTS.Std)["http://w3id.org/dts-ontology/members"]],
["urn:cts:latinLit:phi1294.phi002.perseus-lat2", "urn:cts:latinLit:phi1294.phi002.perseus-eng2"],
"There should be one member in DTS JSON"
)
self.assertCountEqual(
[
x["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]
for x in metadata.export(output=Mimetypes.JSON.DTS.Std)\
["http://w3id.org/dts-ontology/capabilities"]\
["http://w3id.org/dts-ontology/navigation"]\
["http://w3id.org/dts-ontology/parents"]
],
["http://chs.harvard.edu/xmlns/cts/TextGroup", "http://chs.harvard.edu/xmlns/cts/TextInventory"],
"There should be one member in DTS JSON"
)
Loading

0 comments on commit 9454273

Please sign in to comment.