Skip to content

Commit

Permalink
Added basic retrievers, refactored a little
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed May 13, 2016
1 parent bb44006 commit 7eb6354
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 120 deletions.
4 changes: 2 additions & 2 deletions flask_nemo/__init__.py
Expand Up @@ -37,11 +37,11 @@ class Nemo(object):
:type api_url: str
:param retriever: CTS Retriever (Will be defaulted to api_url using cts5 retriever if necessary)
:type retriever: MyCapytain.retrievers.proto.CTS
:param base_url: Base URL to use when registering the endpoint
:param base_url: Base URL to use when registering the endpoint (It cannot be "/" only !)
:type base_url: str
:param cache: SQLITE cache file name
:type base_url: str
:param expire: TIme before expiration of cache, default 3600
:param expire: Time before expiration of cache, default 3600
:type expire: int
:param plugins: List of plugins to connect to the Nemo instance
:type plugins: list(flask_nemo.plugin.PluginPrototype)
Expand Down
42 changes: 14 additions & 28 deletions flask_nemo/query/annotation.py
@@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
from copy import deepcopy


class AnnotationResource(object):

""" AnnotationResource
Expand All @@ -12,21 +15,28 @@ class AnnotationResource(object):
:type resolver: AnnotationResolver
"""

SLUG="annotation"
SLUG = "annotation"

def __init__(self, uri, target, type_uri, resolver, **kwargs):
self.__uri__ = uri
self.__target__ = target
self.__type_uri__ = type_uri
self.__resolver__ = resolver
self.__slug__ = deepcopy(type(self).SLUG)

self.__content__ = None
self.__resolver__ = resolver
self.__retriever__ = None

def read(self):
""" Read the contents of the Annotation Resource
:return: the contents of the resource
:rtype: str
"""
return self.__resolver__.resolve(uri)
if not self.__content__:
self.__retriever__ = self.__resolver__.resolve(self.uri)
self.__content__ = self.__retriever__.read(self.uri)
return self.__content__

def expand(self):
""" Expand the contents of the Annotation if it is expandable
Expand All @@ -48,7 +58,7 @@ def type_uri(self):

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

@property
def expandable(self):
Expand All @@ -68,27 +78,3 @@ def __init__(self, urn, **kwargs):
@property
def urn(self):
return self.__urn__

class Resolver(object):

""" Prototype for a Resolver
:param retriever: Retriever(s) to use to resolve resources passed to this resolver
:type retriever: Retriever
"""


def __init__(self,*retrievers,**kwargs):
self.__retrievers__ = retrievers


def resolve(self,uri):
""" Resolve a Resource identified by URI
:param uri: The URI of the resource to be resolved
:type uri: str
:return: the contents of the resource as a string
:rtype: str
"""
for r in self.__retrievers__:
if r.match(uri):
return r.read(uri)
raise Exception
38 changes: 9 additions & 29 deletions flask_nemo/query/proto.py
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-


class QueryPrototype(object):
""" Prototype for Nemo Query API Implementations
:param name: The Name of this Query API
Expand All @@ -16,7 +18,12 @@ class QueryPrototype(object):
def __init__(self, getreffs, **kwargs):
self.__getreffs__ = getreffs

def getAnnotations(self, *urns, wildcard=".", include=None, exclude=None, limit=None, start=1, expand=False, **kwargs):
def getAnnotations(self,
*urns,
wildcard=".", include=None, exclude=None,
limit=None, start=1,
expand=False, **kwargs
):
""" Retrieve annotations from the query provider
:param urns: The CTS URN(s) to query as the target of annotations
:type urns: MyCapytain.common.reference.URN
Expand All @@ -42,32 +49,5 @@ def getAnnotations(self, *urns, wildcard=".", include=None, exclude=None, limit=
The second element is the list of Annotations
:rtype: (int, list(Annotation)
"""
return (0,[])

class RetrieverPrototype(object):

""" Prototype for a Retriever
"""
def __init__(self,*args,**kwargs):
pass

@staticmethod
def match(uri):
""" Check to see if this URI is retrievable by this Retriever implementation
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: True if it can be, False if not
:rtype: bool
"""
# prototype implementation can't retrieve anything!
return False
return 0, []

