diff --git a/nb2workflow/nbadapter.py b/nb2workflow/nbadapter.py index 5be0700..d1ff367 100644 --- a/nb2workflow/nbadapter.py +++ b/nb2workflow/nbadapter.py @@ -65,6 +65,7 @@ class ModOntology(Ontology): def __init__(self, ontology_path): super().__init__(ontology_path) self.lock = Lock() + self._is_ontology_available = True def get_datatype_restriction(self, param_uri): self.lock.acquire() @@ -74,6 +75,11 @@ def get_datatype_restriction(self, param_uri): logger.warning(f'Unknown datatype for owl_uri {param_uri}') return dt + @property + def is_ontology_available(self): + # TODO will be developed properly in the ontology_helper + return self._is_ontology_available + ontology = ModOntology(oda_ontology_path) oda_prefix = str([x[1] for x in ontology.g.namespaces() if x[0] == 'oda'][0]) @@ -813,9 +819,15 @@ def download_file(self, file_url, tmpdir): def handle_url_params(self, parameters, tmpdir, context={}): adapted_parameters = copy.deepcopy(parameters) exceptions = [] + posix_path_with_annotations_pattern = re.compile(rf"^{re.escape(oda_prefix)}.*_POSIXPath_") for input_par_name, input_par_obj in self.input_parameters.items(): - # TODO use oda_api.ontology_helper - if input_par_obj['owl_type'] == f"{oda_prefix}POSIXPath": + if ontology.is_ontology_available: + parameter_hierarchy = ontology.get_parameter_hierarchy(input_par_obj['owl_type']) + is_posix_path = f"{oda_prefix}POSIXPath" in parameter_hierarchy + else: + is_posix_path = f"{oda_prefix}POSIXPath" == input_par_obj['owl_type'] or \ + posix_path_with_annotations_pattern.match(input_par_obj['owl_type']) is not None + if is_posix_path: arg_par_value = parameters.get(input_par_name, None) if arg_par_value is None: arg_par_value = input_par_obj['default_value'] diff --git a/tests/conftest.py b/tests/conftest.py index 7dde176..0324f26 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,9 @@ import nb2workflow.service from importlib import reload +import rdflib as rdf + +from oda_api.ontology_helper import ODA, ODAS @pytest.fixture def test_notebook(): @@ -51,6 +54,7 @@ def app(test_notebook): app = nb2workflow.service.app app.notebook_adapters = nb2workflow.nbadapter.find_notebooks(test_notebook) nb2workflow.service.setup_routes(app) + nb2workflow.nbadapter.ontology._is_ontology_available = True print("creating app") return app @@ -63,10 +67,19 @@ def app_low_download_limit(): for nb, nba_obj in app_low_download_limit.notebook_adapters.items(): nba_obj.max_download_size = 1 nb2workflow.service.setup_routes(app_low_download_limit) + nb2workflow.nbadapter.ontology._is_ontology_available = True print("creating app with low limit on the download of files") return app_low_download_limit +# TODO improve this, as it requires changes also in the oda_api +@pytest.fixture +def app_not_available_ontology(): + nb2workflow.nbadapter.ontology._is_ontology_available = False + yield + + + def kill_child_processes(parent_pid, sig=signal.SIGTERM): try: parent = psutil.Process(parent_pid) diff --git a/tests/test_input_types.py b/tests/test_input_types.py index 3085d64..c91d149 100644 --- a/tests/test_input_types.py +++ b/tests/test_input_types.py @@ -15,18 +15,30 @@ def app(): app = nb2workflow.service.app app.notebook_adapters = nb2workflow.nbadapter.find_notebooks(testfiles_path) nb2workflow.service.setup_routes(app) + nb2workflow.nbadapter.ontology._is_ontology_available = True print("creating app") return app - def test_posix_download_file(client): r = client.get('/api/v1.0/get/testposixpath') assert r.json['output']['output_file_download'] == 'file not downloaded' +def test_posix_download_file_no_ontology(client, app_not_available_ontology): + r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'}) + assert r.json['output']['output_file_download'] == 'file downloaded successfully' + +def test_posix_download_file_no_ontology_extra_annotations(client, app_not_available_ontology): + r = client.get('/api/v1.0/get/testposixpath_extra_annotated', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'}) + assert r.json['output']['output_file_download'] == 'file downloaded successfully' + def test_posix_download_file_with_arg(client): r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'}) assert r.json['output']['output_file_download'] == 'file downloaded successfully' +def test_posix_download_file_extra_annotations(client): + r = client.get('/api/v1.0/get/testposixpath_extra_annotated', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'}) + assert r.json['output']['output_file_download'] == 'file downloaded successfully' + def test_posix_download_file_with_arg_low_download_limit(client, app_low_download_limit): r = client.get('/api/v1.0/get/testposixpath', query_string={'fits_file_path': 'https://fits.gsfc.nasa.gov/samples/testkeys.fits'}) assert r.json['output'] == {} diff --git a/tests/test_nbadapter.py b/tests/test_nbadapter.py index d479d30..77ffaaf 100644 --- a/tests/test_nbadapter.py +++ b/tests/test_nbadapter.py @@ -84,7 +84,7 @@ def test_find_notebooks(caplog): assert 'Ignoring pattern.' in caplog.text nbas = find_notebooks(nb_dir) - assert len(nbas) == 8 + assert len(nbas) == 9 nbas = find_notebooks(nb_dir, pattern=r'.*bool') assert len(nbas) == 1 diff --git a/tests/testfiles/testposixpath_extra_annotated.ipynb b/tests/testfiles/testposixpath_extra_annotated.ipynb new file mode 100644 index 0000000..a47aa72 --- /dev/null +++ b/tests/testfiles/testposixpath_extra_annotated.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "parameters" + ] + }, + "outputs": [], + "source": [ + "fits_file_path = \"test.fits\" # oda:POSIXPath ; oda:label \"Test file path\" ; oda:description \"Description test file path\" ." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "output_file_download = 'file not downloaded'\n", + "if os.path.exists(fits_file_path) is True:\n", + " output_file_download = 'file downloaded successfully'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "outputs" + ] + }, + "outputs": [], + "source": [ + "output_file_download" + ] + } + ], + "metadata": { + "interpreter": { + "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}