github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-wrapper/lakefs/tag.py (about)

     1  """
     2  Module containing lakeFS tag implementation
     3  """
     4  
     5  from __future__ import annotations
     6  from typing import Optional
     7  
     8  import lakefs_sdk
     9  
    10  from lakefs.client import Client
    11  from lakefs.exceptions import api_exception_handler, LakeFSException, ConflictException
    12  from lakefs.reference import Reference, ReferenceType
    13  
    14  
    15  class Tag(Reference):
    16      """
    17      Class representing a tag in lakeFS.
    18      """
    19  
    20      def __init__(self, repository_id: str, tag_id: str, client: Optional[Client] = None):
    21          super().__init__(repository_id, reference_id=tag_id, client=client)
    22  
    23      def create(self, source_ref: ReferenceType, exist_ok: Optional[bool] = False) -> Tag:
    24          """
    25          Create a tag from the given source_ref
    26  
    27          :param source_ref: The reference to create the tag on (either ID or Reference object)
    28          :param exist_ok: If True returns the existing Tag reference otherwise raises exception
    29          :return: A lakeFS SDK Tag object
    30          :raise NotAuthorizedException: if user is not authorized to perform this operation
    31          :raise NotFoundException: if source_ref_id doesn't exist on the lakeFS server
    32          :raise ServerException: for any other errors.
    33          """
    34          source_ref_id = source_ref if isinstance(source_ref, str) else source_ref.id
    35          tag_creation = lakefs_sdk.TagCreation(id=self.id, ref=source_ref_id)
    36  
    37          def handle_conflict(e: LakeFSException):
    38              if not (isinstance(e, ConflictException) and exist_ok):
    39                  return e
    40              return None
    41  
    42          with api_exception_handler(handle_conflict):
    43              self._client.sdk_client.tags_api.create_tag(self._repo_id, tag_creation)
    44  
    45          return self
    46  
    47      def delete(self) -> None:
    48          """
    49          Delete the tag from the lakeFS server
    50  
    51          :raise NotAuthorizedException: if user is not authorized to perform this operation
    52          :raise NotFoundException: if source_ref_id doesn't exist on the lakeFS server
    53          :raise ServerException: for any other errors
    54          """
    55          with api_exception_handler():
    56              self._client.sdk_client.tags_api.delete_tag(self._repo_id, self.id)
    57              self._commit = None