|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Optional |
| 4 | + |
| 5 | +from pydantic.v1 import Field, validator |
| 6 | + |
| 7 | +from pyatlan.model.assets import Asset |
| 8 | +from pyatlan.model.assets.relations import RelationshipAttributes |
| 9 | +from pyatlan.model.core import AtlanObject |
| 10 | +from pyatlan.model.enums import SaveSemantic, AtlasGlossaryTermRelationshipStatus |
| 11 | + |
| 12 | + |
| 13 | +class {{ relationship_info.name | to_cls_name }}(RelationshipAttributes): |
| 14 | + type_name: str = Field( |
| 15 | + allow_mutation=False, |
| 16 | + default="{{ relationship_info.name }}", |
| 17 | + description="{{ relationship_info.description }}", |
| 18 | + ) |
| 19 | + attributes: {{ relationship_info.name | to_cls_name }}.Attributes = Field( # type: ignore[name-defined] |
| 20 | + default_factory=lambda: {{ relationship_info.name | to_cls_name }}.Attributes(), |
| 21 | + description="Map of attributes in the instance and their values", |
| 22 | + ) |
| 23 | + |
| 24 | + class Attributes(AtlanObject): |
| 25 | + {% for attribute_def in relationship_info.attribute_defs -%} |
| 26 | + {{ attribute_def["name"] | to_snake_case }}: Optional[{{ attribute_def["typeName"] | get_type }}] = Field( |
| 27 | + default=None, |
| 28 | + description="{{ attribute_def["description"] }}", |
| 29 | + ) |
| 30 | + {% endfor %} |
| 31 | + |
| 32 | + def __init__(__pydantic_self__, **data: Any) -> None: |
| 33 | + if "attributes" not in data: |
| 34 | + data = {"attributes": data} |
| 35 | + super().__init__(**data) |
| 36 | + __pydantic_self__.__fields_set__.update(["attributes", "type_name"]) |
| 37 | + |
| 38 | + class {{ relationship_info.end_def1["name"] | to_cls_name }}(Asset): |
| 39 | + type_name: str = Field( |
| 40 | + default="{{ relationship_info.name }}", |
| 41 | + description="{{ relationship_info.end_def1["description"] or 'Name of the relationship type that defines the relationship.'}}", |
| 42 | + ) |
| 43 | + relationship_type: str = Field( |
| 44 | + default="{{ relationship_info.name }}", |
| 45 | + description="Fixed typeName for {{ relationship_info.name }}.", |
| 46 | + ) |
| 47 | + relationship_attributes: {{ relationship_info.name | to_cls_name }} = Field( |
| 48 | + default=None, |
| 49 | + description="Attributes of the {{ relationship_info.name }}.", |
| 50 | + ) |
| 51 | + |
| 52 | + @validator("type_name") |
| 53 | + def validate_type_name(cls, v): |
| 54 | + return v |
| 55 | + |
| 56 | + def __init__(__pydantic_self__, **data: Any) -> None: |
| 57 | + super().__init__(**data) |
| 58 | + __pydantic_self__.__fields_set__.update(["type_name", "relationship_type"]) |
| 59 | + |
| 60 | + {% if relationship_info.end_def1 != relationship_info.end_def2 %} |
| 61 | + class {{ relationship_info.end_def2["name"] | to_cls_name }}(Asset): |
| 62 | + type_name: str = Field( |
| 63 | + default="{{ relationship_info.name }}", |
| 64 | + description="{{ relationship_info.end_def2["description"] or 'Name of the relationship type that defines the relationship.'}}", |
| 65 | + ) |
| 66 | + relationship_type: str = Field( |
| 67 | + default="{{ relationship_info.name }}", |
| 68 | + description="Fixed typeName for {{ relationship_info.name }}.", |
| 69 | + ) |
| 70 | + relationship_attributes: {{ relationship_info.name | to_cls_name }} = Field( |
| 71 | + default=None, |
| 72 | + description="Attributes of the {{ relationship_info.name }}.", |
| 73 | + ) |
| 74 | + |
| 75 | + @validator("type_name") |
| 76 | + def validate_type_name(cls, v): |
| 77 | + return v |
| 78 | + |
| 79 | + def __init__(__pydantic_self__, **data: Any) -> None: |
| 80 | + super().__init__(**data) |
| 81 | + __pydantic_self__.__fields_set__.update(["type_name", "relationship_type"]) |
| 82 | + {% endif %} |
| 83 | + |
| 84 | + def {{ relationship_info.end_def1["name"] | to_snake_case }}( |
| 85 | + self, related: Asset, semantic: SaveSemantic = SaveSemantic.REPLACE |
| 86 | + ) -> {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def1["name"] | to_cls_name }}: |
| 87 | + """ |
| 88 | + Build the {{ relationship_info.name | to_cls_name }} relationship (with attributes) into a related object. |
| 89 | + |
| 90 | + :param: related asset to which to build the detailed relationship |
| 91 | + :param: semantic to use for saving the relationship |
| 92 | + :returns: a detailed Atlan relationship that conforms |
| 93 | + to the necessary interface for a related asset |
| 94 | + """ |
| 95 | + if related.guid: |
| 96 | + return {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def1["name"] | to_cls_name }}._create_ref( |
| 97 | + type_name=related.type_name, |
| 98 | + guid=related.guid, |
| 99 | + semantic=semantic, |
| 100 | + relationship_attributes=self, |
| 101 | + ) |
| 102 | + |
| 103 | + # If the related asset does not have a GUID, we use qualifiedName |
| 104 | + return {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def1["name"] | to_cls_name }}._create_ref( |
| 105 | + type_name=related.type_name, |
| 106 | + unique_attributes={"qualifiedName": related.qualified_name}, |
| 107 | + semantic=semantic, |
| 108 | + relationship_attributes=self, |
| 109 | + ) |
| 110 | + |
| 111 | + {% if relationship_info.end_def1 != relationship_info.end_def2 %} |
| 112 | + def {{ relationship_info.end_def2["name"] | to_snake_case }}( |
| 113 | + self, related: Asset, semantic: SaveSemantic = SaveSemantic.REPLACE |
| 114 | + ) -> {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def2["name"] | to_cls_name }}: |
| 115 | + """ |
| 116 | + Build the {{ relationship_info.name | to_cls_name }} relationship (with attributes) into a related object. |
| 117 | + |
| 118 | + :param: related asset to which to build the detailed relationship |
| 119 | + :param: semantic to use for saving the relationship |
| 120 | + :returns: a detailed Atlan relationship that conforms |
| 121 | + to the necessary interface for a related asset |
| 122 | + """ |
| 123 | + if related.guid: |
| 124 | + return {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def2["name"] | to_cls_name }}._create_ref( |
| 125 | + type_name=related.type_name, |
| 126 | + guid=related.guid, |
| 127 | + semantic=semantic, |
| 128 | + relationship_attributes=self, |
| 129 | + ) |
| 130 | + |
| 131 | + # If the related asset does not have a GUID, we use qualifiedName |
| 132 | + return {{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def2["name"] | to_cls_name }}._create_ref( |
| 133 | + type_name=related.type_name, |
| 134 | + unique_attributes={"qualifiedName": related.qualified_name}, |
| 135 | + semantic=semantic, |
| 136 | + relationship_attributes=self, |
| 137 | + ) |
| 138 | + {% endif %} |
| 139 | + |
| 140 | + |
| 141 | +{{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def1["name"] | to_cls_name }}.update_forward_refs() |
| 142 | +{% if relationship_info.end_def1 != relationship_info.end_def2 -%} |
| 143 | +{{ relationship_info.name | to_cls_name }}.{{ relationship_info.end_def2["name"] | to_cls_name }}.update_forward_refs() |
| 144 | +{% endif -%} |
| 145 | +{{ relationship_info.name | to_cls_name }}.update_forward_refs() |
0 commit comments