def make_client(*args, **kwargs): app = Flask("Nemo") app.debug = True if len(args): nemo = Nemo(app=app, base_url="", plugins=list(args), **kwargs) else: nemo = Nemo(app=app, base_url="", plugins=None, **kwargs) return app.test_client()
def setUp(self): self.cache = FileSystemCache("./cache") self.saxon = SaxonShellTransform( "./jars/saxon.jar", "./tests/data/xsl/ciham.xsl", cache=self.cache ) self.nautilus = NautilusRetriever( folders=[ "./tests/data/repo" ] ) self.nautilus.logger.setLevel(logging.ERROR) app = Flask("Nemo") app.debug = True nemo = Nemo( app=app, base_url="", retriever=self.nautilus, transform={ "default": self.saxon.transform } ) self.client = app.test_client()
def runner(method, address, port, host, css, xslt, groupby, debug): resolver = None app = Flask(__name__) if method == "cts-api": resolver = HttpCtsResolver(HttpCtsRetriever(address)) elif method == "cts-local": resolver = CtsCapitainsLocalResolver([address]) if xslt is not None: xslt = {"default": xslt} # We set up Nemo nemo = Nemo(app=app, name="nemo", base_url="", css=css, transform=xslt, resolver=resolver, chunker={ "default": lambda x, y: level_grouper(x, y, groupby=groupby) }) # We run the app app.debug = debug app.run(port=port, host=host) # For test purposes return nemo, app
def test_static_folder(self): """ Test the use of static fikder parameter to implement assets customization """ app = Flask(__name__) nemo = Nemo(app=app, static_folder="/examples") nemo.register() self.assertEqual(nemo.static_folder, "/examples") self.assertEqual(nemo.blueprint.static_folder, "/examples")
def test_template_folder(self): """ Test the parameter to set up a different template folder """ app = Flask(__name__) nemo = Nemo(app=app, template_folder="/examples") nemo.register() self.assertEqual(nemo.template_folder, "/examples") self.assertEqual(nemo.blueprint.template_folder, "/examples")
def test_register_filter_override(self): app = Flask(__name__) plug = FilterPluginOverride() self.nemo = Nemo(app=app, plugins=[plug]) self.assertEqual( self.nemo.app.jinja_env.filters["formatting_passage_reference"], plug.f_formatting_passage_reference, "Original Nemo filters should be override by newly registered filters" )
def test_transform_default_none(self): """ Test that the transform default is called and applied """ nemo = Nemo() transformed = nemo.transform( XmlCtsTextMetadata(urn="urn:cts:phi1294.phi002.perseus-lat2"), etree.fromstring("<a/>"), "urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr.1") self.assertEqual(transformed, "<a/>")
def test_static_url_path(self): """ Test that the extension static url path is changing when set up """ app = Flask(__name__) nemo = Nemo(app=app, static_url_path="/assets/nemoOo") nemo.register() self.assertIn("flask_nemo", app.blueprints, "Nemo is a registered blueprint") rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/nemo/assets/nemoOo/<path:filename>", [rule[0] for rule in rules]) self.assertIn("/nemo/assets/nemoOo.secondary/<filetype>/<asset>", [rule[0] for rule in rules])
def test_register_filter(self): app = Flask(__name__) plug = FilterPlugin() self.nemo = Nemo(app=app, plugins=[plug]) self.assertEqual( self.nemo.app.jinja_env.filters["formatting_passage_reference"], f_formatting_passage_reference, "Original Nemo filters should be kept") self.assertEqual(self.nemo.app.jinja_env.filters["make_string"], plug.f_make_string, "Plugin filters should be registered")
def test_additional_template(self): # Line 568-575 app = Flask(__name__) nemo = Nemo(app=app, templates={"addendum": "tests/test_data/plugin_templates_main/main"}) blueprint = nemo.create_blueprint() html, path, function = blueprint.jinja_loader.get_source("", "addendum::container.html") self.assertIn("I am A CONTAINER ! Isn't it sweet !", html) with self.assertRaises(TemplateNotFound): html, path, function = blueprint.jinja_loader.get_source("", "addendum::unknown.html")
def test_overwrite_urls(self): """ Check that routes can be added or modified """ routes = [("/index.html", "r_index", ["GET"])] + Nemo.ROUTES[1:] app = Flask(__name__) nemo = Nemo(app=app, urls=routes) nemo.register() self.assertIn("flask_nemo", app.blueprints) rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/nemo/index.html", [rule[0] for rule in rules]) self.assertNotIn("/nemo/", [rule[0] for rule in rules])
def test_register_route(self): app = Flask(__name__) nemo = Nemo(app=app, base_url="/perseus") nemo.register() self.assertIn("flask_nemo", app.blueprints) rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/perseus/collections/<objectId>", [rule[0] for rule in rules]) self.assertIn("flask_nemo.r_collection", [rule[1] for rule in rules]) app = Flask(__name__) nemo = Nemo("nemo", app=app) nemo.register() self.assertIn("nemo", app.blueprints) rules = [(rule.rule, rule.endpoint) for rule in app.url_map.iter_rules()] self.assertIn("/nemo/collections/<objectId>", [rule[0] for rule in rules]) self.assertIn("nemo.r_collection", [rule[1] for rule in rules]) nemo = Nemo() self.assertEqual(nemo.register(), None)
def test_get_siblings(self): """ Test first passage """ app = Flask("Nemo") nemo = Nemo(app=app, base_url="", resolver=NautilusDummy, original_breadcrumb=False, chunker={ "default": lambda x, y: level_grouper(x, y, groupby=20) }) # First range p = nemo.get_passage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.pr.1-1.pr.20") l, r = nemo.get_siblings( "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.pr.1-1.pr.20", p) self.assertEqual(l, None, "There should be nothing on the left") self.assertEqual(r, "1.pr.21-1.pr.22", "Next passage should not be 20 long") # Second range p = nemo.get_passage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.pr.21-1.pr.22") l, r = nemo.get_siblings( "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.pr.21-1.pr.22", p) self.assertEqual(l, "1.pr.1-1.pr.20", "There should be something on the left") self.assertEqual(r, "1.1.1-1.1.6", "Next passage should not be 20 long") # Last range p = nemo.get_passage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "14.223.1-14.223.2") l, r = nemo.get_siblings( "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "14.223.1-14.223.2", p) self.assertEqual(l, "14.222.1-14.222.2", "Previous passage does not have to be 20 long") self.assertEqual(r, None, "There should be nothing on the right") # Passage with unknown range p = nemo.get_passage("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.11.1-1.11.2") l, r = nemo.get_siblings( "urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1.11.1-1.11.2", p) self.assertEqual( l, ('1.10.3', '1.10.4'), "Passage should be computed specifically if texts have unknown range" ) self.assertEqual( r, ("1.11.3", "1.11.4"), "Passage should be computed specifically if texts have unknown range" )
def make_client(*args, **kwargs): app = Flask("Nemo") app.debug = True if len(args): nemo = Nemo(app=app, base_url="", resolver=NautilusDummy, chunker={ "default": lambda x, y: level_grouper(x, y, groupby=30) }, plugins=list(args), **kwargs) else: nemo = Nemo(app=app, base_url="", resolver=NautilusDummy, chunker={ "default": lambda x, y: level_grouper(x, y, groupby=30) }, plugins=None, **kwargs) return app.test_client()
def test_chunker_default(self): """ Test that the chunker default is called and applied """ def default(text, reffs): self.assertEqual(str(text.urn), "urn:cts:phi1294.phi002.perseus-lat2") self.assertEqual(reffs, ["1.pr"]) return [("1.pr", "I PR")] nemo = Nemo(chunker={"default": default}) chunked = nemo.chunk( XmlCtsTextMetadata(urn="urn:cts:phi1294.phi002.perseus-lat2"), ["1.pr"]) self.assertEqual(chunked, [("1.pr", "I PR")])
def test_transform_urn_xslt(self): """ Test that the transform default is called and applied """ nemo = Nemo(transform={"default": "tests/test_data/xsl_test.xml"}) transformed = nemo.transform( XmlCtsTextMetadata( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2"), etree.fromstring( '<tei:body xmlns:tei="http://www.tei-c.org/ns/1.0" />'), objectId="urn:cts:latinLit:phi1294.phi002.perseus-lat2", subreference="1.pr.1") self.assertEqual( transformed, '<tei:notbody xmlns:tei="http://www.tei-c.org/ns/1.0"></tei:notbody>', "It should autoclose the tag")
def test_route_assets_all(self): nemo = Nemo( statics=["testing_data/getcapabilities.xml"], js=["testing_data/getcapabilities.xml"], css=["testing_data/style.css"] ) nemo.blueprint = Mock() nemo.register_assets() with patch("flask_nemo.send_from_directory") as patched: nemo.r_assets("js", "getcapabilities.xml") nemo.r_assets("static", "getcapabilities.xml") nemo.r_assets("css", "style.css") patched.assert_has_calls([ call(directory="testing_data", filename="getcapabilities.xml"), call(directory="testing_data", filename="getcapabilities.xml"), call(directory="testing_data", filename="style.css") ])
def setUp(self): self.resolver = Resolver(LocalRetriever(path="./tests/test_data/")) self.one = (URN("urn:cts:latinLit:phi1294.phi002.perseus-lat2:6.1"), "interface/treebanks/treebank1.xml", "dc:treebank") self.two = (URN("urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.5"), "interface/treebanks/treebank2.xml", "dc:treebank") self.three = (URN("urn:cts:latinLit:phi1294.phi002.perseus-lat2:6.1"), "interface/images/N0060308_TIFF_145_145.tif", "dc:image") self.four = AnnotationResource( "interface/researchobject/researchobject.json", type_uri="dc:researchobject", target=URN("urn:cts:latinLit:phi1294.phi002.perseus-lat2:1.pr.1"), resolver=self.resolver) self.one_anno = AnnotationResource(self.one[1], type_uri=self.one[2], target=self.one[0], resolver=self.resolver) self.two_anno = AnnotationResource(self.two[1], type_uri=self.two[2], target=self.two[0], resolver=self.resolver) self.three_anno = AnnotationResource(self.three[1], type_uri=self.three[2], target=self.three[0], resolver=self.resolver) self.fourth_anno = AnnotationResource( self.three[1], type_uri=self.three[2], target=("urn:cts:latinLit:phi1294.phi002.perseus-lat2", "1-2"), resolver=self.resolver) self.app = Flask("app") logger = logging.getLogger('my-logger') logger.propagate = False self.nautilus = CtsCapitainsLocalResolver( ["tests/test_data/interface/latinLit"], logger=logger) self.nemo = Nemo(app=self.app, resolver=self.nautilus, base_url="") self.query = SimpleQuery( [self.one, self.two, self.three, self.four], # List of annotations self.resolver) self.query.process(self.nemo)
def test_transform_default_function(self): """ Test that the transform default is called and applied when it's a function """ urn_given = "urn:cts:latinLit:phi1294.phi002.perseus-lat2" ref_given = "1.pr.1" def default(work, xml, objectId, subreference=None): self.assertEqual(str(work.urn), "urn:cts:latinLit:phi1294.phi002.perseus-lat2") self.assertEqual(objectId, urn_given, "Passage URN should be passed to transform") self.assertEqual(subreference, ref_given, "Passage URN should be passed to transform") self.assertEqual(xml, "<a></a>") return "<b></b>" nemo = Nemo(transform={"default": default}) transformed = nemo.transform( XmlCtsTextMetadata( urn="urn:cts:latinLit:phi1294.phi002.perseus-lat2"), "<a></a>", urn_given, ref_given) self.assertEqual(transformed, "<b></b>")
def setUp(self): with open("tests/test_data/getcapabilities.xml", "r") as f: self.getCapabilities = RequestPatch(f) with open("tests/test_data/getvalidreff.xml", "r") as f: self.getValidReff_single = RequestPatch(f) self.getValidReff = RequestPatchChained( [self.getCapabilities, self.getValidReff_single]) with open("tests/test_data/getpassage.xml", "r") as f: self.getPassage = RequestPatch(f) self.getPassage_Capabilities = RequestPatchChained( [self.getCapabilities, self.getPassage]) with open("tests/test_data/getpassageplus.xml", "r") as f: self.getPassagePlus = RequestPatch(f) with open("tests/test_data/getprevnext.xml", "r") as f: self.getPrevNext = RequestPatch(f) self.getPassage_Route = RequestPatchChained( [self.getCapabilities, self.getPassage, self.getPrevNext]) self.nemo = Nemo(resolver=NemoResource.endpoint, app=Flask(__name__))
def test_register_filter_namespace(self): app = Flask(__name__) plug = FilterPluginOverride(namespacing=True, name="no_override") plug_noname = FilterPluginOverride(namespacing=True) self.nemo = Nemo(app=app, plugins=[plug, plug_noname]) self.assertEqual( self.nemo.app.jinja_env.filters["formatting_passage_reference"], f_formatting_passage_reference, "Original Nemo filters should be kept when namespacing is done") self.assertEqual( self.nemo.app.jinja_env. filters["no_override_formatting_passage_reference"], plug.f_formatting_passage_reference, "Original Nemo filters should be override by newly registered filters" ) self.assertEqual( self.nemo.app.jinja_env. filters["FilterPluginOverride_formatting_passage_reference"], plug_noname.f_formatting_passage_reference, "Original Nemo filters should be override by newly registered filters" )
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)
def make_nemo(self, app, **kwargs): return Nemo(app=app, cache=Cache(app=app, config={"CACHE_TYPE": "simple"}), **kwargs)
) 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 "./styles/geste.css" ], js=[ # use own js file to load a script to go from normalized edition to diplomatic one. "./styles/geste.js" ], templates={ "main": "./templates" }, statics=["./images/logo-enc2.png","./fonts/Junicode-Regular.ttf","./fonts/Junicode-Regular.woff"] #, #additional_static=[ # "img/logo-enc2.jpg" #] ) cache.init_app(app)
def test_warn_on_empty_value(self): """ Test when an endpoint is set """ from flask_nemo.errors import ValueWarning with self.assertWarns(ValueWarning): nemo = Nemo(base_url="/")
def test_other_endpoint(self): """ Test when an endpoint is set """ nemo = Nemo(resolver=NautilusDummy) self.assertEqual(nemo.resolver, NautilusDummy, "Endpoint should be set through endpoint parameter")
from flask import Flask from flask_nemo import Nemo from flask_nemo.chunker import level_grouper from MyCapytain.common.reference import URN from perseus_nemo_ui import PerseusNemoUi from pkg_resources import resource_filename app = Flask("Nemo") app.debug = True nemo = Nemo(app=app, base_url="", api_url="http://cts.perseids.org/api/cts", chunker={"default": level_grouper}, plugins=[PerseusNemoUi()], transform={ "default": resource_filename("perseus_nemo_ui", "data/assets/static/xslt/epidocShort.xsl") }) app.run()
def test_flask_nemo(self): """ Testing Flask Nemo is set up""" a = Nemo() self.assertIsInstance(a, Nemo) a = Nemo()
from nemo_xslttwo_plugin import SaxonShellTransform import logging from capitains_nautilus.flask_ext import NautilusRetriever from flask_nemo import Nemo from flask import Flask saxon = SaxonShellTransform("./jars/saxon.jar", "./tests/data/xsl/ciham.xsl") nautilus = NautilusRetriever(folders=["./tests/data/repo"]) nautilus.logger.setLevel(logging.ERROR) app = Flask("Nemo") app.debug = True nemo = Nemo(app=app, base_url="", retriever=nautilus, transform={"default": saxon.transform}) if __name__ == "__main__": app.run(debug=True)
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), AlpheiosBreadcrumb() ], transform={ "default": resource_filename("alpheios_nemo_ui", "data/assets/static/xslt/alpheios-enhanced.xsl") }, ) #app.debug = True if __name__ == "__main__": app.run(debug=True, host='0.0.0.0')