Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BDRSPS-657 Mappers now rely upon field schemas for controlled vocabs #166

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions abis_mapping/base/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import rdflib

# Local
from . import types
from abis_mapping.types import spatial
from abis_mapping.types import temporal
from abis_mapping.types import schema
from . import types as base_types
from abis_mapping import types
from abis_mapping import utils


# Typing
from typing import Any, Iterator, Optional, final

Expand All @@ -43,7 +42,7 @@ class ABISMapper(abc.ABC):
@abc.abstractmethod
def apply_validation(
self,
data: types.ReadableType,
data: base_types.ReadableType,
**kwargs: Any,
) -> frictionless.Report:
"""Applies Frictionless Validation to Raw Data to Generate Report.
Expand All @@ -59,7 +58,7 @@ def apply_validation(
@abc.abstractmethod
def apply_mapping(
self,
data: types.ReadableType,
data: base_types.ReadableType,
dataset_iri: Optional[rdflib.URIRef] = None,
base_iri: Optional[rdflib.Namespace] = None,
**kwargs: Any,
Expand Down Expand Up @@ -91,14 +90,14 @@ def add_default_dataset(
graph.add((uri, a, utils.namespaces.TERN.RDFDataset))
graph.add((uri, rdflib.DCTERMS.title, rdflib.Literal(self.DATASET_DEFAULT_NAME)))
graph.add((uri, rdflib.DCTERMS.description, rdflib.Literal(self.DATASET_DEFAULT_DESCRIPTION)))
graph.add((uri, rdflib.DCTERMS.issued, temporal.Date.today().to_rdf_literal()))
graph.add((uri, rdflib.DCTERMS.issued, types.temporal.Date.today().to_rdf_literal()))

def add_geometry_supplied_as(
self,
subj: rdflib.graph.Node,
pred: rdflib.graph.Node,
obj: rdflib.graph.Node,
geom: spatial.Geometry,
geom: types.spatial.Geometry,
graph: rdflib.Graph,
) -> None:
"""Add geometry supplied as originally to the graph.
Expand Down Expand Up @@ -177,7 +176,7 @@ def extract_extra_fields(
@classmethod
def extra_fields_schema(
cls,
data: frictionless.Row | types.ReadableType,
data: frictionless.Row | base_types.ReadableType,
full_schema: bool = False
) -> frictionless.Schema:
"""Creates a schema with all extra fields found in data.
Expand Down Expand Up @@ -306,11 +305,27 @@ def schema(

# Read Schema and validate
s_dict = json.loads(schema_file.read_text())
s_class = schema.Schema.model_validate(s_dict, strict=True)
s_class = types.schema.Schema.model_validate(s_dict, strict=True)

# Dump pydantic class to return dict
return s_class.model_dump(exclude_none=discard_optional)

@final
@classmethod
@property
def fields(cls) -> dict[str, types.schema.Field]:
"""Indexed dictionary of all fields' metadata.

Returns:
dict[str, types.schema.Field]: Dictionary of all fields
with name as key..
"""
# Get schema
schema = types.schema.Schema.model_validate(cls.schema())

# Return dictionary of fields
return {f.name: f for f in schema.fields}

@final
@classmethod
@functools.lru_cache
Expand Down
Loading