Skip to content

Commit 3b19d20

Browse files
authored
Merge pull request #117 from atlanhq/AM-448
Am 448
2 parents 48a6a8d + 67bfaee commit 3b19d20

22 files changed

Lines changed: 651 additions & 269 deletions

HISTORY.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
## 0.8.0 (September 11, 20023)
1+
## 1.0.0 (September 13, 2023)
2+
* Initial production release
3+
* Adds column projections for listing users
4+
* Renamed register_client method to set_default_client from AtlanClient
5+
* Removed reset_default_client from AtlanClient
6+
7+
## 0.8.0 (September 11, 2023)
28
* Improve retry strategy for HTTP
39
* Adds generators for developer portal docs
410
* Adds create helper for Links

pyatlan/cache/atlan_tag_cache.py

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Optional
44

55
from pyatlan.model.enums import AtlanTypeCategory
6-
from pyatlan.model.typedef import AtlanTagDef
6+
from pyatlan.model.typedef import AtlanTagDef, TypeDefResponseProvider
77

88

99
class AtlanTagCache:
@@ -12,70 +12,105 @@ class AtlanTagCache:
1212
for Atlan tags.
1313
"""
1414

15-
cache_by_id: dict[str, AtlanTagDef] = dict()
16-
map_id_to_name: dict[str, str] = dict()
17-
map_name_to_id: dict[str, str] = dict()
18-
deleted_ids: set[str] = set()
19-
deleted_names: set[str] = set()
15+
caches: dict[int, "AtlanTagCache"] = {}
16+
17+
@classmethod
18+
def get_cache(cls) -> "AtlanTagCache":
19+
from pyatlan.client.atlan import AtlanClient
20+
21+
client = AtlanClient.get_default_client()
22+
cache_key = client.cache_key
23+
if cache_key not in cls.caches:
24+
cls.caches[cache_key] = AtlanTagCache(provider=client)
25+
return cls.caches[cache_key]
2026

2127
@classmethod
2228
def refresh_cache(cls) -> None:
2329
"""
2430
Refreshes the cache of Atlan tags by requesting the full set of Atlan tags from Atlan.
2531
"""
26-
from pyatlan.client.atlan import AtlanClient
32+
cls.get_cache()._refresh_cache()
2733

28-
client = AtlanClient.get_default_client()
29-
if client is None:
30-
client = AtlanClient()
31-
response = client.get_typedefs(type_category=AtlanTypeCategory.CLASSIFICATION)
34+
@classmethod
35+
def get_id_for_name(cls, name: str) -> Optional[str]:
36+
"""
37+
Translate the provided human-readable Atlan tag name to its Atlan-internal ID string.
38+
39+
:param name: human-readable name of the Atlan tag
40+
:returns: Atlan-internal ID string of the Atlan tag
41+
"""
42+
return cls.get_cache()._get_id_for_name(name=name)
43+
44+
@classmethod
45+
def get_name_for_id(cls, idstr: str) -> Optional[str]:
46+
"""
47+
Translate the provided Atlan-internal classification ID string to the human-readable Atlan tag name.
48+
49+
:param idstr: Atlan-internal ID string of the Atlan tag
50+
:returns: human-readable name of the Atlan tag
51+
"""
52+
return cls.get_cache()._get_name_for_id(idstr=idstr)
53+
54+
def __init__(self, provider: TypeDefResponseProvider):
55+
self.provider = provider
56+
self.cache_by_id: dict[str, AtlanTagDef] = {}
57+
self.map_id_to_name: dict[str, str] = {}
58+
self.map_name_to_id: dict[str, str] = {}
59+
self.deleted_ids: set[str] = set()
60+
self.deleted_names: set[str] = set()
61+
62+
def _refresh_cache(self) -> None:
63+
"""
64+
Refreshes the cache of Atlan tags by requesting the full set of Atlan tags from Atlan.
65+
"""
66+
response = self.provider.get_typedefs(
67+
type_category=AtlanTypeCategory.CLASSIFICATION
68+
)
3269
if response is not None:
33-
cls.cache_by_id = {}
34-
cls.map_id_to_name = {}
35-
cls.map_name_to_id = {}
70+
self.cache_by_id = {}
71+
self.map_id_to_name = {}
72+
self.map_name_to_id = {}
3673
for atlan_tag in response.atlan_tag_defs:
3774
atlan_tag_id = atlan_tag.name
3875
atlan_tag_name = atlan_tag.display_name
39-
cls.cache_by_id[atlan_tag_id] = atlan_tag
40-
cls.map_id_to_name[atlan_tag_id] = atlan_tag_name
41-
cls.map_name_to_id[atlan_tag_name] = atlan_tag_id
76+
self.cache_by_id[atlan_tag_id] = atlan_tag
77+
self.map_id_to_name[atlan_tag_id] = atlan_tag_name
78+
self.map_name_to_id[atlan_tag_name] = atlan_tag_id
4279

43-
@classmethod
44-
def get_id_for_name(cls, name: str) -> Optional[str]:
80+
def _get_id_for_name(self, name: str) -> Optional[str]:
4581
"""
4682
Translate the provided human-readable Atlan tag name to its Atlan-internal ID string.
4783
4884
:param name: human-readable name of the Atlan tag
4985
:returns: Atlan-internal ID string of the Atlan tag
5086
"""
51-
cls_id = cls.map_name_to_id.get(name)
52-
if not cls_id and name not in cls.deleted_names:
87+
cls_id = self.map_name_to_id.get(name)
88+
if not cls_id and name not in self.deleted_names:
5389
# If not found, refresh the cache and look again (could be stale)
54-
cls.refresh_cache()
55-
cls_id = cls.map_name_to_id.get(name)
90+
self._refresh_cache()
91+
cls_id = self.map_name_to_id.get(name)
5692
if not cls_id:
5793
# If still not found after refresh, mark it as deleted (could be
5894
# an entry in an audit log that refers to a classification that
5995
# no longer exists)
60-
cls.deleted_names.add(name)
96+
self.deleted_names.add(name)
6197
return cls_id
6298

63-
@classmethod
64-
def get_name_for_id(cls, idstr: str) -> Optional[str]:
99+
def _get_name_for_id(self, idstr: str) -> Optional[str]:
65100
"""
66101
Translate the provided Atlan-internal classification ID string to the human-readable Atlan tag name.
67102
68103
:param idstr: Atlan-internal ID string of the Atlan tag
69104
:returns: human-readable name of the Atlan tag
70105
"""
71-
cls_name = cls.map_id_to_name.get(idstr)
72-
if not cls_name and idstr not in cls.deleted_ids:
106+
cls_name = self.map_id_to_name.get(idstr)
107+
if not cls_name and idstr not in self.deleted_ids:
73108
# If not found, refresh the cache and look again (could be stale)
74-
cls.refresh_cache()
75-
cls_name = cls.map_id_to_name.get(idstr)
109+
self._refresh_cache()
110+
cls_name = self.map_id_to_name.get(idstr)
76111
if not cls_name:
77112
# If still not found after refresh, mark it as deleted (could be
78113
# an entry in an audit log that refers to a classification that
79114
# no longer exists)
80-
cls.deleted_ids.add(idstr)
115+
self.deleted_ids.add(idstr)
81116
return cls_name

0 commit comments

Comments
 (0)