diff --git a/src/newcrawler/converters.py b/src/newcrawler/converters.py index 3feb0a9955d1096b7a5625377fd1af9a4e18c2d4..94d50edcf493b857503ddc8edf6dc1f712a21020 100644 --- a/src/newcrawler/converters.py +++ b/src/newcrawler/converters.py @@ -26,6 +26,7 @@ import os import re import caosdb as db +from .utils import has_parent from .stores import GeneralStore, RecordStore from .structure_elements import (StructureElement, Directory, File, TextElement, DictTextElement, DictListElement) @@ -183,9 +184,11 @@ class Converter(object): if "parents" in record: for parent in record["parents"]: - c_record.add_parent(name=parent) + if not has_parent(c_record, parent): + c_record.add_parent(parent) else: - c_record.add_parent(name=name) + if not has_parent(c_record, name): + c_record.add_parent(name) for key, value in record.items(): if key == "parents": diff --git a/src/newcrawler/utils.py b/src/newcrawler/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..a04e896e85e749ba616c30cc450f15bb561dd6df --- /dev/null +++ b/src/newcrawler/utils.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# encoding: utf-8 +# +# ** header v3.0 +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2021 Henrik tom Wörden +# 2021 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 +# + +import caosdb as db + +# Some utility functions, e.g. for extending pylib. + +def has_parent(entity: db.Entity, name: str): + """ + A simple check, whether a parent with the given name exists. + + There is a similar, however more complex function in package caosdb. + """ + + for parent in entity.parents: + if parent.name == name: + return True + return False