Skip to content

Commit

Permalink
More tests for Resolver
Browse files Browse the repository at this point in the history
- Upgrade MimeType for more flexibility
- Added descendant, readable and readableDescendant properties
  • Loading branch information
PonteIneptique committed Dec 5, 2016
1 parent 51f2ea6 commit a8a9049
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 53 deletions.
8 changes: 4 additions & 4 deletions MyCapytain/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ def keys(self):
"""
return self.__keys

def export(self, mime=Mimetypes.JSON):
if mime == Mimetypes.JSON:
def export(self, mime=Mimetypes.JSON.Std):
if mime == Mimetypes.JSON.Std:
return {
key: getattr(value, "__getstate__")() for key, value in self.metadata.items()
}
elif mime == Mimetypes.JSON_DTS:
elif mime == Mimetypes.JSON.DTS:
descs = {

}
Expand All @@ -397,7 +397,7 @@ def export(self, mime=Mimetypes.JSON):
descs[lang][ns+k] = value
return [value for value in descs.values()]

elif mime == Mimetypes.RDFXML:
elif mime == Mimetypes.XML.RDF:
out = ""
for key in sorted(self.metadata.keys()):
metadatum = self.metadata[key]
Expand Down
30 changes: 19 additions & 11 deletions MyCapytain/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,22 @@ class Mimetypes:
:cvar MY_CAPYTAIN: MyCapytain Object Resource (Native Python CapiTainS Object)
"""
JSON = "application/text"
JSON_CTS = "application/ld+json:CTS"
JSON_DTS = "application/ld+json:DTS"
XML = "text/xml"
RDFXML = "application/rdf+xml"
CTS_XML = "text/xml:CTS"
MY_CAPYTAIN = "MyCapytain"
ETREE = "python/lxml"
MyCapytainText = "MyCapytain/Text"
PLAINTEXT = "text/plain"
NestedDict = "python/NestedDict"

class JSON:
Std = "application/text"
CTS = "application/ld+json:CTS"
DTS = "application/ld+json:DTS"

class XML:
Std = "text/xml"
RDF = "application/rdf+xml"
CTS = "text/xml:CTS"

class PYTHON:
NestedDict = "python/NestedDict"
ETREE = "python/lxml"

class MyCapytain:
ReadableText = "Capitains/ReadableText"

PLAINTEXT = "text/plain"
33 changes: 22 additions & 11 deletions MyCapytain/resources/collections/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __str__(self):
strings.append("</ti:{0}>".format(tag_end))
return "".join(strings)

def export(self, output=Mimetypes.ETREE, domain="", **kwargs):
def export(self, output=Mimetypes.PYTHON.ETREE, domain="", **kwargs):
""" Create a {format} version of the Work
:param output: Format to be chosen (Only XML for now)
Expand All @@ -214,9 +214,9 @@ def export(self, output=Mimetypes.ETREE, domain="", **kwargs):
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
elif output == Mimetypes.MyCapytainText:
elif output == Mimetypes.PYTHON.MyCapytain.ReadableText:
complete_metadata = self.metadata
for parent in self.parents:
if isinstance(parent, cts.CTSCollection) and hasattr(parent, "metadata"):
Expand Down Expand Up @@ -274,6 +274,17 @@ def parse(self, resource):

return None

@property
def readable(self):
""" Readable property should return elements where the element can be queried for getPassage / getReffs
"""
return True

@property
def descendants(self):
return []



def Edition(resource=None, urn=None, parents=None):
""" Create an edition subtyped Text object
Expand Down Expand Up @@ -333,7 +344,7 @@ def __str__(self):
strings.append("</ti:work>")
return "".join(strings)

def export(self, output=Mimetypes.ETREE, domain=""):
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)
Expand All @@ -343,7 +354,7 @@ def export(self, output=Mimetypes.ETREE, domain=""):
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)

Expand Down Expand Up @@ -420,7 +431,7 @@ def __str__(self):
strings.append("</ti:textgroup>")
return "".join(strings)

