diff --git a/src/caoscrawler/converters/__init__.py b/src/caoscrawler/converters/__init__.py index 540a4cfca9ff19248baab2bc0fe8d10987d4bd1f..81879b11778f57d389ef2fae20009e0071156ff9 100644 --- a/src/caoscrawler/converters/__init__.py +++ b/src/caoscrawler/converters/__init__.py @@ -23,6 +23,7 @@ from .. import utils from .converters import * from .xml_converter import * +from .rocrate import * try: from .spss import SPSSConverter diff --git a/src/caoscrawler/structure_elements.py b/src/caoscrawler/structure_elements.py index 21ffbc9207994e0ed1c31c23d722891a29f86ba8..b2053421320d52f2fd294cd6c238c7daebbf085c 100644 --- a/src/caoscrawler/structure_elements.py +++ b/src/caoscrawler/structure_elements.py @@ -230,13 +230,13 @@ class ROCrateElement(StructureElement): """ Initializes this ROCrateElement. - The name of the structure element is automatically set from the file path - and the name of the ro-crate separated with a "/". + The name of the structure element is automatically set from the + name of the ro-crate. Arguments: ---------- rocrate: ROCrate The rocrate that was loaded from a file. """ - super().__init__(element.getroottree().getelementpath(element) + "/" + rocrate.name) + super().__init__(rocrate.name) self.rocrate = rocrate diff --git a/unittests/eln_files/PASTA.eln b/unittests/eln_files/PASTA.eln new file mode 100644 index 0000000000000000000000000000000000000000..61866e7d5f57cb32191af6663be230153092e712 Binary files /dev/null and b/unittests/eln_files/PASTA.eln differ diff --git a/unittests/eln_files/records-example.eln b/unittests/eln_files/records-example.eln new file mode 100644 index 0000000000000000000000000000000000000000..09ed53fc179e80a240ab773247d6f9adee71b429 Binary files /dev/null and b/unittests/eln_files/records-example.eln differ diff --git a/unittests/test_rocrate_converter.py b/unittests/test_rocrate_converter.py new file mode 100644 index 0000000000000000000000000000000000000000..cb1fba4a6117d715de0bae1cb01e96963b1683d2 --- /dev/null +++ b/unittests/test_rocrate_converter.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# encoding: utf-8 +# +# This file is a part of the LinkAhead Project. +# +# Copyright (C) 2024 Indiscale GmbH <info@indiscale.com> +# Copyright (C) 2024 Alexander Schlemmer <a.schlemmer@indiscale.com> +# +# 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/>. +# + +""" +test the XML converters +""" +import importlib +import json +import pytest +import sys +import yaml +import os + +from lxml.etree import fromstring +from pathlib import Path + +from rocrate.rocrate import ROCrate + +from caoscrawler.converters import (ELNFileConverter,) +from caoscrawler.scanner import load_definition +from caoscrawler.stores import GeneralStore +from caoscrawler.structure_elements import ROCrateElement, File + + +UNITTESTDIR = Path(__file__).parent + + +@pytest.fixture +def converter_registry(): + converter_registry: dict[str, dict[str, str]] = { + "ELNFile": { + "converter": "ELNFileConverter", + "package": "caoscrawler.converters"}, + } + + for key, value in converter_registry.items(): + module = importlib.import_module(value["package"]) + value["class"] = getattr(module, value["converter"]) + return converter_registry + + + +@pytest.fixture +def basic_eln_converter(converter_registry): + return ELNFileConverter(yaml.safe_load(""" +type: ELNFile +match: .*\\.eln +"""), "TestELNConverter", converter_registry) + + +def test_load_pasta(basic_eln_converter): + """ + Test for loading the .eln example export from PASTA. + """ + f_pasta = File("PASTA.eln", os.path.join(UNITTESTDIR, "eln_files", "PASTA.eln")) + match = basic_eln_converter.match(f_pasta) + assert match is not None + crates = basic_eln_converter.create_children(GeneralStore(), f_pasta) + assert len(crates) == 1 + assert isinstance(crates[0], ROCrateElement) + assert isinstance(crates[0].rocrate, ROCrate)