Пример #1
0
    def _get_resource(self, path, model):
        """Gets the resource object from the string path.

        Args:
            path (str): Resource path.
            crux.models.Resource: Resource model.

        Returns:
            crux.models.Resource: Resource model Object.

        Raises:
            crux.exceptions.CruxResourceNotFoundError: If resource is not found.
        """
        resource_name, folder_path = split_posixpath_filename_dirpath(path)
        rsrc_list = self._list_resources(
            folder=folder_path,
            limit=1,
            name=resource_name,
            include_folders=True,
            model=model,
        )

        if rsrc_list:
            return rsrc_list[0]
        else:
            # As of now API can't fetch id from the resource path or name,
            # hence raising the 404 error from the Python client
            raise CruxResourceNotFoundError({
                "statusCode": 404,
                "name": resource_name
            })
Пример #2
0
    def create_query(self, path, config, tags=None, description=None):
        # type: (str, Dict[str, Any], List[str], str) -> Query
        """Creates Query resource in Dataset.

        Args:
            path (str): Query resource Path.
            config (dict): Query configuration.
            tags (:obj:`list` of :obj:`str`): Tags of the Query resource.
                Defaults to None.
            description (str): Description of the Query resource.
                Defaults to None.

        Returns:
            crux.models.Query: Query Object.
        """

        query_name, folder = split_posixpath_filename_dirpath(path)

        headers = {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }

        tags = tags if tags else []

        query_resource = Query(
            name=query_name,
            type="query",
            tags=tags,
            description=description,
            config=config,
        )
        query_resource.folder = folder

        return self.connection.api_call(
            "POST",
            ["datasets", self.id, "resources"],
            params=query_resource.to_dict(),
            model=Query,
            headers=headers,
        )
Пример #3
0
    def create_table(self, path, config, tags=None, description=None):
        # type: (str, Dict[str, Any], List[str], str) -> Table
        """Creates Table resource in Dataset.

        Args:
            path (str): Table resource Path.
            config (dict): Table Schema Configuration.
            tags (:obj:`list` of :obj:`str`): Tags of the Table resource.
                Defaults to None.
            description (str): Description of the Table resource.
                Defaults to None.

        Returns:
            crux.models.Table: Table Object
        """

        headers = {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }

        table_name, folder = split_posixpath_filename_dirpath(path)

        tags = tags if tags else []

        table_resource = Table(
            name=table_name,
            type="table",
            tags=tags,
            description=description,
            config=config,
        )
        table_resource.folder = folder

        return self.connection.api_call(
            "POST",
            ["datasets", self.id, "resources"],
            params=table_resource.to_dict(),
            model=Table,
            headers=headers,
        )
Пример #4
0
    def create_folder(self, path, folder="/", tags=None, description=None):
        # type: (str, str, List[str], str) -> Folder
        """Creates Folder resource in Dataset.

        Args:
            path (str): Path of the Folder resource.
            folder (str): Parent folder of the Folder resource.
                Defaults to /.
            tags (:obj:`list` of :obj:`str`): Tags of the Folder resource.
                Defaults to None.
            description (str): Description of the Folder resource.
                Defaults to None.

        Returns:
            crux.models.Folder: Folder Object.
        """

        headers = {
            "Content-Type": "application/json",
            "Accept": "application/json"
        }

        tags = tags if tags else []

        file_name, folder = split_posixpath_filename_dirpath(path)

        folder_resource = Folder(name=file_name,
                                 type="folder",
                                 tags=tags,
                                 description=description)
        folder_resource.folder = folder

        return self.connection.api_call(
            "POST",
            ["datasets", self.id, "resources"],
            params=folder_resource.to_dict(),
            model=Folder,
            headers=headers,
        )
Пример #5
0
def test_split_posixpath_filename_dirpath():
    fullpath = "/tmp/path/to/file.txt"
    filename, dirpath = split_posixpath_filename_dirpath(fullpath)
    assert (filename, dirpath) == ("file.txt", "/tmp/path/to")