From 93b66da68e78fca5dcd71260fa14e9985ed1f3c7 Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <a.schlemmer@indiscale.com> Date: Fri, 8 Nov 2024 11:31:44 +0100 Subject: [PATCH] MAINT(structure-elements): moved the ROCrateEntity structure element to separate package --- .../structure_elements/__init__.py | 8 +++ .../rocrate_structure_elements.py | 54 +++++++++++++++++++ .../structure_elements/structure_elements.py | 29 ---------- 3 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 src/caoscrawler/structure_elements/rocrate_structure_elements.py diff --git a/src/caoscrawler/structure_elements/__init__.py b/src/caoscrawler/structure_elements/__init__.py index 4b925a56..351f1069 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 00000000..d816753f --- /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 1e6aac19..67cd1056 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 - -- GitLab