|
| 1 | +""" |
| 2 | +This module provides the Collection class, an extension to the openMINDS Collection |
| 3 | +that knows how to upload metadata to the KG. |
| 4 | +""" |
| 5 | + |
| 6 | +# Copyright 2018-2024 CNRS |
| 7 | + |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | + |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +from importlib import import_module |
| 21 | +import os |
| 22 | +from time import sleep |
| 23 | +from uuid import uuid4 |
| 24 | +from warnings import warn |
| 25 | + |
| 26 | +from openminds import Collection as OMCollection |
| 27 | +from .utility import ActivityLog |
| 28 | +from .errors import AuthenticationError |
| 29 | + |
| 30 | + |
| 31 | +class Collection(OMCollection): |
| 32 | + """ |
| 33 | + A collection of metadata nodes that can be saved to |
| 34 | + and loaded from disk, and uploaded to the KG. |
| 35 | +
|
| 36 | + Args |
| 37 | + ---- |
| 38 | +
|
| 39 | + *nodes (LinkedMetadata): |
| 40 | + Nodes to store in the collection when creating it. |
| 41 | + Child nodes that are referenced from the explicitly |
| 42 | + listed nodes will also be added. |
| 43 | + """ |
| 44 | + |
| 45 | + def load(self, *paths): |
| 46 | + import_module("fairgraph.openminds") |
| 47 | + super().load(*paths) |
| 48 | + |
| 49 | + def upload(self, client, default_space=None, space_map=None, verbosity=0): |
| 50 | + # if not self.complete: |
| 51 | + # raise Exception("Collection contains local ids. Run `generate_ids()` and then re-save the collection") |
| 52 | + # # self.generate_ids(lambda node: client.uri_from_uuid(uuid4())) |
| 53 | + |
| 54 | + nodes_to_save = [ |
| 55 | + node |
| 56 | + for node in self.sort_nodes_for_upload() |
| 57 | + if not node.id.startswith("https://openminds.om-i.org/instances") |
| 58 | + ] |
| 59 | + activity_log = ActivityLog() |
| 60 | + |
| 61 | + if verbosity == 1: |
| 62 | + try: |
| 63 | + tqdm = import_module("tqdm") |
| 64 | + except ImportError: |
| 65 | + warn("Unable to show progress bar, please install tqdm") |
| 66 | + else: |
| 67 | + nodes_to_save = tqdm.tqdm(nodes_to_save) |
| 68 | + |
| 69 | + if os.path.exists(".kg_upload_log.txt"): |
| 70 | + with open(".kg_upload_log.txt") as fp: |
| 71 | + skip = fp.read().strip().split("\n") |
| 72 | + else: |
| 73 | + skip = None |
| 74 | + |
| 75 | + for i, node in enumerate(nodes_to_save): |
| 76 | + if not (skip and node.id in skip): |
| 77 | + if verbosity == 2: |
| 78 | + print(f"[{100*i//len(nodes_to_save)}%] Saving {node.__class__.__name__} {node.id}") |
| 79 | + if space_map: |
| 80 | + target_space = space_map.get(node.__class__, default_space) |
| 81 | + else: |
| 82 | + target_space = default_space |
| 83 | + original_node_id = node.id |
| 84 | + try: |
| 85 | + node.save( |
| 86 | + client, space=target_space, recursive=False, ignore_duplicates=True, activity_log=activity_log |
| 87 | + ) |
| 88 | + except AuthenticationError as err: |
| 89 | + # client.refresh() |
| 90 | + print(err) |
| 91 | + break |
| 92 | + except Exception as err: |
| 93 | + if "500" in str(err): |
| 94 | + sleep(5) |
| 95 | + node.save( |
| 96 | + client, |
| 97 | + space=target_space, |
| 98 | + recursive=False, |
| 99 | + ignore_duplicates=True, |
| 100 | + activity_log=activity_log, |
| 101 | + ) |
| 102 | + else: |
| 103 | + raise |
| 104 | + else: |
| 105 | + with open(".kg_upload_log.txt", "a") as fp: |
| 106 | + fp.write(f"{original_node_id}\n") |
| 107 | + |
| 108 | + return activity_log |
0 commit comments