diff --git a/app.py b/app.py index 8281683..9610750 100644 --- a/app.py +++ b/app.py @@ -30,7 +30,8 @@ config_name = os.environ.get("APP_MODE") or "development" middleware = [ Middleware( - SessionMiddleware, secret_key=os.environ.get("APP_SECRET", "changemeNOW"), + SessionMiddleware, + secret_key=os.environ.get("APP_SECRET", "changemeNOW"), ), # Middleware(CSRFProtectMiddleware, csrf_secret=os.environ.get('APP_SECRET','changemeNOW')), Middleware( @@ -97,11 +98,12 @@ def get_flashed_messages(request: Request): templates.env.globals["get_flashed_messages"] = get_flashed_messages + @app.get("/", response_class=HTMLResponse, include_in_schema=False) async def index(request: Request): start_form = await forms.StartForm.from_formdata(request) start_form.method_sel.choices = [(v, k) for k, v in app.methods_dict.items()] - #request.session.clear() + # request.session.clear() return templates.TemplateResponse( "index.html", { @@ -113,11 +115,14 @@ async def index(request: Request): }, ) + @app.post("/create_mapper", response_class=HTMLResponse, include_in_schema=False) async def create_mapper(request: Request): - authorization=request.headers.get('Authorization',None) + # print(request.headers) + authorization = request.headers.get("Authorization", None) + # print(f"create mapper authorization: {authorization}") if authorization: - logging.debug('got authorization header') + logging.debug("got authorization header") logging.debug(await request.body()) start_form = await forms.StartForm.from_formdata(request) @@ -168,13 +173,13 @@ async def create_mapper(request: Request): method_object_super_class_uris=[ URIRef(uri) for uri in mapping_object_class_uris ], - authorization=authorization + authorization=authorization, ) flash(request, str(mapper), "info") - #flash(request, str(mapper.subjects), "info") + # flash(request, str(mapper.subjects), "info") except Exception as err: flash(request, str(err), "error") - #print(mapper.objects.keys()) + # print(mapper.objects.keys()) # only named instances in the data can be mapped else: info_choices = [ @@ -186,9 +191,9 @@ async def create_mapper(request: Request): select_forms = forms.get_select_entries(mapper.objects.keys(), info_choices) mapping_form = await forms.MappingFormList.from_formdata(request) mapping_form.assignments.entries = select_forms - request.session['auth']=authorization - logging.debug('session: {}'.format(request.session)) - + request.session["auth"] = authorization + logging.debug("session: {}".format(request.session)) + return templates.TemplateResponse( "index.html", { @@ -203,14 +208,15 @@ async def create_mapper(request: Request): @app.post("/map", response_class=HTMLResponse, include_in_schema=False) async def map(request: Request): - authorization=request.headers.get('Authorization',None) or request.session.get('auth',None) + authorization = request.headers.get("Authorization", None) or request.session.get( + "auth", None + ) if authorization: - logging.debug('got authorization header') - logging.debug('get map') - logging.debug('session: {}'.format(request.session)) + logging.debug("got authorization header") + logging.debug("get map") + logging.debug("session: {}".format(request.session)) - - #logging.debug(request.data) + # logging.debug(request.data) formdata = await request.form() data_url = request.session.get("data_url", None) method_url = request.session.get("method_url", None) @@ -235,8 +241,8 @@ async def map(request: Request): maplist = [(k, v) for k, v in select_dict.items() if v != "None"] logging.info("Creating mapping file for mapping list: {}".format(maplist)) request.session["maplist"] = maplist - logging.debug('subjects: {}'.format(subjects)) - logging.debug('objects: {}'.format(objects)) + logging.debug("subjects: {}".format(subjects)) + logging.debug("objects: {}".format(objects)) with maptomethod.Mapper( data_url=data_url, method_url=method_url, @@ -251,13 +257,13 @@ async def map(request: Request): maplist=maplist, subjects=subjects, objects=objects, - authorization=authorization + authorization=authorization, ) as mapper: result = mapper.to_pretty_yaml() filename = result["filename"] result_string = result["filedata"] - #print(type(result_string)) - #print(result_string) + # print(type(result_string)) + # print(result_string) b64 = base64.b64encode(result_string.encode()) payload = b64.decode() return templates.TemplateResponse( @@ -298,10 +304,12 @@ class Config: @app.post("/api/entities") def query_entities(request: QueryRequest, req: Request): - authorization=req.headers.get('Authorization',None) + authorization = req.headers.get("Authorization", None) # translate urls in entity_classes list to URIRef objects request.entity_classes = [URIRef(str(url)) for url in request.entity_classes] - return maptomethod.query_entities(str(request.url), request.entity_classes, authorization) + return maptomethod.query_entities( + str(request.url), request.entity_classes, authorization + ) class MappingRequest(BaseModel): @@ -311,8 +319,10 @@ class MappingRequest(BaseModel): method_url: AnyUrl = Field( "", title="Method Graph Url", description="Url to knowledge graph to use." ) - use_template_rowwise: Optional[bool] = Field( - False, title="Use Template Rowwise", description="If to duplicate the Method Graph for each row.", + use_template_rowwise: Optional[bool] = Field( + False, + title="Use Template Rowwise", + description="If to duplicate the Method Graph for each row.", omit_default=True, ) data_super_classes: List = Field( @@ -363,10 +373,10 @@ class YAMLResponse(StreamingResponse): @app.post("/api/mapping", response_class=YAMLResponse) def mapping(request: MappingRequest, req: Request) -> StreamingResponse: - authorization=req.headers.get('Authorization',None) + authorization = req.headers.get("Authorization", None) if authorization: - logging.debug('got authorization header') - + logging.debug("got authorization header") + try: result = maptomethod.Mapper( str(request.data_url), @@ -375,15 +385,14 @@ def mapping(request: MappingRequest, req: Request) -> StreamingResponse: URIRef(str(uri)) for uri in request.method_super_classes ], mapping_predicate_uri=URIRef(str(request.predicate)), - use_template_rowwise = request.use_template_rowwise, + use_template_rowwise=request.use_template_rowwise, data_subject_super_class_uris=[ URIRef(str(uri)) for uri in request.data_super_classes ], maplist=request.map.items(), - authorization=authorization + authorization=authorization, ).to_pretty_yaml() except Exception as err: - print(err) raise HTTPException(status_code=500, detail=str(err)) data_bytes = BytesIO(result["filedata"].encode()) filename = result["filename"] @@ -424,6 +433,7 @@ async def info() -> dict: log_config=config, ) + @app.exception_handler(HTTPException) async def http_exception_handler(request, exc): return JSONResponse(content={"message": exc.detail}, status_code=exc.status_code) diff --git a/maptomethod.ipynb b/maptomethod.ipynb deleted file mode 100644 index 6311568..0000000 --- a/maptomethod.ipynb +++ /dev/null @@ -1,802 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "ZiNqYFGS5Sek" - }, - "outputs": [], - "source": [ - "#@title Code - Please run once!\n", - "!pip install rdflib\n", - "!pip install PyGithub\n", - "!pip install rdflib\n", - "import ipywidgets as widgets\n", - "from IPython.display import display, clear_output\n", - "import getpass\n", - "from urllib.request import urlretrieve\n", - "import base64\n", - "\n", - "url = \"https://raw.githubusercontent.com/Mat-O-Lab/MapToMethod/main/maptomethod.py\"\n", - "file_name = url.split('/')[-1]\n", - "\n", - "import os.path\n", - "if not os.path.isfile(file_name):\n", - " urlretrieve(url,file_name) \n", - "import maptomethod\n", - "\n", - "class MapDialog():\n", - " def __init__(self, csv_meta_url='', method_url=''):\n", - " self.csv_meta_url = csv_meta_url\n", - " self.method_url = method_url\n", - " self.methods=maptomethod.get_methods()\n", - " def _create_initial_widgets(self):\n", - " self.meta_url_widget=widgets.Text(\n", - " value='',\n", - " placeholder='put ur url to a *-metadata.json here',\n", - " description='Url:',\n", - " disabled=False\n", - " )\n", - " # self.uploader = widgets.FileUpload(accept='', # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'\n", - " # multiple=False, # True to accept multiple files upload else False\n", - " # description='Upload'\n", - " # )\n", - " #self.clear_button = widgets.Button(description='Clear!', layout=widgets.Layout(width='100px')); \n", - " self.file_dialog= widgets.HBox([widgets.Label(value=\"File:\"), self.meta_url_widget]) #,self.uploader,self.clear_button])\n", - " #self.clear_button.on_click(self._on_clear)\n", - " # self.data_type_sel=widgets.Box(\n", - " # [\n", - " # widgets.Label(value='primary or secondary data:'),\n", - " # widgets.RadioButtons(\n", - " # options=[\n", - " # 'primary - the data describes one experiment with one specimen',\n", - " # 'secondary - data table with results of multiple experiments',\n", - " # ],\n", - " # layout={'width': 'max-content'}\n", - " # )\n", - " # ]\n", - " # )\n", - " self.method_sel = widgets.Dropdown(\n", - " options=list(self.methods.keys()),\n", - " #value=list(methods.items())[0][1],\n", - " description='method:',\n", - " disabled=False,\n", - " )\n", - " self.out = widgets.Output() # this is the output widget in which the df is displayed\n", - " self.start_mapping_button = widgets.Button(description='Start Mapping!', layout=widgets.Layout(width='200px')); \n", - " self.start_mapping_button.on_click(self._start_mapping)\n", - " def _on_clear(self,button):\n", - " self.uploader.value.clear()\n", - " self.uploader._counter = 0\n", - " def _start_mapping(self,button):\n", - " # \"linking function with output\"\n", - " with self.out:\n", - " # what happens when we press the button\n", - " clear_output()\n", - " if not (self.meta_url_widget.value or self.uploader.value.keys()):\n", - " print('pls upload a file first or insert a url')\n", - " return\n", - " if self.meta_url_widget.value:\n", - " self.csv_meta_url=self.meta_url_widget.value\n", - " file_name=self.csv_meta_url.split('/')[-1]\n", - " else:\n", - " input_file=self.uploader.value[list(self.uploader.value.keys())[0]]\n", - " self.csv_meta_url=input_file['metadata']['name']\n", - " file_name = input_file['metadata']['name']\n", - " self.file_data = input_file['content']\n", - " self.method_url=self.methods[self.method_sel.value] \n", - " self.mapper=maptomethod.Mapper(self.csv_meta_url,self.method_url)\n", - " self.methode_ices=self.mapper.ices\n", - " self.data_info=self.mapper.info_lines\n", - " info_lines=[(value['text'],id) for id, value in self.data_info.items()]\n", - " info_lines.insert(0,('None',None))\n", - " self.map_elements=list()\n", - " for id, url in self.methode_ices.items():\n", - " self.map_elements.append(widgets.HBox([\n", - " widgets.Label(id),\n", - " widgets.Dropdown(\n", - " options=info_lines,\n", - " disabled=False,)\n", - " ]))\n", - " display(widgets.VBox(self.map_elements))\n", - " self.create_mapping_button = widgets.Button(description='Create Mapping!', layout=widgets.Layout(width='200px'));\n", - " buttons= widgets.HBox([self.create_mapping_button,])\n", - " self.create_mapping_button.on_click(self._create_mapping)\n", - " display(buttons)\n", - " def _create_mapping(self,button):\n", - " # \"linking function with output\"\n", - " with self.out:\n", - " clear_output()\n", - " map_list=[element.children for element in self.map_elements]\n", - " map_list=[(widget_label.value,select.value) for (widget_label,select) in map_list if select.value]\n", - " self.mapper.maplist=map_list\n", - " \n", - " mapping_filename, mapping_data=self.mapper.to_yaml()\n", - " print(mapping_data) \n", - " res = mapping_data\n", - " b64 = base64.b64encode(res.encode())\n", - " payload = b64.decode()\n", - " button_html = '''\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " '''\n", - " download_button = button_html.format(title='Download File',payload=payload,filename=mapping_filename)\n", - " display(widgets.HTML(download_button))\n", - " \n", - " def display_widgets(self):\n", - " self._create_initial_widgets()\n", - " display(widgets.VBox(\n", - " [\n", - " self.file_dialog,\n", - " #self.data_type_sel,\n", - " self.method_sel,\n", - " self.start_mapping_button,\n", - " self.out\n", - " ]\n", - " )\n", - " )\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "cellView": "form", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 113, - "referenced_widgets": [ - "10980c94c8774abbaf1f0b17f97b7ed8", - "f8ec55796f4f4d6f86ca89215912ea86", - "0149873c4f6d4e0884d2d1296d3f0d6e", - "766d8be49c574998b50e9d37dd4d18a8", - "930d8dcb778b431997dede981675a118", - "8d61573d828f47e6bb1629bcb6eddd4a", - "72a2a32094894733ac4e56b1e59274f8", - "06431b47df3846d9a1f3f460afbf9b01", - "f93089db909645a0ac36a592e40fd144", - "58d6876ce9054491a911dfa0a9e07491", - "8821ff76786f4d5394ae8b9ecda9e11a", - "9e94b71146d6483fa8ce4c55439a1847", - "b5e95720844e462db96296f0d6dfbcbf", - "99087faff557404b9d23cbf6bf96eb70", - "42ab927ba46d47879cbc7a26ee8b250e", - "87f49146dee34012b75de55a0c0b0eeb", - "4b8b5abe33ed42828a21478baabe9c77", - "af1de1b8780946c996ab4350c4cccb56" - ] - }, - "id": "mjxF8ZNEbZ2e", - "outputId": "54f90b09-beaf-48a6-cca4-a8fda984b7b0" - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "10980c94c8774abbaf1f0b17f97b7ed8", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(Label(value='File:'), Text(value='', description='Url:', placeholder='put ur url…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "#@title Dialog - Run Cell to begin\n", - "dialog = MapDialog()\n", - "dialog.display_widgets()" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "maptomethod.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "name": "python" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "0149873c4f6d4e0884d2d1296d3f0d6e": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "HBoxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "HBoxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "HBoxView", - "box_style": "", - "children": [ - "IPY_MODEL_06431b47df3846d9a1f3f460afbf9b01", - "IPY_MODEL_f93089db909645a0ac36a592e40fd144" - ], - "layout": "IPY_MODEL_72a2a32094894733ac4e56b1e59274f8" - } - }, - "06431b47df3846d9a1f3f460afbf9b01": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "LabelModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "LabelModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "LabelView", - "description": "", - "description_tooltip": null, - "layout": "IPY_MODEL_42ab927ba46d47879cbc7a26ee8b250e", - "placeholder": "​", - "style": "IPY_MODEL_99087faff557404b9d23cbf6bf96eb70", - "value": "File:" - } - }, - "10980c94c8774abbaf1f0b17f97b7ed8": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "VBoxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "VBoxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "VBoxView", - "box_style": "", - "children": [ - "IPY_MODEL_0149873c4f6d4e0884d2d1296d3f0d6e", - "IPY_MODEL_766d8be49c574998b50e9d37dd4d18a8", - "IPY_MODEL_930d8dcb778b431997dede981675a118", - "IPY_MODEL_8d61573d828f47e6bb1629bcb6eddd4a" - ], - "layout": "IPY_MODEL_f8ec55796f4f4d6f86ca89215912ea86" - } - }, - "42ab927ba46d47879cbc7a26ee8b250e": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "4b8b5abe33ed42828a21478baabe9c77": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "58d6876ce9054491a911dfa0a9e07491": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "state": { - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "DescriptionStyleModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "StyleView", - "description_width": "" - } - }, - "72a2a32094894733ac4e56b1e59274f8": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "766d8be49c574998b50e9d37dd4d18a8": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DropdownModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "DropdownModel", - "_options_labels": [ - "DIN_EN_ISO_527-3", - "DIN_EN_ISO_6506-1" - ], - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "DropdownView", - "description": "method:", - "description_tooltip": null, - "disabled": false, - "index": 0, - "layout": "IPY_MODEL_8821ff76786f4d5394ae8b9ecda9e11a", - "style": "IPY_MODEL_58d6876ce9054491a911dfa0a9e07491" - } - }, - "87f49146dee34012b75de55a0c0b0eeb": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "state": { - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "DescriptionStyleModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "StyleView", - "description_width": "" - } - }, - "8821ff76786f4d5394ae8b9ecda9e11a": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "8d61573d828f47e6bb1629bcb6eddd4a": { - "model_module": "@jupyter-widgets/output", - "model_module_version": "1.0.0", - "model_name": "OutputModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/output", - "_model_module_version": "1.0.0", - "_model_name": "OutputModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/output", - "_view_module_version": "1.0.0", - "_view_name": "OutputView", - "layout": "IPY_MODEL_af1de1b8780946c996ab4350c4cccb56", - "msg_id": "", - "outputs": [] - } - }, - "930d8dcb778b431997dede981675a118": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "ButtonModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "ButtonModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "ButtonView", - "button_style": "", - "description": "Start Mapping!", - "disabled": false, - "icon": "", - "layout": "IPY_MODEL_b5e95720844e462db96296f0d6dfbcbf", - "style": "IPY_MODEL_9e94b71146d6483fa8ce4c55439a1847", - "tooltip": "" - } - }, - "99087faff557404b9d23cbf6bf96eb70": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "state": { - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "DescriptionStyleModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "StyleView", - "description_width": "" - } - }, - "9e94b71146d6483fa8ce4c55439a1847": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "ButtonStyleModel", - "state": { - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "ButtonStyleModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "StyleView", - "button_color": null, - "font_weight": "" - } - }, - "af1de1b8780946c996ab4350c4cccb56": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "b5e95720844e462db96296f0d6dfbcbf": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": "200px" - } - }, - "f8ec55796f4f4d6f86ca89215912ea86": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "state": { - "_model_module": "@jupyter-widgets/base", - "_model_module_version": "1.2.0", - "_model_name": "LayoutModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "LayoutView", - "align_content": null, - "align_items": null, - "align_self": null, - "border": null, - "bottom": null, - "display": null, - "flex": null, - "flex_flow": null, - "grid_area": null, - "grid_auto_columns": null, - "grid_auto_flow": null, - "grid_auto_rows": null, - "grid_column": null, - "grid_gap": null, - "grid_row": null, - "grid_template_areas": null, - "grid_template_columns": null, - "grid_template_rows": null, - "height": null, - "justify_content": null, - "justify_items": null, - "left": null, - "margin": null, - "max_height": null, - "max_width": null, - "min_height": null, - "min_width": null, - "object_fit": null, - "object_position": null, - "order": null, - "overflow": null, - "overflow_x": null, - "overflow_y": null, - "padding": null, - "right": null, - "top": null, - "visibility": null, - "width": null - } - }, - "f93089db909645a0ac36a592e40fd144": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "TextModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "TextModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "TextView", - "continuous_update": true, - "description": "Url:", - "description_tooltip": null, - "disabled": false, - "layout": "IPY_MODEL_4b8b5abe33ed42828a21478baabe9c77", - "placeholder": "put ur url to a *-metadata.json here", - "style": "IPY_MODEL_87f49146dee34012b75de55a0c0b0eeb", - "value": "" - } - } - } - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/maptomethod.py b/maptomethod.py index 612bbd7..54ccb8b 100644 --- a/maptomethod.py +++ b/maptomethod.py @@ -44,10 +44,16 @@ def dict_constructor(loader, node): BFO_URL = "http://purl.obolibrary.org/obo/bfo.owl" IOF_URL = "./ontologies/iof.rdf" IOF = Namespace("https://spec.industrialontologies.org/ontology/core/Core/") -IOF_QUAL_URL = "https://github.com/iofoundry/ontology/raw/'qualities'/qualities/qualities.rdf" +IOF_QUAL_URL = ( + "https://github.com/iofoundry/ontology/raw/'qualities'/qualities/qualities.rdf" +) IOF_QUAL = Namespace("https://spec.industrialontologies.org/ontology/qualities/") -IOF_MAT_URL = "https://github.com/iofoundry/ontology/raw/materials/materials/Materials.rdf" -IOF_MAT = Namespace("https://spec.industrialontologies.org/ontology/materials/Materials/") +IOF_MAT_URL = ( + "https://github.com/iofoundry/ontology/raw/materials/materials/Materials.rdf" +) +IOF_MAT = Namespace( + "https://spec.industrialontologies.org/ontology/materials/Materials/" +) OA = Namespace("http://www.w3.org/ns/oa#") OA_URL = "http://www.w3.org/ns/oa.ttl" @@ -81,6 +87,7 @@ def get_rdflib_Namespaces() -> dict: pass return class_dict + ontologies = get_rdflib_Namespaces() ontologies["BFO"] = {"uri": str(BFO), "src": BFO_URL} ontologies["OA"] = {"uri": str(OA), "src": OA_URL} @@ -90,28 +97,34 @@ def get_rdflib_Namespaces() -> dict: ontologies["IOF-QUAL"] = {"uri": str(IOF_QUAL), "src": IOF_QUAL_URL} -def open_file(uri: AnyUrl,authorization= None) -> Tuple["filedata": str, "filename": str]: +def open_file(uri: AnyUrl, authorization=None) -> Tuple["filedata":str, "filename":str]: try: uri_parsed = urlparse(uri) - #print(uri_parsed, urljoin('file:', pathname2url(os.path.abspath(uri_parsed.path)))) + # print(uri_parsed, urljoin('file:', pathname2url(os.path.abspath(uri_parsed.path)))) except: - raise HTTPException(status_code=400, detail=uri + " is not an uri - if local file add file:// as prefix") + raise HTTPException( + status_code=400, + detail=uri + " is not an uri - if local file add file:// as prefix", + ) else: - if uri_parsed.path.startswith('.'): - #is local file path create a proper file url - uri_parsed = urlparse(urljoin('file:', pathname2url(os.path.abspath(uri_parsed.path)))) + if uri_parsed.path.startswith("."): + # is local file path create a proper file url + uri_parsed = urlparse( + urljoin("file:", pathname2url(os.path.abspath(uri_parsed.path))) + ) filename = unquote(uri_parsed.path).rsplit("/download/upload")[0].split("/")[-1] if uri_parsed.scheme in ["https", "http"]: # r = urlopen(uri) - s= requests.Session() + s = requests.Session() s.verify = SSL_VERIFY s.headers.update({"Authorization": authorization}) r = s.get(uri, allow_redirects=True, stream=True) - - #r.raise_for_status() - if r.status_code!=200: - #logging.debug(r.content) - raise HTTPException(status_code=r.status_code, detail="cant get file at {}".format(uri)) + # print(r.content) + if r.status_code != 200: + # logging.info(r.content) + raise HTTPException( + status_code=r.status_code, detail="cant get file at {}".format(uri) + ) filedata = r.content # charset=r.info().get_content_charset() # if not charset: @@ -120,7 +133,9 @@ def open_file(uri: AnyUrl,authorization= None) -> Tuple["filedata": str, "filena elif uri_parsed.scheme == "file": filedata = open(unquote(uri_parsed.path), "rb").read() else: - raise HTTPException(status_code=400,detail="unknown scheme {}".format(uri_parsed.scheme)) + raise HTTPException( + status_code=400, detail="unknown scheme {}".format(uri_parsed.scheme) + ) return filedata, filename @@ -142,7 +157,7 @@ def get_all_sub_classes(superclass: URIRef, authorization=None) -> List[URIRef]: for key, item in ontologies.items() if ontology_url in item["uri"] ] - #print(ontology_url,result) + # print(ontology_url,result) if result: ontology_url = result[0][1] logging.info( @@ -196,7 +211,6 @@ def get_methods() -> Dict: return methods - # InformtionContentEntity = CCO.InformationContentEntity InformtionContentEntity = IOF.InformationContentEntity TemporalRegionClass = BFO.BFO_0000008 @@ -218,7 +232,7 @@ def __init__( subjects: List[URIRef] = [], objects: List[URIRef] = [], maplist: List[Tuple[str, str]] = [], - authorization=None + authorization=None, ): """Mapper Class for creating Rule based yarrrml mappings for data metadata to link to a knowledge graph. @@ -240,7 +254,7 @@ def __init__( self.method_url = method_url self.use_template_rowwise = use_template_rowwise self.mapping_predicate_uri = mapping_predicate_uri - self.authorization=authorization + self.authorization = authorization logging.debug("checking objects and subjects populated") # file_data, file_name =open_file(data_url) if not objects: @@ -303,7 +317,9 @@ def to_pretty_yaml(self) -> str: return result -def query_entities(data_url: str, entity_classes: List[URIRef],authorization=None) -> dict: +def query_entities( + data_url: str, entity_classes: List[URIRef], authorization=None +) -> dict: """Get all named individuals at data_url location that are of any type in entity_classes. Args: @@ -313,7 +329,9 @@ def query_entities(data_url: str, entity_classes: List[URIRef],authorization=Non Returns: dict: Dict with short entity IRI as key """ - subclasses = [get_all_sub_classes(superclass) for superclass in entity_classes] + subclasses = [ + get_all_sub_classes(superclass, authorization) for superclass in entity_classes + ] class_list = [item for sublist in subclasses for item in sublist] logging.info( "query data at url: {}\nfor entitys classes: {}".format(data_url, class_list) @@ -328,7 +346,7 @@ def query_entities(data_url: str, entity_classes: List[URIRef],authorization=Non # parse template and add mapping results data.parse(data=data_data, format=format) # find base iri if any - print(list(data.namespaces())) + # print(list(data.namespaces())) base_ns = None for ns_prefix, namespace in data.namespaces(): if ns_prefix == "base": @@ -432,7 +450,7 @@ def get_mapping_output( # 'po':[['obo:0010002', 'method:'+str(mapping[0]).split('/')[-1]],] "po": [ [str(mapping_predicate_uri), "method:" + ice_key + "~iri"], - ] + ], #'po': [[str(mapping_predicate_uri), _il+'~iri'], ] } )