From 01eb95cf689ae66e6afedda57946110adaafeb84 Mon Sep 17 00:00:00 2001 From: sbilge Date: Wed, 11 Dec 2024 11:11:24 +0000 Subject: [PATCH] custom types added --- .../common/custom_types.py | 25 +++++++++++++++++++ .../common/instruction.py | 2 +- .../count_content_values/data_transform.py | 24 ++++++++++++------ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/metldata/builtin_transformations/common/custom_types.py diff --git a/src/metldata/builtin_transformations/common/custom_types.py b/src/metldata/builtin_transformations/common/custom_types.py new file mode 100644 index 0000000..2ce3b76 --- /dev/null +++ b/src/metldata/builtin_transformations/common/custom_types.py @@ -0,0 +1,25 @@ +# Copyright 2021 - 2024 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln +# for the German Human Genome-Phenome Archive (GHGA) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +"""A collection of custom types used for builtin transformations.""" + +from typing import Any, TypeAlias + +ResourceId: TypeAlias = str +MutableDatapack: TypeAlias = dict[str, Any] +MutableResource: TypeAlias = dict[str, dict] +MutableClassResources: TypeAlias = dict[ResourceId, MutableResource] +MutableResourceContent: TypeAlias = dict[str, Any] diff --git a/src/metldata/builtin_transformations/common/instruction.py b/src/metldata/builtin_transformations/common/instruction.py index 05591cd..c53d1b0 100644 --- a/src/metldata/builtin_transformations/common/instruction.py +++ b/src/metldata/builtin_transformations/common/instruction.py @@ -17,7 +17,7 @@ from typing import Protocol, TypeVar -from metldata.builtin_transformations.common.contentschema import NewContentSchemaPath +from metldata.builtin_transformations.common import NewContentSchemaPath class InstructionProtocol(Protocol): diff --git a/src/metldata/builtin_transformations/count_content_values/data_transform.py b/src/metldata/builtin_transformations/count_content_values/data_transform.py index d2fe549..0cc6353 100644 --- a/src/metldata/builtin_transformations/count_content_values/data_transform.py +++ b/src/metldata/builtin_transformations/count_content_values/data_transform.py @@ -16,7 +16,6 @@ """Data transformation logic for count content values transformation.""" from collections import Counter -from typing import Any from schemapack._internals.spec.custom_types import ResourceId from schemapack._internals.spec.datapack import ResourceIdSet @@ -25,6 +24,12 @@ from metldata.builtin_transformations.add_content_properties.path import ( resolve_data_object_path, ) +from metldata.builtin_transformations.common.custom_types import ( + MutableClassResources, + MutableDatapack, + MutableResource, + MutableResourceContent, +) from metldata.builtin_transformations.common.path.path_utils import ( get_directly_referenced_class, ) @@ -37,7 +42,9 @@ ) -def get_class_resources(*, data: dict, class_name: str) -> dict: +def get_class_resources( + *, data: MutableDatapack, class_name: str +) -> MutableClassResources: """Extract the resources of a given class from the dictionary.""" resources = data.get("resources", {}).get(class_name) if not resources: @@ -61,7 +68,10 @@ def count_content( def transform_class( - *, class_name: str, data: dict, instructions: list[CountContentValueInstruction] + *, + class_name: str, + data: MutableDatapack, + instructions: list[CountContentValueInstruction], ): """Apply the count content value transformations to the specified class.""" # the target prefix refers to resources that will be modified by the transformation @@ -88,8 +98,8 @@ def transform_class( def transform_resource( *, - referenced_resources: dict[str, Any], - target_resource: dict, + referenced_resources: MutableClassResources, + target_resource: MutableResource, relation_name: str, instruction: CountContentValueInstruction, ): @@ -117,7 +127,7 @@ def transform_resource( def get_values_to_count( *, relation_target_ids: ResourceId | ResourceIdSet, - referenced_resources: dict[str, Any], + referenced_resources: MutableClassResources, content_path: str, ): """Get countable properties from all resources referred to by the relation.""" @@ -131,7 +141,7 @@ def get_values_to_count( def get_modification_target( - *, data: dict[str, Any], instruction: CountContentValueInstruction + *, data: MutableResourceContent, instruction: CountContentValueInstruction ): """Get the json object that is to be modified.""" path = instruction.target_content.object_path