Beispiel #1
0
    def _get_dataset(self, name: str) -> Dict[str, Any]:
        """Get the information of the TensorBay dataset with the input name.

        Arguments:
            name: The name of the requested dataset.

        Returns:
            The dict of dataset information.

        Raises:
            ResourceNotExistError: When the required dataset does not exist.

        """
        if not name:
            raise ResourceNotExistError(resource="dataset",
                                        identification=name)

        try:
            response = self._list_datasets(name=name)
            info = response["datasets"][0]
        except IndexError as error:
            raise ResourceNotExistError(resource="dataset",
                                        identification=name) from error

        dataset_id = info["id"]
        response = self._client.open_api_do("GET", "", dataset_id).json()
        response["id"] = dataset_id
        return response
    def get_commit(self, revision: Optional[str] = None) -> Commit:
        """Get the certain commit with the given revision.

        Get the certain commit with the given revision. If the revision is not given,
        get the commit based on the commit id stored in the dataset client.

        Arguments:
            revision: The information to locate the specific commit, which can be the commit id,
                the branch name, or the tag name.
                If is not given, get the current commit.

        Returns:
            The :class:`.Commit` instance with the given revision.

        Raises:
            TypeError: When the given revision is illegal.
            ResourceNotExistError: When the required commit does not exist.

        """
        if revision is None:
            self._status.check_authority_for_commit()
            revision = self._status.commit_id

        if not revision:
            raise TypeError("The given revision is illegal")

        try:
            commit = next(self._generate_commits(revision))
        except StopIteration as error:
            raise ResourceNotExistError(resource="commit", identification=revision) from error

        return commit
    def get_draft(self, draft_number: Optional[int] = None) -> Draft:
        """Get the certain draft with the given draft number.

        Get the certain draft with the given draft number. If the draft number is not given,
        get the draft based on the draft number stored in the dataset client.

        Arguments:
            draft_number: The required draft number.
                If is not given, get the current draft.

        Returns:
            The :class:`.Draft` instance with the given number.

        Raises:
            TypeError: When the given draft number is illegal.
            ResourceNotExistError: When the required draft does not exist.

        """
        if draft_number is None:
            self._status.check_authority_for_draft()
            draft_number = self._status.draft_number

        if not draft_number:
            raise TypeError("The given draft number is illegal")

        for draft in self.list_drafts():
            if draft_number == draft.number:
                return draft

        raise ResourceNotExistError(resource="draft", identification=draft_number)
    def get_segment(self, name: str = "default") -> FusionSegmentClient:
        """Get a fusion segment in a certain commit according to given name.

        Arguments:
            name: The name of the required fusion segment.

        Returns:
            The required :class:`~tensorbay.client.segment.FusionSegmentClient`.

        Raises:
            ResourceNotExistError: When the required fusion segment does not exist.

        """
        if name not in self.list_segment_names():
            raise ResourceNotExistError(resource="segment",
                                        identification=name)
        return FusionSegmentClient(name, self)
Beispiel #5
0
    def get_benchmark(self, name: str) -> Benchmark:
        """Get a benchmark instance by name.

        Arguments:
            name: Name of the Benchmark.

        Returns:
            The Benchmark instance with the given name.

        Raises:
            ResourceNotExistError: When the required benchmark does not exist.

        """
        for benchmark in self.list_benchmarks():
            if benchmark.name == name:
                return benchmark

        raise ResourceNotExistError(resource="benchmark", identification=name)
    def _get_data_details(self, remote_path: str) -> Dict[str, Any]:
        params: Dict[str, Any] = {
            "segmentName": self._name,
            "remotePath": remote_path,
        }
        params.update(self._status.get_status_info())

        if config.is_internal:
            params["isInternal"] = True

        response = self._client.open_api_do("GET",
                                            "data/details",
                                            self._dataset_id,
                                            params=params)
        try:
            data_details = response.json()["dataDetails"][0]
        except IndexError as error:
            raise ResourceNotExistError(resource="data",
                                        identification=remote_path) from error

        return data_details  # type: ignore[no-any-return]
    def _get_mask_url(self, mask_type: str, remote_path: str) -> str:
        params: Dict[str, Any] = {
            "segmentName": self._name,
            "maskType": mask_type,
            "remotePath": remote_path,
        }
        params.update(self._status.get_status_info())

        if config.is_internal:
            params["isInternal"] = True

        response = self._client.open_api_do("GET",
                                            "masks/urls",
                                            self._dataset_id,
                                            params=params)
        try:
            mask_url = response.json()["urls"][0]["url"]
        except IndexError as error:
            raise ResourceNotExistError(resource="{mask_type} of data",
                                        identification=remote_path) from error

        return mask_url  # type: ignore[no-any-return]
    def get_data(self, remote_path: str) -> RemoteData:
        """Get required Data object from a dataset segment.

        Arguments:
            remote_path: The remote paths of the required data.

        Returns:
            :class:`~tensorbay.dataset.data.RemoteData`.

        Raises:
            ResourceNotExistError: When the required data does not exist.

        """
        if not remote_path:
            raise ResourceNotExistError(resource="data",
                                        identification=remote_path)

        data_details = self._get_data_details(remote_path)
        data = RemoteData.from_response_body(
            data_details,
            url=URL(data_details["url"], lambda: self._get_url(remote_path)),
            cache_path=self._cache_path,
        )
        label = data.label

        for key in _MASK_KEYS:
            mask = getattr(label, key, None)
            if mask:
                mask.url = URL(
                    data_details["label"][key.upper()]["url"],
                    lambda k=key.upper(), r=remote_path:
                    (  # type: ignore[misc, arg-type]
                        self._get_mask_url(k, r)),
                )
                mask.cache_path = os.path.join(self._cache_path, key,
                                               mask.path)

        return data
    def get_tag(self, name: str) -> Tag:
        """Get the certain tag with the given name.

        Arguments:
            name: The required tag name.

        Returns:
            The :class:`.Tag` instance with the given name.

        Raises:
            TypeError: When the given tag is illegal.
            ResourceNotExistError: When the required tag does not exist.

        """
        if not name:
            raise TypeError("The given tag name is illegal")

        try:
            tag = next(self._generate_tags(name))
        except StopIteration as error:
            raise ResourceNotExistError(resource="tag", identification=name) from error

        return tag
    def get_branch(self, name: str) -> Branch:
        """Get the branch with the given name.

        Arguments:
            name: The required branch name.

        Returns:
            The :class:`.Branch` instance with the given name.

        Raises:
            TypeError: When the given branch is illegal.
            ResourceNotExistError: When the required branch does not exist.

        """
        if not name:
            raise TypeError("The given branch name is illegal")

        try:
            branch = next(self._generate_branches(name))
        except StopIteration as error:
            raise ResourceNotExistError(resource="branch", identification=name) from error

        return branch
Beispiel #11
0
    def get_auth_storage_config(self, name: str) -> StorageConfig:
        """Get the auth storage config with the given name.

        Arguments:
            name: The required auth storage config name.

        Returns:
            The auth storage config with the given name.

        Raises:
            TypeError: When the given auth storage config is illegal.
            ResourceNotExistError: When the required auth storage config does not exist.

        """
        if not name:
            raise TypeError("The given auth storage config name is illegal")

        try:
            config = next(self._generate_auth_storage_configs(name))
        except StopIteration as error:
            raise ResourceNotExistError(resource="auth storage config",
                                        identification=name) from error

        return config