github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/python/aistore/sdk/object_attributes.py (about) 1 from typing import Dict 2 3 from requests.structures import CaseInsensitiveDict 4 5 from aistore.sdk.const import ( 6 HEADER_CONTENT_LENGTH, 7 AIS_CHECKSUM_TYPE, 8 AIS_CHECKSUM_VALUE, 9 AIS_ACCESS_TIME, 10 AIS_VERSION, 11 AIS_CUSTOM_MD, 12 ) 13 14 15 # pylint: disable=too-few-public-methods 16 class ObjectAttributes: 17 """ 18 Represents the attributes parsed from the response headers returned from an API call to get an object 19 20 Args: 21 response_headers (CaseInsensitiveDict): Response header dict containing object attributes 22 """ 23 24 def __init__(self, response_headers: CaseInsensitiveDict): 25 self.response_headers = response_headers 26 27 @property 28 def size(self) -> int: 29 """ 30 Size of object content 31 """ 32 return int(self.response_headers.get(HEADER_CONTENT_LENGTH, 0)) 33 34 @property 35 def checksum_type(self) -> str: 36 """ 37 Type of checksum, e.g. xxhash or md5 38 """ 39 return self.response_headers.get(AIS_CHECKSUM_TYPE, "") 40 41 @property 42 def checksum_value(self) -> str: 43 """ 44 Checksum value 45 """ 46 return self.response_headers.get(AIS_CHECKSUM_VALUE, "") 47 48 @property 49 def access_time(self) -> str: 50 """ 51 Time this object was accessed 52 """ 53 return self.response_headers.get(AIS_ACCESS_TIME, "") 54 55 @property 56 def obj_version(self) -> str: 57 """ 58 Object version 59 """ 60 return self.response_headers.get(AIS_VERSION, "") 61 62 @property 63 def custom_metadata(self) -> Dict[str, str]: 64 """ 65 Dictionary of custom metadata 66 """ 67 custom_md_header = self.response_headers.get(AIS_CUSTOM_MD, "") 68 if len(custom_md_header) > 0: 69 return self._parse_custom(custom_md_header) 70 return {} 71 72 @staticmethod 73 def _parse_custom(custom_md_header) -> Dict[str, str]: 74 """ 75 Parse the comma-separated list of optional custom metadata from the custom metadata header 76 Args: 77 custom_md_header: Header containing metadata csv 78 79 Returns: 80 Dictionary of custom metadata 81 """ 82 custom_metadata = {} 83 for entry in custom_md_header.split(","): 84 try: 85 assert isinstance(entry, str) 86 entry_list = entry.strip().split("=") 87 assert len(entry_list) == 2 88 custom_metadata[entry_list[0]] = entry_list[1] 89 except AssertionError: 90 continue 91 return custom_metadata