|
| 1 | + |
| 2 | +from pyatlan.client.atlan import AtlanClient |
| 3 | +from pyatlan.client.typedef import TypeDefClient |
| 4 | +from pyatlan.model.typedef import CustomMetadataDef, AttributeDef |
| 5 | +from pyatlan.model.enums import AtlanTypeCategory |
| 6 | +from pyatlan.error import LogicError |
| 7 | + |
| 8 | +from typing import Optional |
| 9 | + |
| 10 | +class CustomMetadataCache: |
| 11 | + |
| 12 | + cache_by_id: dict[str, CustomMetadataDef] = dict() |
| 13 | + map_id_to_name: dict[str, str] = dict() |
| 14 | + map_name_to_id: dict[str, str] = dict() |
| 15 | + map_attr_id_to_name: dict[str, dict[str, str]] = dict() |
| 16 | + map_attr_name_to_id: dict[str, dict[str, str]] = dict() |
| 17 | + archived_attr_ids: dict[str, str] = dict() |
| 18 | + |
| 19 | + @classmethod |
| 20 | + def _refresh_cache(cls) -> None: |
| 21 | + response = TypeDefClient(AtlanClient()).get_typedefs(type=AtlanTypeCategory.CUSTOM_METADATA) |
| 22 | + if response is not None: |
| 23 | + cls.cache_by_id = dict() |
| 24 | + cls.map_id_to_name = dict() |
| 25 | + cls.map_name_to_id = dict() |
| 26 | + cls.map_attr_id_to_name = dict() |
| 27 | + cls.map_attr_name_to_id = dict() |
| 28 | + cls.archived_attr_ids = dict() |
| 29 | + for cm in response.custom_metadata_defs: |
| 30 | + type_id = cm.name |
| 31 | + type_name = cm.display_name |
| 32 | + cls.cache_by_id[type_id] = cm |
| 33 | + cls.map_id_to_name[type_id] = type_name |
| 34 | + cls.map_name_to_id[type_name] = type_id |
| 35 | + cls.map_attr_id_to_name[type_id] = dict() |
| 36 | + cls.map_attr_name_to_id[type_id] = dict() |
| 37 | + if cm.attribute_defs: |
| 38 | + for attr in cm.attribute_defs: |
| 39 | + attr_id = attr.name |
| 40 | + attr_name = attr.display_name |
| 41 | + cls.map_attr_id_to_name[type_id][attr_id] = attr_name |
| 42 | + if attr.options and attr.options.is_archived: |
| 43 | + cls.archived_attr_ids[attr_id] = attr_name |
| 44 | + else: |
| 45 | + if attr_name in cls.map_attr_name_to_id[type_id]: |
| 46 | + raise LogicError( |
| 47 | + "Multiple custom attributes with exactly the same name (" + attr_name + ") found for: " + type_name, |
| 48 | + code="ATLAN-PYTHON-500-100" |
| 49 | + ) |
| 50 | + cls.map_attr_name_to_id[type_id][attr_name] = attr_id |
| 51 | + |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def get_id_for_name(cls, name: str) -> Optional[str]: |
| 55 | + """ |
| 56 | + Translate the provided human-readable custom metadata set name to its Atlan-internal ID string. |
| 57 | + """ |
| 58 | + cm_id = cls.map_name_to_id.get(name) |
| 59 | + if cm_id: |
| 60 | + return cm_id |
| 61 | + else: |
| 62 | + # If not found, refresh the cache and look again (could be stale) |
| 63 | + cls._refresh_cache() |
| 64 | + return cls.map_name_to_id.get(name) |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def get_name_for_id(cls, idstr: str) -> Optional[str]: |
| 68 | + """ |
| 69 | + Translate the provided Atlan-internal custom metadata ID string to the human-readable custom metadata set name. |
| 70 | + """ |
| 71 | + cm_name = cls.map_id_to_name.get(idstr) |
| 72 | + if cm_name: |
| 73 | + return cm_name |
| 74 | + else: |
| 75 | + # If not found, refresh the cache and look again (could be stale) |
| 76 | + cls._refresh_cache() |
| 77 | + return cls.map_id_to_name.get(idstr) |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def get_all_custom_attributes(cls, include_deleted: bool=False, force_refresh: bool=False) -> dict[str, list[AttributeDef]]: |
| 81 | + """ |
| 82 | + Retrieve all the custom metadata attributes. The map will be keyed by custom metadata set |
| 83 | + name, and the value will be a listing of all the attributes within that set (with all the details |
| 84 | + of each of those attributes). |
| 85 | + """ |
| 86 | + if len(cls.cache_by_id) == 0 or force_refresh: |
| 87 | + cls._refresh_cache() |
| 88 | + m = {} |
| 89 | + for type_id, cm in cls.cache_by_id.items(): |
| 90 | + type_name = cls.get_name_for_id(type_id) |
| 91 | + attribute_defs = cm.attribute_defs |
| 92 | + if include_deleted: |
| 93 | + to_include = attribute_defs |
| 94 | + else: |
| 95 | + to_include = [] |
| 96 | + if attribute_defs: |
| 97 | + for attr in attribute_defs: |
| 98 | + if not attr.options or not attr.options.is_archived: |
| 99 | + to_include.append(attr) |
| 100 | + m[type_name] = to_include |
| 101 | + return m |
| 102 | + |
| 103 | + @classmethod |
| 104 | + def get_attr_id_for_name(cls, set_name: str, attr_name: str) -> Optional[str]: |
| 105 | + """ |
| 106 | + Translate the provided human-readable custom metadata set and attribute names to the Atlan-internal ID string |
| 107 | + for the attribute. |
| 108 | + """ |
| 109 | + attr_id = None |
| 110 | + set_id = cls.get_id_for_name(set_name) |
| 111 | + if set_id: |
| 112 | + sub_map = cls.map_attr_name_to_id.get(set_id) |
| 113 | + if sub_map: |
| 114 | + attr_id = sub_map.get(attr_name) |
| 115 | + if attr_id: |
| 116 | + # If found, return straight away |
| 117 | + return attr_id |
| 118 | + else: |
| 119 | + # Otherwise, refresh the cache and look again (could be stale) |
| 120 | + cls._refresh_cache() |
| 121 | + sub_map = cls.map_attr_name_to_id.get(set_id) |
| 122 | + if sub_map: |
| 123 | + return sub_map.get(attr_name) |
| 124 | + return None |
| 125 | + |
| 126 | + @classmethod |
| 127 | + def _get_attributes_for_search_results(cls, set_id: str) -> Optional[list[str]]: |
| 128 | + sub_map = cls.map_attr_name_to_id.get(set_id) |
| 129 | + if sub_map: |
| 130 | + attr_ids = sub_map.values() |
| 131 | + dot_names = [] |
| 132 | + for idstr in attr_ids: |
| 133 | + dot_names.append(set_id + "." + idstr) |
| 134 | + return dot_names |
| 135 | + return None |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def get_attributes_for_search_results(cls, set_name: str) -> Optional[list[str]]: |
| 139 | + """ |
| 140 | + Retrieve the full set of custom attributes to include on search results. |
| 141 | + """ |
| 142 | + set_id = cls.get_id_for_name(set_name) |
| 143 | + if set_id: |
| 144 | + dot_names = cls._get_attributes_for_search_results(set_id) |
| 145 | + if dot_names: |
| 146 | + return dot_names |
| 147 | + else: |
| 148 | + cls._refresh_cache() |
| 149 | + return cls._get_attributes_for_search_results(set_id) |
| 150 | + return None |
0 commit comments