def read(self,uri):
""" Retrieve the contents of the resource
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: the contents of the resource
:rtype: str
"""
return None

156 changes: 156 additions & 0 deletions flask_nemo/query/resolve.py
@@ -0,0 +1,156 @@
from requests import request
import re


class UnresolvableURIError(Exception):
""" Error to be run when a URI is not resolvable
"""


class Resolver(object):

""" Prototype for a Resolver
:param retriever: Retriever(s) to use to resolve resources passed to this resolver
:type retriever: Retriever instances
"""

def __init__(self, *retrievers, **kwargs):
self.__retrievers__ = retrievers

def resolve(self, uri):
""" Resolve a Resource identified by URI
:param uri: The URI of the resource to be resolved
:type uri: str
:return: the contents of the resource as a string
:rtype: str
"""
for r in self.__retrievers__:
if type(r).match(uri):
return r
raise UnresolvableURIError()


class RetrieverPrototype(object):

""" Prototype for a Retriever
"""

@staticmethod
def match(uri):
""" Check to see if this URI is retrievable by this Retriever implementation
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: True if it can be, False if not
:rtype: bool
"""
# prototype implementation can't retrieve anything!
return False

def read(self, uri):
""" Retrieve the contents of the resource
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: the contents of the resource
:rtype: str
"""
return None


class HTTPRetriever(RetrieverPrototype):
""" Http retriever retrieves resources being remotely hosted in CTS
"""
__reg_exp__ = re.compile("^(https?:)?\/\/")

@staticmethod
def match(uri):
""" Check to see if this URI is retrievable by this Retriever implementation
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: True if it can be, False if not
:rtype: bool
"""
# prototype implementation can't retrieve anything!
return HTTPRetriever.__reg_exp__.match(uri) is not None

def read(self, uri):
""" Retrieve the contents of the resource
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: the contents of the resource
:rtype: str
"""
return request("GET", uri).text


class LocalRetriever(RetrieverPrototype):
""" Http retriever retrieves resources being remotely hosted in CTS
:cvar FORCE_MATCH: Force the local retriever to read a resource even if it does not match with the regular expression
:type FORCE_MATCH: bool
"""
__reg_exp__ = re.compile("^([a-z0-9_ ]+|\.{1,2})?/")
FORCE_MATCH = False

@staticmethod
def match(uri):
""" Check to see if this URI is retrievable by this Retriever implementation
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: True if it can be, False if not
:rtype: bool
"""
# prototype implementation can't retrieve anything!
if LocalRetriever.FORCE_MATCH:
return LocalRetriever.__reg_exp__.match(uri) is not None
else:
return True

def read(self, uri):
""" Retrieve the contents of the resource
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: the contents of the resource
:rtype: str
"""
file = None
with open(uri, "r") as f:
file = f.read()
return file


class CTSRetriever(RetrieverPrototype):
""" CTS retriever retrieves resources being remotely hosted in CTS
:param retriever: CTS5 Retrieve
:type retriever: MyCapytain.retrievers.cts5.
"""
__reg_exp__ = re.compile("^urn:cts:")

def __init__(self, retriever):
self.__retriever__ = retriever

@staticmethod
def match(uri):
""" Check to see if this URI is retrievable by this Retriever implementation
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: True if it can be, False if not
:rtype: bool
"""
# prototype implementation can't retrieve anything!
return CTSRetriever.__reg_exp__.match(uri) is not None

def read(self, uri):
""" Retrieve the contents of the resource
:param uri: the URI of the resource to be retrieved
:type uri: str
:return: the contents of the resource
:rtype: str
"""
return self.__retriever__.getPassage(uri)
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -13,7 +13,8 @@
install_requires=[
"MyCapytain>=1.0.1",
"requests_cache>=0.4.9",
"Flask>=0.10.1"
"Flask>=0.10.1",
"requests>=2.10.0"
],
tests_require=[
"mock==1.0.1",
Expand Down
60 changes: 0 additions & 60 deletions tests/test_query.py

This file was deleted.

Empty file added tests/test_query/__init__.py
Empty file.

0 comments on commit 7eb6354

Please sign in to comment.