Skip to content

Commit

Permalink
2.0.0b18 (#119)
Browse files Browse the repository at this point in the history
- Made GRAPH a global 'private' and changed its access with function. [ #117 and Capitains/Nautilus]
- Fixed dependency on local variable for .texts on local resolver [ #116 ]
- Fixed a bug where translation from GetMetadata would not get the text lang transmitted [ #118 ]
  • Loading branch information
PonteIneptique committed Mar 6, 2017
1 parent fb865b2 commit e0bad2a
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 35 deletions.
2 changes: 1 addition & 1 deletion MyCapytain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""

__version__ = "2.0.0b17"
__version__ = "2.0.0b18"
25 changes: 19 additions & 6 deletions MyCapytain/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


from inspect import getmro
from rdflib import Namespace, Graph
from rdflib.namespace import SKOS
Expand Down Expand Up @@ -186,14 +188,25 @@ def export(self, output=None, **kwargs):
)


GRAPH = Graph()
GRAPH.bind("", NAMESPACES.CTS)
GRAPH.bind("dts", NAMESPACES.DTS)
GRAPH.bind("tei", NAMESPACES.TEI)
GRAPH.bind("skos", SKOS)
global __MYCAPYTAIN_TRIPLE_GRAPH__
__MYCAPYTAIN_TRIPLE_GRAPH__ = Graph()
__MYCAPYTAIN_TRIPLE_GRAPH__.bind("", NAMESPACES.CTS)
__MYCAPYTAIN_TRIPLE_GRAPH__.bind("dts", NAMESPACES.DTS)
__MYCAPYTAIN_TRIPLE_GRAPH__.bind("tei", NAMESPACES.TEI)
__MYCAPYTAIN_TRIPLE_GRAPH__.bind("skos", SKOS)


def set_graph(graph):
global __MYCAPYTAIN_TRIPLE_GRAPH__
__MYCAPYTAIN_TRIPLE_GRAPH__ = graph


def get_graph():
return __MYCAPYTAIN_TRIPLE_GRAPH__

RDFLIB_MAPPING = {
Mimetypes.XML.RDF: "xml",
Mimetypes.JSON.LD: "json-ld",
Mimetypes.JSON.DTS.Std: "json-ld"
}
}

10 changes: 5 additions & 5 deletions MyCapytain/common/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
from __future__ import unicode_literals
from MyCapytain.common.constants import Mimetypes, Exportable, GRAPH
from MyCapytain.common.constants import Mimetypes, Exportable, get_graph
from rdflib import BNode, Literal, Graph


Expand All @@ -27,7 +27,7 @@ class Metadata(Exportable):

def __init__(self, node=None, *args, **kwargs):
super(Metadata, self).__init__(*args, **kwargs)
self.__graph__ = GRAPH
self.__graph__ = get_graph()
if node is not None:
self.__node__ = node
else:
Expand Down Expand Up @@ -103,7 +103,7 @@ def __export__(self, output=Mimetypes.JSON.Std, **kwargs):
:return: Formatted Export
"""
graph = Graph()
graph.namespace_manager = GRAPH.namespace_manager
graph.namespace_manager = get_graph().namespace_manager
for predicate, object in self.graph[self.asNode()]:
graph.add((self.asNode(), predicate, object))

Expand Down Expand Up @@ -138,6 +138,6 @@ def getOr(subject, predicate, *args, **kwargs):
:rtype: Metadata
"""
if (subject, predicate, None) in GRAPH:
return Metadata(node=GRAPH.objects(subject, predicate).__next__())
if (subject, predicate, None) in get_graph():
return Metadata(node=get_graph().objects(subject, predicate).__next__())
return Metadata(*args, **kwargs)
4 changes: 2 additions & 2 deletions MyCapytain/common/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from copy import copy
import re
from lxml.etree import _Element
from MyCapytain.common.constants import NS, Exportable, Mimetypes, GRAPH, NAMESPACES
from MyCapytain.common.constants import NS, Exportable, Mimetypes, get_graph, NAMESPACES
from MyCapytain.common.utils import make_xml_node

REFSDECL_SPLITTER = re.compile(r"/+[*()|\sa-zA-Z0-9:\[\]@=\\{$'\".\s]+")
Expand Down Expand Up @@ -885,7 +885,7 @@ def __export__(self, output=None, **kwargs):
label = self.name

return make_xml_node(
GRAPH, NAMESPACES.CTS.citation, attributes={
get_graph(), NAMESPACES.CTS.citation, attributes={
"xpath": re.sub(Citation.escape, "'", self.xpath),
"scope": re.sub(Citation.escape, "'", self.scope),
"label": label
Expand Down
9 changes: 4 additions & 5 deletions MyCapytain/resolvers/cts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def inventory(self):

@property
def texts(self):
return self.__texts__
return self.inventory.readableDescendants

def __init__(self, resource, name=None, logger=None, dispatcher=None):
""" Initiate the XMLResolver
Expand All @@ -60,7 +60,6 @@ def __init__(self, resource, name=None, logger=None, dispatcher=None):
else:
self.dispatcher = dispatcher
self.__inventory__ = self.dispatcher.collection
self.__texts__ = []
self.name = name

self.logger = logger
Expand All @@ -83,9 +82,9 @@ def xmlparse(self, file):
return xmlparser(file)

def parse(self, resource):
""" Parse a list of directories ans
""" Parse a list of directories and reades it into a collection
:param resource: List of folders
:param cache: Auto cache the results
:return: An inventory resource and a list of PrototypeText metadata-objects
"""
for folder in resource:
Expand Down Expand Up @@ -307,7 +306,7 @@ def getMetadata(self, objectId=None, **filters):
x = Edition(urn=txt_urn, parent=inventory.textgroups[tg_urn].works[wk_urn])
x.citation = text.citation
elif isinstance(text, Translation):
x = Translation(urn=txt_urn, parent=inventory.textgroups[tg_urn].works[wk_urn])
x = Translation(urn=txt_urn, parent=inventory.textgroups[tg_urn].works[wk_urn], lang=text.lang)
x.citation = text.citation

return inventory[objectId]
Expand Down
9 changes: 7 additions & 2 deletions MyCapytain/resources/prototypes/cts/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,21 @@ class PrototypeText(ResourceCollection, PrototypeCTSCollection):
CTS_PROPERTIES = [NAMESPACES.CTS.label, NAMESPACES.CTS.description]
SUBTYPE = "unknown"

def __init__(self, urn="", parent=None):
def __init__(self, urn="", parent=None, lang=None):
self.__subtype__ = self.SUBTYPE
super(PrototypeText, self).__init__(identifier=str(urn))
self.resource = None
self.citation = None
self.__urn__ = URN(urn)
self.docname = None
self.validate = None
if lang is not None:
self.lang = lang

if parent is not None:
self.parent = parent
self.lang = self.parent.lang
if lang is None:
self.lang = self.parent.lang

@property
def subtype(self):
Expand Down Expand Up @@ -319,6 +322,8 @@ class PrototypeTranslation(PrototypeText):
:type urn: str
:param parent: Parent of current item
:type parent: PrototypeWork
:param lang: Language of the translation
:type lang: Lang
"""
TYPE_URI = NAMESPACES.CTS.term("translation")
MODEL_URI = URIRef(NAMESPACES.DTS.resource)
Expand Down
6 changes: 3 additions & 3 deletions MyCapytain/resources/prototypes/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from MyCapytain.common.metadata import Metadata
from MyCapytain.errors import UnknownCollection
from MyCapytain.common.utils import Subgraph, LiteralToDict
from MyCapytain.common.constants import NAMESPACES, RDFLIB_MAPPING, Mimetypes, Exportable, GRAPH
from MyCapytain.common.constants import NAMESPACES, RDFLIB_MAPPING, Mimetypes, Exportable, get_graph
from rdflib import URIRef, RDF, Literal, Graph, RDFS
from rdflib.namespace import SKOS, DC

Expand All @@ -31,7 +31,7 @@ class Collection(Exportable):

def __init__(self, identifier="", *args, **kwargs):
super(Collection, self).__init__(identifier, *args, **kwargs)
self.__graph__ = GRAPH
self.__graph__ = get_graph()

self.__node__ = URIRef(identifier)
self.__metadata__ = Metadata.getOr(self.__node__, NAMESPACES.DTS.metadata)
Expand Down Expand Up @@ -305,7 +305,7 @@ def __export__(self, output=None, domain=""):

RDFSLabel = self.graph.qname(RDFS.label)
RDFType = self.graph.qname(RDF.type)
store = Subgraph(GRAPH.namespace_manager)
store = Subgraph(get_graph().namespace_manager)
store.graphiter(self.graph, self.metadata, ascendants=0, descendants=1)
metadata = {}
for _, predicate, obj in store.graph:
Expand Down
4 changes: 2 additions & 2 deletions MyCapytain/resources/prototypes/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rdflib import BNode, URIRef
from MyCapytain.common.reference import URN, Citation, NodeId
from MyCapytain.common.metadata import Metadata
from MyCapytain.common.constants import Mimetypes, Exportable, GRAPH, NAMESPACES
from MyCapytain.common.constants import Mimetypes, Exportable, get_graph, NAMESPACES
from MyCapytain.resources.prototypes.metadata import Collection


Expand All @@ -30,7 +30,7 @@ class TextualElement(Exportable):
default_exclude = []

def __init__(self, identifier=None, metadata=None):
self.__graph__ = GRAPH
self.__graph__ = get_graph()
self.__identifier__ = identifier

self.__node__ = BNode()
Expand Down
14 changes: 9 additions & 5 deletions tests/resolvers/cts/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from __future__ import unicode_literals

from MyCapytain.resolvers.cts.local import CTSCapitainsLocalResolver
from MyCapytain.common.constants import NS, Mimetypes, NAMESPACES, GRAPH
from MyCapytain.common.constants import NS, Mimetypes, NAMESPACES, get_graph
from MyCapytain.common.constants import NS, Mimetypes, NAMESPACES, get_graph
from MyCapytain.common.reference import URN, Reference
from MyCapytain.errors import InvalidURN, UnknownObjectError, UndispatchedTextError
from MyCapytain.resources.prototypes.metadata import Collection
Expand Down Expand Up @@ -150,7 +151,7 @@ def test_pagination(self):
class TextXMLFolderResolver(TestCase):
""" Ensure working state of resolver """
def setUp(self):
GRAPH.remove((None, None, None))
get_graph().remove((None, None, None))
self.resolver = CTSCapitainsLocalResolver(["./tests/testing_data/latinLit2"])

def test_getPassage_full(self):
Expand Down Expand Up @@ -489,6 +490,9 @@ def test_getMetadata_subset(self):
self.assertIsInstance(
tr, PrototypeTranslation, "Metadata should be translation"
)
self.assertEqual(
tr.lang, "fre", "Language is French"
)
self.assertIn(
"Histoire de la Guerre du Péloponnése",
tr.get_description("eng"),
Expand Down Expand Up @@ -574,7 +578,7 @@ def test_getReffs_full(self):
class TextXMLFolderResolverDispatcher(TestCase):
""" Ensure working state of resolver """
def setUp(self):
GRAPH.remove((None, None, None))
get_graph().remove((None, None, None))

def test_dispatching_latin_greek(self):
tic = TextInventoryCollection()
Expand Down Expand Up @@ -706,7 +710,7 @@ def dispatchGreekLit(collection, path=None, **kwargs):
latin_stuff = resolver.getMetadata("urn:perseus:latinLit").export(Mimetypes.XML.CTS)
greek_stuff = resolver.getMetadata("urn:perseus:greekLit").export(Mimetypes.XML.CTS)
farsi_stuff = resolver.getMetadata("urn:perseus:farsiLit").export(Mimetypes.XML.CTS)
GRAPH.remove((None, None, None))
get_graph().remove((None, None, None))
latin_stuff, greek_stuff, farsi_stuff = TextInventory.parse(latin_stuff), TextInventory.parse(greek_stuff),\
TextInventory.parse(farsi_stuff)
self.assertEqual(
Expand All @@ -728,7 +732,7 @@ def dispatchGreekLit(collection, path=None, **kwargs):
greek_stuff.get_label("fre"), None, # Text inventory have no label in CTS
"Label should be correct"
)
GRAPH.remove((None, None, None))
get_graph().remove((None, None, None))
all = TextInventory.parse(all)
self.assertEqual(
len(all.readableDescendants), 25,
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/collections/test_cts.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class TestXMLImplementation(unittest.TestCase, xmlunittest.XmlTestMixin):
""" Test XML Implementation of resources Endpoint request making """

def setUp(self):
constants.GRAPH.remove((None, None, None))
constants.__MYCAPYTAIN_TRIPLE_GRAPH__.remove((None, None, None))

self.getCapabilities = open("tests/testing_data/cts/getCapabilities.xml", "r")
self.ed = """<ti:edition urn='urn:cts:latinLit:phi1294.phi002.perseus-lat2' workUrn='urn:cts:latinLit:phi1294.phi002' xmlns:ti='http://chs.harvard.edu/xmlns/cts'>
Expand Down Expand Up @@ -286,7 +286,7 @@ def test_Inventory_pickle(self):
del TI

self.assertEqual(
len(list(constants.GRAPH.triples((None, None, None)))),
len(list(constants.__MYCAPYTAIN_TRIPLE_GRAPH__.triples((None, None, None)))),
0,
"There should be 0 metadata node for the child 1294 because everything is gone"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest import TestCase
from MyCapytain.resources.collections.cts import TextInventory, TextGroup, Work, Edition
from MyCapytain.resources.collections.cts import TextInventory, TextGroup, Work, Edition, Translation
from MyCapytain.resources.prototypes.cts.inventory import PrototypeTextGroup

with open("tests/testing_data/examples/getcapabilities.seneca.xml") as f:
SENECA = f.read()
Expand Down Expand Up @@ -33,3 +34,9 @@ def test_title(self):
"de Otio Sapientis, Moral essays Vol 2"],
"Title should be computed correctly : default should be set"
)

def test_new_object(self):
""" When creating an object with same urn, we should retrieve the same metadata"""
TI = TextInventory.parse(resource=SENECA)
a = TI["urn:cts:latinLit:stoa0255.stoa012.perseus-lat2"].metadata
b = (PrototypeTextGroup("urn:cts:latinLit:stoa0255")).metadata
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

</ti:translation>

<ti:translation projid="greekLit:opp-fre1" workUrn="urn:cts:greekLit:tlg0003.tlg001" urn="urn:cts:greekLit:tlg0003.tlg001.opp-fre1">
<ti:translation xml:lang="fre" projid="greekLit:opp-fre1" workUrn="urn:cts:greekLit:tlg0003.tlg001" urn="urn:cts:greekLit:tlg0003.tlg001.opp-fre1">

<ti:label xml:lang="en">The Peloponnesian War (French. Marie Charles Zévort
translator.)</ti:label>
Expand Down

0 comments on commit e0bad2a

Please sign in to comment.