Exemple #1
0
    def setUp(self):
        # Full creation of app
        self.cache = FileSystemCache(subprocess_cache_dir, default_timeout=0)
        self.resolver = NautilusCTSResolver(subprocess_repository,
                                            dispatcher=make_dispatcher(),
                                            cache=self.cache)
        self.__app__ = Flask("Nautilus")
        self.http_cache = Cache(self.app,
                                config={
                                    'CACHE_TYPE': "filesystem",
                                    "CACHE_DIR": http_cache_dir,
                                    "CACHE_DEFAULT_TIMEOUT": 0
                                })
        self.nautilus = FlaskNautilus(app=self.app,
                                      prefix="/api",
                                      name="nautilus",
                                      resolver=self.resolver,
                                      flask_caching=self.http_cache)

        self.test_client = self.app.test_client()

        # Option to ensure cache works
        self.former_parse = self.resolver.parse

        def x(*k, **kw):
            raise self.ParsingCalled("Parse should not be called")

        self.resolver.parse = x
Exemple #2
0
 def test_restricted_cors(self):
     """ Check that area-restricted cors works """
     app = Flask("Nautilus")
     FlaskNautilus(
         app=app,
         resolver=NautilusCTSResolver(["./tests/test_data/latinLit"]),
         access_Control_Allow_Methods={"r_cts": "OPTIONS", "r_dts_collection": "OPTIONS", "r_dts_collections": "OPTIONS"},
         access_Control_Allow_Origin={"r_cts": "foo.bar", "r_dts_collection":"*", "r_dts_collections":"*"}
     )
     _app = app.test_client()
     self.assertEqual(_app.get("/cts?request=GetCapabilities").headers["Access-Control-Allow-Origin"], "foo.bar")
     self.assertEqual(_app.get("/cts?request=GetCapabilities").headers["Access-Control-Allow-Methods"], "OPTIONS")
Exemple #3
0
def _commandline(repositories,
                 port=8000,
                 host="127.0.0.1",
                 debug=False,
                 cache=None,
                 cache_path="./cache",
                 redis=None):
    """ Run a CTS API from command line.

    .. warning:: This function should not be used in the production context

    :param repositories:
    :param port:
    :param ip:
    :param debug:
    :param cache:
    :param cache_path:
    :return:
    """

    if cache == "redis":
        nautilus_cache = RedisCache(redis)
        cache_type = "redis"
    elif cache == "filesystem":
        nautilus_cache = FileSystemCache(cache_path)
        cache_type = "simple"
    else:
        nautilus_cache = NullCache()
        cache_type = "simple"

    app = Flask("Nautilus")
    if debug:
        app.logger.setLevel(logging.INFO)

    resolver = NautilusCTSResolver(resource=repositories)
    nautilus = FlaskNautilus(
        app=app,
        resolver=resolver
        #parser_cache=WerkzeugCacheWrapper(nautilus_cache),
        #logger=None
    )
    nautilus.resolver.parse()
    if debug:
        app.run(debug=debug, port=port, host=host)
    else:
        app.debug = debug
        http_server = HTTPServer(WSGIContainer(app))
        http_server.bind(port=port, address=host)
        http_server.start(0)
        IOLoop.current().start()
Exemple #4
0
 def setUp(self):
     """ Set up a dummy application with a manager """
     nautilus_cache = FileSystemCache("cache_dir")
     nautilus_cache.clear()
     app = Flask("Nautilus")
     resolver = NautilusCTSResolver(["./tests/test_data/latinLit"], cache=nautilus_cache, logger=logger)
     flask_nautilus = FlaskNautilus(
         app=app,
         resolver=resolver,
         flask_caching=Cache(config={'CACHE_TYPE': 'filesystem'}),
         logger=logger
     )
     self.cache_manager = nautilus_cache
     self.nautilus = flask_nautilus
     self.resolver = resolver
     self.resolver.logger.disabled = True
     self.manager = FlaskNautilusManager(resolver, flask_nautilus)
