Skip to content

Commit

Permalink
Merge pull request #131 from Capitains/dev
Browse files Browse the repository at this point in the history
Nemo 2.0.0
  • Loading branch information
sonofmun authored Oct 25, 2019
2 parents 313384a + 982df8e commit 9497ac7
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ deploy:
password: $PYPASS
on:
tags: true
python: "3.5"
python: "3.5"
7 changes: 7 additions & 0 deletions CHANGES.mD
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change logs
===

## 2.0.0 - 22/10/2019

By @sonofmun

- Minor changes to make Nemo compatible with MyCapytain 3.0.0
- This version is no longer compatible with earlier versions of MyCapytain. Please upgrade to MyCapytain 3.0.0.

## 1.0.4 - 22/10/2019

By @ponteineptique
Expand Down
4 changes: 3 additions & 1 deletion flask_nemo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from urllib.parse import quote
from operator import itemgetter
from warnings import warn
from collections import Callable, OrderedDict
from collections import OrderedDict
from collections.abc import Callable
from copy import deepcopy as copy
from pkg_resources import resource_filename

Expand Down Expand Up @@ -498,6 +499,7 @@ def expose_ancestors_or_children(self, member, collection, lang=None):
x = {
"id": member.id,
"label": str(member.get_label(lang)),
"is_resource": isinstance(member, ResourceCollection),
"model": str(member.model),
"type": str(member.type),
"size": member.size,
Expand Down
10 changes: 5 additions & 5 deletions flask_nemo/chunker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def default_chunker(text, getreffs):
:rtype: [(str, str)]
"""
level = len(text.citation)
return [tuple([reff.split(":")[-1]]*2) for reff in getreffs(level=level)]
return [tuple([str(reff)]*2) for reff in getreffs(level=level)]


def scheme_chunker(text, getreffs):
Expand All @@ -34,7 +34,7 @@ def scheme_chunker(text, getreffs):
level = 2
elif types == ["book", "line"]:
return line_chunker(text, getreffs)
return [tuple([reff.split(":")[-1]]*2) for reff in getreffs(level=level)]
return [tuple([str(reff)]*2) for reff in getreffs(level=level)]


def line_chunker(text, getreffs, lines=30):
Expand All @@ -50,7 +50,7 @@ def line_chunker(text, getreffs, lines=30):
:rtype: [(str, str)]
"""
level = len(text.citation)
source_reffs = [reff.split(":")[-1] for reff in getreffs(level=level)]
source_reffs = [str(reff) for reff in getreffs(level=level)]
reffs = []
i = 0
while i + lines - 1 < len(source_reffs):
Expand All @@ -72,7 +72,7 @@ def level_chunker(text, getreffs, level=1):
:rtype: [(str, str)]
"""
references = getreffs(level=level)
return [(ref.split(":")[-1], ref.split(":")[-1]) for ref in references]
return [(str(ref), str(ref)) for ref in references]


def level_grouper(text, getreffs, level=None, groupby=20):
Expand All @@ -87,7 +87,7 @@ def level_grouper(text, getreffs, level=None, groupby=20):
if level is None or level > len(text.citation):
level = len(text.citation)

references = [ref.split(":")[-1] for ref in getreffs(level=level)]
references = [str(ref) for ref in getreffs(level=level)]
_refs = OrderedDict()

for key in references:
Expand Down
2 changes: 1 addition & 1 deletion flask_nemo/data/templates/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1>
<section class="card-columns">
<ul>
{% for coll in collections.members %}
{% if coll.model == 'http://w3id.org/dts-ontology/resource' %}
{% if coll.is_resource %}
<li class="card">
[{{coll.lang}}] {{ coll.label }}<br />
<a class="card-link" href="{{url_for('.r_references_semantic', objectId=coll.id, semantic=coll.semantic)}}">Browse</a>
Expand Down
14 changes: 7 additions & 7 deletions flask_nemo/plugins/annotations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AnnotationsApiPlugin(PluginPrototype):

def __init__(self, queryinterface, *args, **kwargs):
super(AnnotationsApiPlugin, self).__init__(*args, **kwargs)
self.__queryinterface__ = queryinterface
self._queryinterface = queryinterface

# TODO we should have a response at the base of annotations/api that returns link types and link relations
# showing the next level of options
Expand Down Expand Up @@ -63,13 +63,13 @@ def r_annotations(self):
except ValueError:
return "invalid urn", 400

count, annotations = self.__queryinterface__.getAnnotations(urn, wildcard=wildcard, include=include,
exclude=exclude, limit=limit, start=start,
expand=expand)
count, annotations = self._queryinterface.getAnnotations(urn, wildcard=wildcard, include=include,
exclude=exclude, limit=limit, start=start,
expand=expand)
else:
# Note that this implementation is not done for too much annotations
# because we do not implement pagination here
count, annotations = self.__queryinterface__.getAnnotations(None, limit=limit, start=start, expand=expand)
count, annotations = self._queryinterface.getAnnotations(None, limit=limit, start=start, expand=expand)
mapped = []
response = {
"@context": type(self).JSONLD_CONTEXT,
Expand Down Expand Up @@ -102,7 +102,7 @@ def r_annotation(self, sha):
:return: annotation contents
:rtype: {str: Any}
"""
annotation = self.__queryinterface__.getResource(sha)
annotation = self._queryinterface.getResource(sha)
if not annotation:
return "invalid resource uri", 404
response = {
Expand All @@ -125,7 +125,7 @@ def r_annotation_body(self, sha):
:return: annotation contents
:rtype: {str: Any}
"""
annotation = self.__queryinterface__.getResource(sha)
annotation = self._queryinterface.getResource(sha)
if not annotation:
return "invalid resource uri", 404
# TODO this should inspect the annotation content
Expand Down
48 changes: 24 additions & 24 deletions flask_nemo/query/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ def __init__(self, objectId, subreference=None, **kwargs):
subreference = None
elif isinstance(objectId, tuple):
objectId, subreference = objectId
self.__objectId__ = objectId
self.__subreference__ = subreference
self._objectId = objectId
self._subreference = subreference

@property
def objectId(self):
return self.__objectId__
return self._objectId

@property
def subreference(self):
return self.__subreference__
return self._subreference

def to_json(self):
""" Method to call to get a serializable object for json.dump or jsonify based on the target
Expand Down Expand Up @@ -84,45 +84,45 @@ class AnnotationResource(object):
SLUG = "annotation"

def __init__(self, uri, target, type_uri, resolver, target_class=Target, mimetype=None, slug=None, **kwargs):
self.__uri__ = uri
self._uri = uri
if not isinstance(target, Target):
self.__target__ = target_class(target)
self._target = target_class(target)
else:
self.__target__ = target
self.__type_uri__ = type_uri
self.__slug__ = slug or deepcopy(type(self).SLUG)
self.__sha__ = hashlib.sha256(
self._target = target
self._type_uri = type_uri
self._slug = slug or deepcopy(type(self).SLUG)
self._sha = hashlib.sha256(
"{uri}::{type_uri}".format(uri=uri, type_uri=type_uri).encode('utf-8')
).hexdigest()

self.__content__ = None
self.__resolver__ = resolver
self.__retriever__ = None
self.__mimetype__ = mimetype
self._content = None
self._resolver = resolver
self._retriever = None
self._mimetype = mimetype

@property
def mimetype(self):
return self.__mimetype__
return self._mimetype

@property
def sha(self):
return self.__sha__
return self._sha

@property
def uri(self):
return self.__uri__
return self._uri

@property
def type_uri(self):
return self.__type_uri__
return self._type_uri

@property
def slug(self):
return self.__slug__
return self._slug

@property
def target(self):
return self.__target__
return self._target

@property
def expandable(self):
Expand All @@ -135,10 +135,10 @@ def read(self):
:return: the contents of the resource
:rtype: str or bytes or flask.response
"""
if not self.__content__:
self.__retriever__ = self.__resolver__.resolve(self.uri)
self.__content__, self.__mimetype__ = self.__retriever__.read(self.uri)
return self.__content__
if not self._content:
self._retriever = self._resolver.resolve(self.uri)
self._content, self._mimetype = self._retriever.read(self.uri)
return self._content

def expand(self):
""" Expand the contents of the Annotation if it is expandable (i.e. if it references multiple resources)
Expand Down
55 changes: 33 additions & 22 deletions flask_nemo/query/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from MyCapytain.common.reference import URN
from MyCapytain.common.reference import URN, BaseReferenceSet, BaseReference
from MyCapytain.errors import CitationDepthError
from flask_nemo.query.proto import QueryPrototype
from flask_nemo.query.annotation import AnnotationResource
from werkzeug.exceptions import NotFound
Expand All @@ -20,24 +21,28 @@ class SimpleQuery(QueryPrototype):
"""

# ToDo: We should probably make a real use (and not just test fixes)
# of BaseReferenceSet and BaseReference here. This seems silly
# that we are restringing stuff here.

def __init__(self, annotations, resolver=None):
super(SimpleQuery, self).__init__(None)
self.__annotations__ = []
self.__nemo__ = None
self.__resolver__ = resolver
self._annotations = []
self._nemo = None
self._resolver = resolver

for resource in annotations:
if isinstance(resource, tuple):
target, body, type_uri = resource
self.__annotations__.append(AnnotationResource(
body, target, type_uri, self.__resolver__
self._annotations.append(AnnotationResource(
body, target, type_uri, self._resolver
))
else:
self.__annotations__.append(resource)
self._annotations.append(resource)

@property
def textResolver(self):
return self.__nemo__.resolver
return self._nemo.resolver

def process(self, nemo):
""" Register nemo and parses annotations
Expand All @@ -46,16 +51,16 @@ def process(self, nemo):
:param nemo: Nemo
"""
self.__nemo__ = nemo
for annotation in self.__annotations__:
self._nemo = nemo
for annotation in self._annotations:
annotation.target.expanded = frozenset(
self.__getinnerreffs__(
self._getinnerreffs(
objectId=annotation.target.objectId,
subreference=annotation.target.subreference
)
)

def __get_resource_metadata__(self, objectId):
def _get_resource_metadata(self, objectId):
""" Return a metadata text object
:param objectId: objectId of the text
Expand All @@ -65,7 +70,7 @@ def __get_resource_metadata__(self, objectId):

@property
def annotations(self):
return self.__annotations__
return self._annotations

def getResource(self, sha):
try:
Expand All @@ -91,7 +96,7 @@ def getAnnotations(self, targets, wildcard=".", include=None, exclude=None, limi
else:
objectId, subreference = target, None

ref_in_range = list(self.__getinnerreffs__(
ref_in_range = list(self._getinnerreffs(
objectId=objectId,
subreference=subreference
))
Expand All @@ -109,7 +114,7 @@ def getAnnotations(self, targets, wildcard=".", include=None, exclude=None, limi

return len(annotations), sorted(annotations, key=lambda x: x.uri)

def __getinnerreffs__(self, objectId, subreference):
def _getinnerreffs(self, objectId, subreference) -> BaseReference:
""" Resolve the list of urns between in a range
:param text_metadata: Resource Metadata
Expand All @@ -121,15 +126,21 @@ def __getinnerreffs__(self, objectId, subreference):
"""
level = 0
yield subreference

while level > -1:
reffs = self.__nemo__.resolver.getReffs(
objectId,
subreference=subreference,
level=level
)
if len(reffs) == 0:
try:
# type == BaseReferenceSet. I removed the explicit typing below since it broke the tests on Python 3.5.
reffs = self._nemo.resolver.getReffs(
objectId,
subreference=subreference,
level=level
)
# This is the new behavior in MyCapytain 3.0.0
except CitationDepthError:
break
else:
for r in reffs:
yield r
# We only needs the start of the reference here,
# because we specifically want to drop ranges here.
yield r.start
level += 1
2 changes: 1 addition & 1 deletion flask_nemo/query/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class QueryPrototype(object):
MATCH_ALL = "%.%"

def __init__(self, getreffs, **kwargs):
self.__getreffs__ = getreffs
self._getreffs = getreffs

def getAnnotations(self, targets, wildcard=".", include=None, exclude=None, limit=None, start=1, expand=False,
**kwargs):
Expand Down
Loading

0 comments on commit 9497ac7

Please sign in to comment.