def export(self, output=Mimetypes.ETREE, domain=""):
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)
Expand All @@ -430,7 +441,7 @@ def export(self, output=Mimetypes.ETREE, domain=""):
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)

Expand Down Expand Up @@ -466,7 +477,7 @@ class TextInventory(cts.TextInventory):
.. automethod:: __str__
"""

def export(self, output=Mimetypes.ETREE, domain=""):
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)
Expand All @@ -476,7 +487,7 @@ def export(self, output=Mimetypes.ETREE, domain=""):
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)

Expand All @@ -500,7 +511,7 @@ def __str__(self):
strings.append("</ti:TextInventory>")
return "".join(strings)

def export(self, output=Mimetypes.ETREE, domain=""):
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)
Expand All @@ -510,7 +521,7 @@ def export(self, output=Mimetypes.ETREE, domain=""):
:rtype: lxml.etree._Element
:returns: XML representation of the object
"""
if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
return xmlparser(str(self))
return self.default_export(output, domain)

Expand Down
22 changes: 19 additions & 3 deletions MyCapytain/resources/prototypes/cts/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ def __init__(self, resource=None, urn=None, parents=None, subtype="Edition"):
if resource is not None:
self.setResource(resource)

@property
def readable(self):
return True

@property
def members(self):
return []

@property
def descendants(self):
return []

def translations(self, key=None):
""" Get translations in given language
Expand Down Expand Up @@ -243,6 +255,10 @@ def __init__(self, resource=None, urn=None, parents=None):
if resource is not None:
self.setResource(resource)

@property
def readable(self):
return True

def update(self, other):
""" Merge two Work Objects.
Expand Down Expand Up @@ -293,7 +309,7 @@ def __len__(self):

@property
def members(self):
return self.texts.values()
return list(self.texts.values())


class TextGroup(CTSCollection):
Expand All @@ -313,7 +329,7 @@ class TextGroup(CTSCollection):

@property
def members(self):
return self.works.values()
return list(self.works.values())

def __init__(self, resource=None, urn=None, parents=None):
super(TextGroup, self).__init__()
Expand Down Expand Up @@ -384,7 +400,7 @@ class TextInventory(CTSCollection):

@property
def members(self):
return self.textgroups.values()
return list(self.textgroups.values())

def __init__(self, resource=None, name=None):
super(TextInventory, self).__init__()
Expand Down
31 changes: 27 additions & 4 deletions MyCapytain/resources/prototypes/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def id(self):
"""
return self.__id__

@property
def readable(self):
""" Readable property should return elements where the element can be queried for getPassage / getReffs
"""
return False

@id.setter
def id(self, value):
self.__id__ = value
Expand All @@ -57,7 +63,24 @@ def members(self):
"""
return []

def default_export(self, output=Mimetypes.JSON_DTS, domain=""):
@property
def descendants(self):
""" Any descendant (no max level) of the collection's item
:rtype: [Collection]
"""
return self.members + \
[submember for member in self.members for submember in member.descendants]

@property
def readableDescendants(self):
""" List of element available which are readable
:rtype: [Collection]
"""
return [member for member in self.descendants if member.readable]

def default_export(self, output=Mimetypes.JSON.DTS, domain=""):
""" Export the collection item in the Mimetype required
:param output: Mimetype to export to (Uses MyCapytain.common.utils.Mimetypes)
Expand All @@ -66,19 +89,19 @@ def default_export(self, output=Mimetypes.JSON_DTS, domain=""):
:type domain: str
:return: Object using a different representation
"""
if output == Mimetypes.JSON_DTS:
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"] + "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
member.export(Mimetypes.JSON.DTS, domain) for member in self.members
]
return o

Expand Down
2 changes: 1 addition & 1 deletion MyCapytain/resources/prototypes/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def metadata(self, value):
else:
raise TypeError(".metadata should be an instance of Metadata")

