[docs]classAtlanTagCache:""" Lazily-loaded cache for translating between Atlan-internal ID strings and human-readable names for Atlan tags. """def__init__(self,client:AtlanClient):self.client:AtlanClient=clientself.cache_by_id:Dict[str,AtlanTagDef]={}self.map_id_to_name:Dict[str,str]={}self.map_name_to_id:Dict[str,str]={}self.deleted_ids:Set[str]=set()self.deleted_names:Set[str]=set()self.map_id_to_source_tags_attr_id:Dict[str,str]={}self.lock:Lock=Lock()
[docs]defrefresh_cache(self)->None:""" Refreshes the cache of Atlan tags by requesting the full set of Atlan tags from Atlan. """self._refresh_cache()
[docs]defget_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable Atlan tag name to its Atlan-internal ID string. :param name: human-readable name of the Atlan tag :returns: Atlan-internal ID string of the Atlan tag """returnself._get_id_for_name(name=name)
[docs]defget_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided Atlan-internal classification ID string to the human-readable Atlan tag name. :param idstr: Atlan-internal ID string of the Atlan tag :returns: human-readable name of the Atlan tag """returnself._get_name_for_id(idstr=idstr)
[docs]defget_source_tags_attr_id(self,id:str)->Optional[str]:""" Translate the provided Atlan-internal Atlan tag ID string to the Atlan-internal name of the attribute that captures tag attachment details (for source-synced tags). :param id: Atlan-internal ID string of the Atlan tag :returns: Atlan-internal ID string of the attribute containing source-synced tag attachment details """returnself._get_source_tags_attr_id(id)
def_refresh_cache(self)->None:""" Refreshes the cache of Atlan tags by requesting the full set of Atlan tags from Atlan. """withself.lock:response=self.client.typedef.get(type_category=[AtlanTypeCategory.CLASSIFICATION,AtlanTypeCategory.STRUCT,])ifnotresponseornotresponse.struct_defs:raiseErrorCode.EXPIRED_API_TOKEN.exception_with_parameters()ifresponseisnotNone:self.cache_by_id={}self.map_id_to_name={}self.map_name_to_id={}foratlan_taginresponse.atlan_tag_defs:atlan_tag_id=atlan_tag.nameatlan_tag_name=atlan_tag.display_nameself.cache_by_id[atlan_tag_id]=atlan_tagself.map_id_to_name[atlan_tag_id]=atlan_tag_nameself.map_name_to_id[atlan_tag_name]=atlan_tag_idsourceTagsId=""forattr_definatlan_tag.attribute_defsor[]:ifattr_def.display_name=="sourceTagAttachment":sourceTagsId=attr_def.nameor""self.map_id_to_source_tags_attr_id[atlan_tag_id]=sourceTagsIddef_get_id_for_name(self,name:str)->Optional[str]:""" Translate the provided human-readable Atlan tag name to its Atlan-internal ID string. :param name: human-readable name of the Atlan tag :returns: Atlan-internal ID string of the Atlan tag """cls_id=self.map_name_to_id.get(name)ifnotcls_idandnamenotinself.deleted_names:# If not found, refresh the cache and look again (could be stale)self._refresh_cache()cls_id=self.map_name_to_id.get(name)ifnotcls_id:# If still not found after refresh, mark it as deleted (could be# an entry in an audit log that refers to a classification that# no longer exists)self.deleted_names.add(name)returncls_iddef_get_name_for_id(self,idstr:str)->Optional[str]:""" Translate the provided Atlan-internal classification ID string to the human-readable Atlan tag name. :param idstr: Atlan-internal ID string of the Atlan tag :returns: human-readable name of the Atlan tag """cls_name=self.map_id_to_name.get(idstr)ifnotcls_nameandidstrnotinself.deleted_ids:# If not found, refresh the cache and look again (could be stale)self._refresh_cache()cls_name=self.map_id_to_name.get(idstr)ifnotcls_name:# If still not found after refresh, mark it as deleted (could be# an entry in an audit log that refers to a classification that# no longer exists)self.deleted_ids.add(idstr)returncls_namedef_get_source_tags_attr_id(self,id:str)->Optional[str]:""" Translate the provided Atlan-internal Atlan tag ID string to the Atlan-internal name of the attribute that captures tag attachment details (for source-synced tags). :param id: Atlan-internal ID string of the Atlan tag :returns: Atlan-internal ID string of the attribute containing source-synced tag attachment details """ifidandid.strip():attr_id=self.map_id_to_source_tags_attr_id.get(id)ifattr_idisnotNoneoridinself.deleted_ids:returnattr_idself.refresh_cache()ifattr_id:=self.map_id_to_source_tags_attr_id.get(id):returnattr_idself.deleted_ids.add(id)raiseErrorCode.ATLAN_TAG_NOT_FOUND_BY_ID.exception_with_parameters(id)raiseErrorCode.MISSING_ATLAN_TAG_ID.exception_with_parameters()