diff --git a/src/caoscrawler/structure_elements/__init__.py b/src/caoscrawler/structure_elements/__init__.py index 4b925a567f87febdac7b5547111a468eb0a3253c..351f1069708ec94c0dd27313b6329d89858d4330 100644 --- a/src/caoscrawler/structure_elements/__init__.py +++ b/src/caoscrawler/structure_elements/__init__.py @@ -20,4 +20,12 @@ """Submdule containing all default and optional converters.""" +from .. import utils from .structure_elements import * + +try: + from .rocrate_structure_elements import ROCrateEntity +except ImportError as err: + ROCrateEntity: type = utils.MissingImport( + name="ROCrateEntity", hint="Try installing with the `rocrate` extra option.", + err=err) diff --git a/src/caoscrawler/structure_elements/rocrate_structure_elements.py b/src/caoscrawler/structure_elements/rocrate_structure_elements.py new file mode 100644 index 0000000000000000000000000000000000000000..d816753f20b073500028cc002e1c6d3b1bbf2104 --- /dev/null +++ b/src/caoscrawler/structure_elements/rocrate_structure_elements.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# encoding: utf-8 +# +# ** header v3.0 +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2024 Alexander Schlemmer +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +# ** end header +# + +from rocrate.model.entity import Entity +from .structure_elements import StructureElement + + +class ROCrateEntity(StructureElement): + """ + Store entities contained in ROCrates. + """ + + def __init__(self, folder: str, entity: Entity): + """ + Initializes this ROCrateEntity. + + Arguments: + ---------- + folder: str + The folder that contains the ROCrate data. In case of a zipped ROCrate, this + is a temporary folder that the ROCrate was unzipped to. + The folder is the folder containing the ro-crate-metadata.json. + + entity: Entity + The ROCrate entity that is stored in this structure element. + The entity automatically contains an attribute ".crate" + that stores the ROCrate that this entity belongs to. It can be used + e.g. to look up links to other entities (ROCrate.dereference). + """ + super().__init__(entity.properties()["@id"]) + self.folder = folder + self.entity = entity + diff --git a/src/caoscrawler/structure_elements/structure_elements.py b/src/caoscrawler/structure_elements/structure_elements.py index 1e6aac19a1b9f7740c5b342c679d1055522daedd..67cd1056b382c92485deada2058526a03b6d8535 100644 --- a/src/caoscrawler/structure_elements/structure_elements.py +++ b/src/caoscrawler/structure_elements/structure_elements.py @@ -25,7 +25,6 @@ import warnings import lxml.etree -from rocrate.model.entity import Entity class StructureElement(object): @@ -219,31 +218,3 @@ class XMLAttributeNode(StructureElement): self.value = element.attrib[key] self.key = key self.tag = element - - -class ROCrateEntity(StructureElement): - """ - Store entities contained in ROCrates. - """ - - def __init__(self, folder: str, entity: Entity): - """ - Initializes this ROCrateEntity. - - Arguments: - ---------- - folder: str - The folder that contains the ROCrate data. In case of a zipped ROCrate, this - is a temporary folder that the ROCrate was unzipped to. - The folder is the folder containing the ro-crate-metadata.json. - - entity: Entity - The ROCrate entity that is stored in this structure element. - The entity automatically contains an attribute ".crate" - that stores the ROCrate that this entity belongs to. It can be used - e.g. to look up links to other entities (ROCrate.dereference). - """ - super().__init__(entity.properties()["@id"]) - self.folder = folder - self.entity = entity -