Skip to content

Commit add9892

Browse files
committed
[generator] Updated templates as per latest refactoring changes
1 parent f4819eb commit add9892

File tree

13 files changed

+65
-92
lines changed

13 files changed

+65
-92
lines changed

pyatlan/generator/templates/imports.jinja2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ from pyatlan.model.data_mesh import DataProductsAssetsDSL
127127
from pyatlan.model.contract import DataContractSpec
128128
from pyatlan.model.lineage_ref import LineageRef
129129
from pyatlan.model.utils import construct_object_key
130+
if TYPE_CHECKING:
131+
from pyatlan.client.atlan import AtlanClient

pyatlan/generator/templates/methods/asset/badge.jinja2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
def creator(
55
cls,
66
*,
7+
client: AtlanClient,
78
name: StrictStr,
89
cm_name: str,
910
cm_attribute: str,
@@ -12,6 +13,7 @@
1213
return cls(
1314
status=EntityStatus.ACTIVE,
1415
attributes=Badge.Attributes.create(
16+
client=client,
1517
name=name,
1618
cm_name=cm_name,
1719
cm_attribute=cm_attribute,
@@ -24,6 +26,7 @@
2426
def create(
2527
cls,
2628
*,
29+
client: AtlanClient,
2730
name: StrictStr,
2831
cm_name: str,
2932
cm_attribute: str,
@@ -38,6 +41,7 @@
3841
stacklevel=2,
3942
)
4043
return cls.creator(
44+
client=client,
4145
name=name,
4246
cm_name=cm_name,
4347
cm_attribute=cm_attribute,

pyatlan/generator/templates/methods/asset/connection.jinja2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
def creator(
55
cls,
66
*,
7+
client: AtlanClient,
78
name: str,
89
connector_type: AtlanConnectorType,
910
admin_users: Optional[List[str]] = None,
@@ -12,7 +13,9 @@
1213
host: Optional[str] = None,
1314
port: Optional[int] = None,
1415
) -> Connection:
15-
validate_required_fields(["name", "connector_type"], [name, connector_type])
16+
validate_required_fields(
17+
["client", "name", "connector_type"], [client, name, connector_type]
18+
)
1619
if not admin_users and not admin_groups and not admin_roles:
1720
raise ValueError(
1821
"One of admin_user, admin_groups or admin_roles is required"
@@ -23,6 +26,9 @@
2326
connector_name=connector_type.value,
2427
category=connector_type.category.value,
2528
)
29+
client.user_cache.validate_names(names=admin_users or [])
30+
client.role_cache.validate_idstrs(idstrs=admin_roles or [])
31+
client.group_cache.validate_aliases(aliases=admin_groups or [])
2632
attr.admin_users = set() if admin_users is None else set(admin_users)
2733
attr.admin_groups = set() if admin_groups is None else set(admin_groups)
2834
attr.admin_roles = set() if admin_roles is None else set(admin_roles)
@@ -35,6 +41,7 @@
3541
def create(
3642
cls,
3743
*,
44+
client: AtlanClient,
3845
name: str,
3946
connector_type: AtlanConnectorType,
4047
admin_users: Optional[List[str]] = None,
@@ -50,6 +57,7 @@
5057
stacklevel=2,
5158
)
5259
return cls.creator(
60+
client=client,
5361
name=name,
5462
connector_type=connector_type,
5563
admin_users=admin_users,

pyatlan/generator/templates/methods/asset/data_product.jinja2

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,15 @@
8585
name=name,
8686
)
8787

88-
def get_assets(self, client: Optional[AtlanClient] = None):
88+
def get_assets(self, client: AtlanClient):
8989
"""
9090
Reterieves list of all assets linked to the provided data product.
9191

92-
:param client: connectivity to an Atlan tenant (optional). If not provided, the default client will be used.
92+
:param client: connectivity to an Atlan tenant.
9393
:raises InvalidRequestError: if DataProduct asset DSL cannot be found (does not exist) in Atlan
9494
:raises AtlanError: if there is an issue interacting with the API
9595
:returns: instance of `IndexSearchResults` with list of all assets linked to the provided data product
9696
"""
97-
from pyatlan.client.atlan import AtlanClient
98-
99-
client = AtlanClient.get_current_client() if not client else client
10097
dp_dsl = self.data_product_assets_d_s_l
10198
if not dp_dsl:
10299
raise ErrorCode.MISSING_DATA_PRODUCT_ASSET_DSL.exception_with_parameters()

pyatlan/generator/templates/methods/asset/purpose.jinja2

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
def create_metadata_policy(
2424
cls,
2525
*,
26+
client: AtlanClient,
2627
name: str,
2728
purpose_id: str,
2829
policy_type: AuthPolicyType,
@@ -32,8 +33,8 @@
3233
all_users: bool = False,
3334
) -> AuthPolicy:
3435
validate_required_fields(
35-
["name", "purpose_id", "policy_type", "actions"],
36-
[name, purpose_id, policy_type, actions],
36+
["client", "name", "purpose_id", "policy_type", "actions"],
37+
[client, name, purpose_id, policy_type, actions],
3738
)
3839
target_found = False
3940
policy = AuthPolicy._AuthPolicy__create(name=name) # type: ignore[attr-defined]
@@ -51,12 +52,8 @@
5152
policy.policy_groups = {"public"}
5253
else:
5354
if policy_groups:
54-
from pyatlan.client.atlan import AtlanClient
55-
5655
for group_name in policy_groups:
57-
if not AtlanClient.get_current_client().group_cache.get_id_for_name(
58-
group_name
59-
):
56+
if not client.group_cache.get_id_for_name(group_name):
6057
raise ValueError(
6158
f"Provided group name {group_name} was not found in Atlan."
6259
)
@@ -65,12 +62,8 @@
6562
else:
6663
policy.policy_groups = None
6764
if policy_users:
68-
from pyatlan.client.atlan import AtlanClient
69-
7065
for username in policy_users:
71-
if not AtlanClient.get_current_client().user_cache.get_id_for_name(
72-
username
73-
):
66+
if not client.user_cache.get_id_for_name(username):
7467
raise ValueError(
7568
f"Provided username {username} was not found in Atlan."
7669
)
@@ -87,6 +80,7 @@
8780
def create_data_policy(
8881
cls,
8982
*,
83+
client: AtlanClient,
9084
name: str,
9185
purpose_id: str,
9286
policy_type: AuthPolicyType,
@@ -95,7 +89,8 @@
9589
all_users: bool = False,
9690
) -> AuthPolicy:
9791
validate_required_fields(
98-
["name", "purpose_id", "policy_type"], [name, purpose_id, policy_type]
92+
["client", "name", "purpose_id", "policy_type"],
93+
[client, name, purpose_id, policy_type],
9994
)
10095
policy = AuthPolicy._AuthPolicy__create(name=name) # type: ignore[attr-defined]
10196
policy.policy_actions = {DataAction.SELECT.value}
@@ -112,12 +107,8 @@
112107
policy.policy_groups = {"public"}
113108
else:
114109
if policy_groups:
115-
from pyatlan.client.atlan import AtlanClient
116-
117110
for group_name in policy_groups:
118-
if not AtlanClient.get_current_client().group_cache.get_id_for_name(
119-
group_name
120-
):
111+
if not client.group_cache.get_id_for_name(group_name):
121112
raise ValueError(
122113
f"Provided group name {group_name} was not found in Atlan."
123114
)
@@ -126,12 +117,8 @@
126117
else:
127118
policy.policy_groups = None
128119
if policy_users:
129-
from pyatlan.client.atlan import AtlanClient
130-
131120
for username in policy_users:
132-
if not AtlanClient.get_current_client().user_cache.get_id_for_name(
133-
username
134-
):
121+
if not client.user_cache.get_id_for_name(username):
135122
raise ValueError(
136123
f"Provided username {username} was not found in Atlan."
137124
)

pyatlan/generator/templates/methods/attribute/badge.jinja2

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
def create(
55
cls,
66
*,
7+
client: AtlanClient,
78
name: StrictStr,
89
cm_name: str,
910
cm_attribute: str,
1011
badge_conditions: List[BadgeCondition],
1112
) -> Badge.Attributes:
1213
validate_required_fields(
13-
["name", "cm_name", "cm_attribute", "badge_conditions"],
14-
[name, cm_name, cm_attribute, badge_conditions],
14+
["client", "name", "cm_name", "cm_attribute", "badge_conditions"],
15+
[client, name, cm_name, cm_attribute, badge_conditions],
1516
)
16-
from pyatlan.client.atlan import AtlanClient
17-
18-
client = AtlanClient.get_current_client()
1917
cm_id = client.custom_metadata_cache.get_id_for_name(cm_name)
2018
cm_attr_id = client.custom_metadata_cache.get_attr_id_for_name(
2119
set_name=cm_name, attr_name=cm_attribute

pyatlan/generator/templates/methods/attribute/collection.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
client: AtlanClient,
88
name: str,
99
) -> Collection.Attributes:
10-
validate_required_fields(["name"], [name])
10+
validate_required_fields(["client", "name"], [client, name])
1111
return Collection.Attributes(
1212
name=name,
1313
qualified_name=Collection._generate_qualified_name(client),
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,2 @@
11

22
is_loaded: bool = Field(default=True)
3-
4-
@validator("admin_users")
5-
def admin_users_valid(cls, admin_users, values):
6-
from pyatlan.client.atlan import AtlanClient
7-
8-
if values.get("is_loaded", False):
9-
AtlanClient.get_current_client().user_cache.validate_names(
10-
names=admin_users
11-
)
12-
return admin_users
13-
14-
@validator("admin_roles")
15-
def admin_roles_valid(cls, admin_roles, values):
16-
from pyatlan.client.atlan import AtlanClient
17-
18-
if values.get("is_loaded", False):
19-
AtlanClient.get_current_client().role_cache.validate_idstrs(
20-
idstrs=admin_roles
21-
)
22-
return admin_roles
23-
24-
@validator("admin_groups")
25-
def admin_groups_valid(cls, admin_groups, values):
26-
from pyatlan.client.atlan import AtlanClient
27-
28-
if values.get("is_loaded", False):
29-
AtlanClient.get_current_client().group_cache.validate_aliases(
30-
aliases=admin_groups
31-
)
32-
return admin_groups

pyatlan/generator/templates/referenceable_attributes.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090

9191
type_name: str = Field(default="Referenceable", description='Name of the type definition that defines this instance.'
9292
)
93-
_metadata_proxy: CustomMetadataProxy = PrivateAttr()
93+
_metadata_proxy: CustomMetadataProxy = PrivateAttr(default=None)
9494
attributes: {{entity_def.name}}.Attributes = Field(
9595
default_factory = lambda : {{entity_def.name}}.Attributes(),
9696
description='Map of attributes in the instance and their values. The specific keys of this map will vary '

pyatlan/generator/templates/referenceable_methods.jinja2

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
def __init__(__pydantic_self__, **data: Any) -> None:
33
super().__init__(**data)
44
__pydantic_self__.__fields_set__.update(["attributes", "type_name"])
5-
__pydantic_self__._metadata_proxy = CustomMetadataProxy(
6-
__pydantic_self__.business_attributes
7-
)
85

96
@root_validator(pre=True)
107
def parse_custom_attributes(cls, values):
@@ -24,21 +21,39 @@
2421
return values
2522

2623
def json(self, *args, **kwargs) -> str:
27-
if self._metadata_proxy and self._metadata_proxy.business_attributes:
24+
if not self._metadata_proxy and kwargs.get("client"):
25+
self._metadata_proxy = CustomMetadataProxy(
26+
client=kwargs.get("client"), # type: ignore[arg-type]
27+
business_attributes=self.business_attributes,
28+
)
2829
self.business_attributes = self._metadata_proxy.business_attributes
2930
return super().json(**kwargs)
3031

3132
def validate_required(self):
3233
if not self.create_time or self.created_by:
3334
self.attributes.validate_required()
3435

35-
def get_custom_metadata(self, name: str) -> CustomMetadataDict:
36+
def get_custom_metadata(self, client: AtlanClient, name: str) -> CustomMetadataDict:
37+
if not self._metadata_proxy:
38+
self._metadata_proxy = CustomMetadataProxy(
39+
business_attributes=self.business_attributes, client=client
40+
)
3641
return self._metadata_proxy.get_custom_metadata(name=name)
3742

38-
def set_custom_metadata(self, custom_metadata: CustomMetadataDict):
43+
def set_custom_metadata(
44+
self, client: AtlanClient, custom_metadata: CustomMetadataDict
45+
):
46+
if not self._metadata_proxy:
47+
self._metadata_proxy = CustomMetadataProxy(
48+
business_attributes=self.business_attributes, client=client
49+
)
3950
return self._metadata_proxy.set_custom_metadata(custom_metadata=custom_metadata)
4051

41-
def flush_custom_metadata(self):
52+
def flush_custom_metadata(self, client: AtlanClient):
53+
if not self._metadata_proxy:
54+
self._metadata_proxy = CustomMetadataProxy(
55+
business_attributes=self.business_attributes, client=client
56+
)
4257
self.business_attributes = self._metadata_proxy.business_attributes
4358

4459
@classmethod
@@ -59,13 +74,4 @@
5974

6075
@property
6176
def atlan_tag_names(self) -> List[str]:
62-
from pyatlan.client.atlan import AtlanClient
63-
from pyatlan.model.constants import DELETED_
64-
65-
if self.classification_names:
66-
return [
67-
AtlanClient.get_current_client().atlan_tag_cache.get_name_for_id(tag_id)
68-
or DELETED_
69-
for tag_id in self.classification_names
70-
]
71-
return []
77+
return self.classification_names or []

0 commit comments

Comments
 (0)