Skip to content

Commit

Permalink
custom types added
Browse files Browse the repository at this point in the history
  • Loading branch information
sbilge committed Dec 11, 2024
1 parent ef1fe27 commit 01eb95c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/metldata/builtin_transformations/common/custom_types.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion src/metldata/builtin_transformations/common/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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,
):
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down

0 comments on commit 01eb95c

Please sign in to comment.