def default_export(self, output=Mimetypes.JSON_DTS, exclude=None):
def default_export(self, output=Mimetypes.JSON.DTS, exclude=None):
""" Export the textual node item in the Mimetype required
:param output: Mimetype to export to (Uses MyCapytain.common.utils.Mimetypes)
Expand Down
4 changes: 3 additions & 1 deletion MyCapytain/resources/texts/api/cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def getPassagePlus(self, reference=None):
response = xmlparser(self.retriever.getPassagePlus(urn=urn))

self.__parse_request__(response.xpath("//ti:reply/ti:label", namespaces=NS)[0])
return Passage(urn=urn, resource=response, retriever=self.retriever)
passage = Passage(urn=urn, resource=response, retriever=self.retriever)
passage.metadata, passage.citation = self.metadata, self.citation
return passage

def __parse_request__(self, xml):
""" Parse a request with metadata information
Expand Down
10 changes: 5 additions & 5 deletions MyCapytain/resources/texts/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __str__(self):
:rtype: basestring
:returns: XML of the passage in string form
"""
return self.export(output=Mimetypes.XML)
return self.export(output=Mimetypes.XML.Std)

def export(self, output=Mimetypes.PLAINTEXT, exclude=None, _preformatted=False):
""" Text content of the passage
Expand Down Expand Up @@ -63,20 +63,20 @@ def export(self, output=Mimetypes.PLAINTEXT, exclude=None, _preformatted=False):
elif _preformatted is False:
exclude = ""

if output == Mimetypes.ETREE:
if output == Mimetypes.PYTHON.ETREE:
""" Exports the whole resource as a LXML object
"""
return self.resource

elif output == Mimetypes.XML:
elif output == Mimetypes.XML.Std:
""" Exports the whole resource formatted as XML but as string object
"""
return tostring(self.resource, encoding=str)

elif output == Mimetypes.NestedDict:
elif output == Mimetypes.PYTHON.NestedDict:
""" Exports the whole resource into a NestedDict
"""
reffs = self.getValidReff(level=len(self.citation))
reffs = self.getReffs(level=len(self.citation))
text = nested_ordered_dictionary()
for reff in reffs:
_r = reff.split(".")
Expand Down
6 changes: 3 additions & 3 deletions tests/common/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_export_json(self):

six.assertCountEqual(
self,
b.export(Mimetypes.JSON),
b.export(Mimetypes.JSON.Std),
{'dc:editor': {'default': 'eng', 'langs': [('eng', 'Captain Hook'), ('fre', 'Capitaine Crochet')],
'name': 'dc:editor'},
'title': {'default': 'eng', 'langs': [('eng', 'ttl'), ('fre', 'titre')], 'name': 'title'},
Expand All @@ -237,7 +237,7 @@ def test_export_jsonld(self):

six.assertCountEqual(
self,
b.export(Mimetypes.JSON_DTS),
b.export(Mimetypes.JSON.DTS),
[
{
'http://chs.harvard.edu/xmlns/cts/desc': 'Omelette',
Expand All @@ -261,7 +261,7 @@ def test_export_xmlRDF(self):
m5 = Metadatum("dc:editor", [("eng", "Captain Hook"), ("fre", "Capitaine Crochet")])
b[("desc", "title", "dc:editor")] = (m3, m4, m5)
self.assertEqual(
b.export(Mimetypes.RDFXML),
b.export(Mimetypes.XML.RDF),
"""<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description>
<editor xmlns="http://purl.org/dc/elements/1.1//" xml:lang="eng">Captain Hook</editor><editor xmlns="http://purl.org/dc/elements/1.1//" xml:lang="fre">Capitaine Crochet</editor><desc xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="fre">Omelette</desc><title xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="eng">ttl</title><title xmlns="http://chs.harvard.edu/xmlns/cts/" xml:lang="fre">titre</title>
Expand Down

0 comments on commit a8a9049

Please sign in to comment.