Exemple #5
0
    def setUp(self):
        nautilus_cache = RedisCache()
        app = Flask("Nautilus")
        self.cache = Cache(config={'CACHE_TYPE': 'simple'})
        self.nautilus = FlaskNautilus(
            app=app,
            resolver=NautilusCTSResolver(["./tests/test_data/latinLit"]),
            flask_caching=self.cache,
            logger=logger
        )
        app.debug = True
        self.cache.init_app(app)
        self.app = app.test_client()
        self.parent = HttpCtsRetriever("/cts")
        self.resolver = HttpCtsResolver(endpoint=self.parent)
        logassert.setup(self, self.nautilus.logger.name)
        self.nautilus.logger.disabled = True

        def call(this, parameters={}):
            """ Call an endpoint given the parameters

            :param parameters: Dictionary of parameters
            :type parameters: dict
            :rtype: text
            """

            parameters = {
                key: str(parameters[key]) for key in parameters if parameters[key] is not None
            }
            if this.inventory is not None and "inv" not in parameters:
                parameters["inv"] = this.inventory

            request = self.app.get("/cts?{}".format(
                "&".join(
                    ["{}={}".format(key, value) for key, value in parameters.items()])
                )
            )
            self.parent.called.append(parameters)
            return request.data.decode()

        self.parent.called = []
        self.parent.call = lambda x: call(self.parent, x)
Exemple #6
0
organizer = CollectionDispatcher(general_collection,
                                 default_inventory_name="id:misc")


@organizer.inventory("greek_texts")
def organize_my_meadow(collection, path=None, **kwargs):
    if collection.id.startswith("urn:cts:greekLit"):
        return True
    return False


flask_app = Flask("Flask Application for Nemo")
resolver = NautilusCTSResolver(["corpora/meadow"], dispatcher=organizer)
resolver.parse()

nautilus_api = FlaskNautilus(prefix="/api", app=flask_app, resolver=resolver)

nemo = Nemo(
    name="InstanceNemo",
    app=flask_app,
    resolver=resolver,
    base_url="",
    css=["assets/css/theme.css"],
    js=["assets/js/alpheios.js"],
    statics=["assets/images/logo.jpg"],
    transform={"default": "components/main.xsl"},
    templates={"main": "templates/main"},
    chunker={"urn:cts:greekLit:tlg2856.tlg001.1st1K-grc1": meadow_chunker})

if __name__ == "__main__":
    flask_app.run(debug=True)
Exemple #7
0
# For test purposes
from capitains_nautilus.flask_ext import WerkzeugCacheWrapper, FlaskNautilus
from flask import Flask
from werkzeug.contrib.cache import NullCache
nautilus_cache = WerkzeugCacheWrapper(NullCache())
app = Flask("Nautilus")
nautilus = FlaskNautilus(app=app,
                         resources=["./tests/test_data/latinLit"],
                         parser_cache=nautilus_cache)

app.debug = True
app.run("0.0.0.0", 5000)
Exemple #8
0
        level = len(citation_types)
        level_name = citation_types[-1]
        excerpt_length = 20

    if excerpt_length > 1:
        reffs = level_grouper(text, getreffs, level, excerpt_length)
    else:
        reffs = getreffs(level=level)
        reffs = [(str(reff), level_name + " " + str(reff)) for reff in reffs]

    return reffs


flask_app = Flask("Flask Application for Nemo")
nautilus_api = FlaskNautilus(name="Nautilus",
                             prefix="/api",
                             app=flask_app,
                             resolver=resolver)
nemo = MyNemo(name="InstanceNemo",
              app=flask_app,
              resolver=resolver,
              base_url="",
              css=["assets/css/theme.css"],
              js=["assets/js/empty.js"],
              statics=["assets/images/logo.jpeg"],
              transform={"default": "components/main.xsl"},
              templates={"main": "templates/main"},
              chunker={"default": generic_chunker})

