Skip to content

Commit

Permalink
refactor terms patch
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 committed Dec 31, 2024
1 parent 68573cb commit f7dfa64
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 139 deletions.
43 changes: 43 additions & 0 deletions metadata-ingestion/src/datahub/specific/aspect_helpers/terms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Union

from typing_extensions import Self

from datahub.emitter.mcp_patch_builder import MetadataPatchProposal
from datahub.metadata.schema_classes import (
GlossaryTermAssociationClass as Term,
GlossaryTermsClass,
)
from datahub.metadata.urns import GlossaryTermUrn, Urn


class HasTermsPatch(MetadataPatchProposal):
def add_term(self, term: Term) -> Self:
"""Adds a glossary term to the entity.
Args:
term: The Term object representing the glossary term to be added.
Returns:
The patch builder instance.
"""
# TODO: Make this support raw strings, in addition to Term objects.
self._add_patch(
GlossaryTermsClass.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> Self:
"""Removes a glossary term from the entity.
Args:
term: The term to remove, specified as a string or Urn object.
Returns:
The patch builder instance.
"""
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = GlossaryTermUrn(term)
self._add_patch(
GlossaryTermsClass.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self
41 changes: 6 additions & 35 deletions metadata-ingestion/src/datahub/specific/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
ChartInfoClass as ChartInfo,
ChartTypeClass,
EdgeClass as Edge,
GlossaryTermAssociationClass as Term,
GlossaryTermsClass as GlossaryTerms,
KafkaAuditHeaderClass,
SystemMetadataClass,
)
from datahub.specific.aspect_helpers.custom_properties import HasCustomPropertiesPatch
from datahub.specific.aspect_helpers.ownership import HasOwnershipPatch
from datahub.specific.aspect_helpers.tags import HasTagsPatch
from datahub.specific.aspect_helpers.terms import HasTermsPatch
from datahub.utilities.urns.urn import Urn


class ChartPatchBuilder(
HasOwnershipPatch, HasCustomPropertiesPatch, HasTagsPatch, MetadataPatchProposal
HasOwnershipPatch,
HasCustomPropertiesPatch,
HasTagsPatch,
HasTermsPatch,
MetadataPatchProposal,
):
def __init__(
self,
Expand Down Expand Up @@ -117,38 +120,6 @@ def set_input_edges(self, inputs: List[Edge]) -> "ChartPatchBuilder":
)
return self

def add_term(self, term: Term) -> "ChartPatchBuilder":
"""
Adds a glossary term to the ChartPatchBuilder.
Args:
term: The Term object representing the glossary term to be added.
Returns:
The ChartPatchBuilder instance.
"""
self._add_patch(
GlossaryTerms.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> "ChartPatchBuilder":
"""
Removes a glossary term from the ChartPatchBuilder.
Args:
term: The term to remove, specified as a string or Urn object.
Returns:
The ChartPatchBuilder instance.
"""
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = "urn:li:glossaryTerm:" + term
self._add_patch(
GlossaryTerms.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self

def set_title(self, title: str) -> "ChartPatchBuilder":
assert title, "ChartInfo title should not be None"
self._add_patch(
Expand Down
41 changes: 6 additions & 35 deletions metadata-ingestion/src/datahub/specific/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
ChangeAuditStampsClass,
DashboardInfoClass as DashboardInfo,
EdgeClass as Edge,
GlossaryTermAssociationClass as Term,
GlossaryTermsClass as GlossaryTerms,
KafkaAuditHeaderClass,
SystemMetadataClass,
)
from datahub.specific.aspect_helpers.custom_properties import HasCustomPropertiesPatch
from datahub.specific.aspect_helpers.ownership import HasOwnershipPatch
from datahub.specific.aspect_helpers.tags import HasTagsPatch
from datahub.specific.aspect_helpers.terms import HasTermsPatch
from datahub.utilities.urns.urn import Urn


class DashboardPatchBuilder(
HasOwnershipPatch, HasCustomPropertiesPatch, HasTagsPatch, MetadataPatchProposal
HasOwnershipPatch,
HasCustomPropertiesPatch,
HasTagsPatch,
HasTermsPatch,
MetadataPatchProposal,
):
def __init__(
self,
Expand Down Expand Up @@ -210,38 +213,6 @@ def set_chart_edges(self, charts: List[Edge]) -> "DashboardPatchBuilder":
)
return self

def add_term(self, term: Term) -> "DashboardPatchBuilder":
"""
Adds a glossary term to the DashboardPatchBuilder.
Args:
term: The Term object representing the glossary term to be added.
Returns:
The DashboardPatchBuilder instance.
"""
self._add_patch(
GlossaryTerms.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> "DashboardPatchBuilder":
"""
Removes a glossary term from the DashboardPatchBuilder.
Args:
term: The term to remove, specified as a string or Urn object.
Returns:
The DashboardPatchBuilder instance.
"""
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = "urn:li:glossaryTerm:" + term
self._add_patch(
GlossaryTerms.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self

def set_title(self, title: str) -> "DashboardPatchBuilder":
assert title, "DashboardInfo title should not be None"
self._add_patch(
Expand Down
41 changes: 6 additions & 35 deletions metadata-ingestion/src/datahub/specific/datajob.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
DataJobInfoClass as DataJobInfo,
DataJobInputOutputClass as DataJobInputOutput,
EdgeClass as Edge,
GlossaryTermAssociationClass as Term,
GlossaryTermsClass as GlossaryTerms,
KafkaAuditHeaderClass,
SystemMetadataClass,
)
from datahub.metadata.urns import SchemaFieldUrn, Urn
from datahub.specific.aspect_helpers.custom_properties import HasCustomPropertiesPatch
from datahub.specific.aspect_helpers.ownership import HasOwnershipPatch
from datahub.specific.aspect_helpers.tags import HasTagsPatch
from datahub.specific.aspect_helpers.terms import HasTermsPatch


class DataJobPatchBuilder(
HasOwnershipPatch, HasCustomPropertiesPatch, HasTagsPatch, MetadataPatchProposal
HasOwnershipPatch,
HasCustomPropertiesPatch,
HasTagsPatch,
HasTermsPatch,
MetadataPatchProposal,
):
def __init__(
self,
Expand Down Expand Up @@ -427,35 +430,3 @@ def set_output_dataset_fields(self, outputs: List[Edge]) -> "DataJobPatchBuilder
value=outputs,
)
return self

def add_term(self, term: Term) -> "DataJobPatchBuilder":
"""
Adds a glossary term to the DataJobPatchBuilder.
Args:
term: The Term object representing the glossary term to be added.
Returns:
The DataJobPatchBuilder instance.
"""
self._add_patch(
GlossaryTerms.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> "DataJobPatchBuilder":
"""
Removes a glossary term from the DataJobPatchBuilder.
Args:
term: The term to remove, specified as a string or Urn object.
Returns:
The DataJobPatchBuilder instance.
"""
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = "urn:li:glossaryTerm:" + term
self._add_patch(
GlossaryTerms.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self
26 changes: 7 additions & 19 deletions metadata-ingestion/src/datahub/specific/dataproduct.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from typing import List, Optional, Tuple, Union
from typing import List, Optional, Tuple

from datahub.emitter.mcp_patch_builder import MetadataPatchProposal, PatchPath
from datahub.metadata.schema_classes import (
DataProductAssociationClass as DataProductAssociation,
DataProductPropertiesClass as DataProductProperties,
GlossaryTermAssociationClass as Term,
GlossaryTermsClass as GlossaryTerms,
KafkaAuditHeaderClass,
SystemMetadataClass,
)
from datahub.specific.aspect_helpers.custom_properties import HasCustomPropertiesPatch
from datahub.specific.aspect_helpers.ownership import HasOwnershipPatch
from datahub.specific.aspect_helpers.tags import HasTagsPatch
from datahub.utilities.urns.urn import Urn
from datahub.specific.aspect_helpers.terms import HasTermsPatch


class DataProductPatchBuilder(
HasOwnershipPatch, HasCustomPropertiesPatch, HasTagsPatch, MetadataPatchProposal
HasOwnershipPatch,
HasCustomPropertiesPatch,
HasTagsPatch,
HasTermsPatch,
MetadataPatchProposal,
):
def __init__(
self,
Expand All @@ -34,20 +36,6 @@ def __init__(
def _custom_properties_location(cls) -> Tuple[str, PatchPath]:
return DataProductProperties.ASPECT_NAME, ("customProperties",)

def add_term(self, term: Term) -> "DataProductPatchBuilder":
self._add_patch(
GlossaryTerms.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> "DataProductPatchBuilder":
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = "urn:li:glossaryTerm:" + term
self._add_patch(
GlossaryTerms.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self

def set_name(self, name: str) -> "DataProductPatchBuilder":
self._add_patch(
DataProductProperties.ASPECT_NAME,
Expand Down
17 changes: 2 additions & 15 deletions metadata-ingestion/src/datahub/specific/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
EditableSchemaMetadataClass as EditableSchemaMetadata,
FineGrainedLineageClass as FineGrainedLineage,
GlossaryTermAssociationClass as Term,
GlossaryTermsClass as GlossaryTerms,
KafkaAuditHeaderClass,
SchemaMetadataClass,
SystemMetadataClass,
Expand All @@ -22,6 +21,7 @@
HasStructuredPropertiesPatch,
)
from datahub.specific.aspect_helpers.tags import HasTagsPatch
from datahub.specific.aspect_helpers.terms import HasTermsPatch
from datahub.utilities.urns.tag_urn import TagUrn
from datahub.utilities.urns.urn import Urn

Expand Down Expand Up @@ -99,6 +99,7 @@ class DatasetPatchBuilder(
HasCustomPropertiesPatch,
HasStructuredPropertiesPatch,
HasTagsPatch,
HasTermsPatch,
MetadataPatchProposal,
):
def __init__(
Expand Down Expand Up @@ -214,20 +215,6 @@ def set_fine_grained_upstream_lineages(
)
return self

def add_term(self, term: Term) -> "DatasetPatchBuilder":
self._add_patch(
GlossaryTerms.ASPECT_NAME, "add", path=("terms", term.urn), value=term
)
return self

def remove_term(self, term: Union[str, Urn]) -> "DatasetPatchBuilder":
if isinstance(term, str) and not term.startswith("urn:li:glossaryTerm:"):
term = "urn:li:glossaryTerm:" + term
self._add_patch(
GlossaryTerms.ASPECT_NAME, "remove", path=("terms", term), value={}
)
return self

def for_field(
self, field_path: str, editable: bool = True
) -> FieldPatchHelper["DatasetPatchBuilder"]:
Expand Down

0 comments on commit f7dfa64

Please sign in to comment.