if __name__ == "__main__":
    flask_app.run(debug=True)
auth0 = oauth.register(
    'auth0',
    client_id=client_id,
    client_secret=client_secret,
    api_base_url='https://alpheios.auth0.com',
    access_token_url='https://alpheios.auth0.com/oauth/token',
    authorize_url='https://alpheios.auth0.com/authorize',
    client_kwargs={
        'audience': audience,
        'scope': 'openid profile',
    },
)
nautilus = FlaskNautilus(app=app,
                         prefix="/api",
                         name="nautilus",
                         resolver=resolver,
                         flask_caching=None)
nautilus_api = FlaskNautilus(prefix="/api", app=app, resolver=resolver)

# We set up Nemo
nemo = Nemo(
    app=app,
    name="nemo",
    base_url="",
    cache=None,
    resolver=resolver,
    chunker={"default": scheme_grouper},
    original_breadcrumb=False,
    plugins=[
        AlpheiosNemoUI("", auth0, proxy_base, auth_max_age_override),
Exemple #10
0
            return True
        return False

    return dispatcher


nautilus_cache = FileSystemCache(subprocess_cache_dir, default_timeout=0)

resolver = NautilusCTSResolver(subprocess_repository,
                               dispatcher=make_dispatcher(),
                               cache=nautilus_cache)

app = Flask("Nautilus")
http_cache = Cache(app,
                   config={
                       'CACHE_TYPE': "filesystem",
                       "CACHE_DIR": http_cache_dir,
                       "CACHE_DEFAULT_TIMEOUT": 0
                   })
nautilus = FlaskNautilus(app=app,
                         prefix="/api",
                         name="nautilus",
                         resolver=resolver,
                         flask_caching=http_cache)

#http_cache.init_app(app)
app.debug = True

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')
Exemple #11
0
from . import configurable


app = Flask("app")


extension_nemo = configurable.nemo_class(
    base_url="",
    resolver=configurable.resolver,
    templates={
        "main": configurable.templates_folder,
        "additional": configurable.templates_folder_additional
    },
    chunker={
        "default": configurable.chunker
    },
    transform=configurable.xslt_dict,
    static_folder=configurable.statics_folder
)
extension_nautilus = FlaskNautilus(
    prefix="/api",
    resolver=configurable.resolver
)


extension_nautilus.init_app(app)
extension_nemo.init_app(app)

configurable.instantiate_errors(app, extension_nemo)
Exemple #12
0
import os

from capitains_nautilus.flask_ext import FlaskNautilus
from flask import Flask, jsonify
from flask_caching import Cache
from raven.contrib.flask import Sentry

from .data import ROOT_DIR_PATH
from .resolver import resolver

app = Flask("Nautilus")
http_cache = Cache(config={"CACHE_TYPE": "simple"})
nautilus = FlaskNautilus(
    app=app,
    prefix="/api",
    name="nautilus",
    resolver=resolver,
    flask_caching=http_cache,
    logger=logging.getLogger("nautilus"),
)
http_cache.init_app(app)
Sentry(app)


@app.route("/repos")
def repos():
    repos_json = os.path.join(ROOT_DIR_PATH, "repos.json")
    with open(repos_json, "r") as f:
        return jsonify(json.loads(f.read()))


@app.route("/corpus-metadata")
from flask import Flask, request
from flask.ext.nemo import Nemo
from capitains_nautilus.flask_ext import FlaskNautilus, WerkzeugCacheWrapper
from werkzeug.contrib.cache import RedisCache
from flask_cache import Cache
import os

d = "/opt/data"
app = Flask("Nautilus")
nautilus_cache = RedisCache("redis", port=6379, default_timeout=0)
nautilus = FlaskNautilus(app=app,
                         prefix="/api/cts",
                         name="nautilus",
                         resources=[
                             os.path.join(d, o) for o in os.listdir(d)
                             if os.path.isdir(os.path.join(d, o))
                         ],
                         parser_cache=WerkzeugCacheWrapper(nautilus_cache),
                         http_cache=Cache(config={'CACHE_TYPE': "simple"}))

# We set up Nemo
nemo = Nemo(app=app,
            name="nemo",
            base_url="",
            api_url="/api/cts",
            retriever=nautilus.retriever,
            chunker={"default": Nemo.level_grouper},
            css=["/opt/static/assets/nemo.secondary/css/theme-ext.css"],
            transform={"default": "/opt/static/assets/xslt/epidocShort.xsl"},
            templates={"passage_footer": "templates/passage_footer.html"})
Exemple #14
0
    elif types == ["line"]:
        level, groupby = 1, 30
    elif types == ["chapter", "section"]:
        level, groupby = 2, 2
    elif types == ["chapter", "mishnah"]:
        level, groupby = 2, 1
    elif types == ["chapter", "verse"]:
        level, groupby = 2, 1
    elif "line" in types:
        groupby = 30
    return level_grouper(text, getreffs, level, groupby)


nautilus = FlaskNautilus(
    app=app,
    prefix="/elec/geste/api",
    name="nautilus",
    resolver=NautilusDummy
)

nemo = Nemo(
    app=app,
    base_url="/elec/geste",
    resolver=NautilusDummy,
    chunker={"default": scheme_grouper},
    plugins=None,
    cache=cache,
    transform={
        "default": "./geste.xslt"
    },
    css=[
        # USE Own CSS
Exemple #15
0
from flask_nemo import Nemo
from capitains_nautilus.cts.resolver import NautilusCTSResolver
from capitains_nautilus.flask_ext import FlaskNautilus

app = flask.Flask("CTS webserver demo (nemo)")

LOCAL_CONFIG = 'config.py'
if os.path.exists(LOCAL_CONFIG):
    app.config.from_pyfile(LOCAL_CONFIG)

corpora = [entry.path for entry in os.scandir('corpora') if entry.is_dir()]
print('app: resolver given', corpora)
resolver = NautilusCTSResolver(corpora)
resolver.parse()

nautilus = FlaskNautilus(prefix='/api', app=app, resolver=resolver)

nemo = Nemo(name='Nemo', app=app, resolver=resolver, base_url='/nemo')


@app.route('/')
def home():
    '''Placeholder home page to help visitors.

    We're mainly here to serve the nemo browser and api endoints,
    but provide a simple landing page in case we're serving the
    whole domain.'''
    return flask.render_template('index.html',
                                 nemo=nemo.prefix,
                                 nautilus=nautilus.prefix,
                                 scaife=app.config.get('SCAIFE_URL'))
Exemple #16
0
# -*- coding: utf-8 -*-

from flask import Flask, request
from flask_nemo import Nemo
from flask_nemo.chunker import level_grouper
from flask_nemo.query.resolve import Resolver, HTTPRetriever
from flask_nemo.query.interface import SimpleQuery
from capitains_nautilus.flask_ext import FlaskNautilus
from pkg_resources import resource_filename
from MyCapytain.common.reference import URN
from aperire_ui import AperireUI
from nemo_arethusa_plugin import Arethusa

app = Flask("Nautilus")
nautilus = FlaskNautilus(app=app,
                         prefix="/api/cts",
                         name="nautilus",
                         resources=["."])


def lesson_chunker(version, getValidReff):
    reffs = [urn.split(":")[-1] for urn in getValidReff(level=2)]
    # Satura scheme contains three level (book, poem, lines) but only the Satura number is sequential
    # So as human readable, we give only the second member of the reference body
    return [(reff, "Texte {1}".format(*tuple(reff.split("."))))
            for reff in reffs]


# Setup the Query Interface
# this is temporary to allow for demo of functionality
# should eventually call to a collections api
query = SimpleQuery(