Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add limit, offset and include_deleted to show_folder #494

Merged
merged 25 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5b889cf
add limit and offset to show_folder
bernt-matthias Jan 29, 2025
c49b94a
add include_deleted
bernt-matthias Jan 30, 2025
f5a26d6
Simply test subfolder creation
bernt-matthias Jan 30, 2025
107d985
Just use the upstream default values
bernt-matthias Jan 30, 2025
82dc9cb
Revert to loop and drop subfolders variable
bernt-matthias Jan 30, 2025
5f22c37
add test for include_deleted
bernt-matthias Jan 30, 2025
b1d43f7
Merge branch 'folder-update' of https://github.com/bernt-matthias/bio…
bernt-matthias Jan 30, 2025
7a36f0a
Simplify test
bernt-matthias Jan 30, 2025
40a7810
Simplify test
bernt-matthias Jan 30, 2025
e64a4ca
test content counts
bernt-matthias Jan 30, 2025
a7c2ff6
fix test and add docs
bernt-matthias Jan 30, 2025
11e8793
total_rows became available only after
bernt-matthias Jan 31, 2025
30c4c91
add contents generator
bernt-matthias Jan 31, 2025
4a46da3
Rename content to content_iter
bernt-matthias Feb 1, 2025
687d281
Use sys.maxsize for iteration
bernt-matthias Feb 4, 2025
2f93514
overload show_folder
bernt-matthias Feb 4, 2025
a90a19d
Apply suggestions from code review
bernt-matthias Feb 4, 2025
9317cb1
Update bioblend/_tests/TestGalaxyFolders.py
bernt-matthias Feb 4, 2025
b442c17
revert accidental change
bernt-matthias Feb 4, 2025
080f24a
rewording
bernt-matthias Feb 4, 2025
136fc5e
return is Iterator
bernt-matthias Feb 4, 2025
7a6ee4a
rename limit
bernt-matthias Feb 4, 2025
42b8c06
Merge branch 'folder-update' of https://github.com/bernt-matthias/bio…
bernt-matthias Feb 4, 2025
9ce5013
finish parameter renaming
bernt-matthias Feb 4, 2025
a0b9337
more missing renaming
bernt-matthias Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion bioblend/_tests/TestGalaxyFolders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
List,
)

from . import GalaxyTestBase
from . import (
GalaxyTestBase,
test_util,
)

FOO_DATA = "foo\nbar\n"

Expand Down Expand Up @@ -36,6 +39,69 @@ def test_show_folder_contents(self):
assert "metadata" in f2
assert self.name == f2["metadata"]["folder_name"]

@test_util.skip_unless_galaxy("release_21.05")
def test_show_folder_contents_limit(self):
for i in range(12):
self.gi.folders.create_folder(self.folder["id"], f"{self.name} {i}")

# check defaults for limit and offset
f2 = self.gi.folders.show_folder(self.folder["id"], contents=True)
assert len(f2["folder_contents"]) == 10
assert f2["folder_contents"][0]["name"] == f"{self.name} 0"

# check non defaults
f2 = self.gi.folders.show_folder(self.folder["id"], contents=True, limit=1, offset=1)
assert len(f2["folder_contents"]) == 1
assert f2["folder_contents"][0]["name"] == f"{self.name} 1"

@test_util.skip_unless_galaxy("release_21.05")
def test_folder_contents_iter(self):
for i in range(12):
self.gi.folders.create_folder(self.folder["id"], f"{self.name} {i}")

# check defaults for limit and offset
f2 = list(self.gi.folders.contents_iter(self.folder["id"]))
assert len(f2) == 12
assert f2[0]["name"] == f"{self.name} 0"

# check non defaults
f2 = list(self.gi.folders.contents_iter(self.folder["id"], limit=1))
assert len(f2) == 12
assert f2[0]["name"] == f"{self.name} 0"

@test_util.skip_unless_galaxy("release_21.01")
def test_show_folder_contents_include_deleted(self):
history = self.gi.histories.create_history(name="Test History")
hda_id = self._test_dataset(history["id"])

# Create 2 library datasets into the library folder
ldda1 = self.gi.libraries.copy_from_dataset(
library_id=self.library["id"], dataset_id=hda_id, folder_id=self.folder["id"], message="Added HDA"
)
ldda2 = self.gi.libraries.copy_from_dataset(
library_id=self.library["id"], dataset_id=hda_id, folder_id=self.folder["id"], message="Added HDA"
)
folder_info = self.gi.folders.show_folder(self.folder["id"], contents=True)
assert len(folder_info["folder_contents"]) == 2
assert folder_info["folder_contents"][0]["type"] == "file"

# Delete the library datasets and check if include_deleted works
self.gi.libraries.delete_library_dataset(self.library["id"], ldda1["id"])
self.gi.libraries.delete_library_dataset(self.library["id"], ldda2["id"], purged=True)
folder_info = self.gi.folders.show_folder(self.folder["id"], contents=True, include_deleted=True)
# check if there are 2 contents and the number is correct
assert len(folder_info["folder_contents"]) == 2
assert folder_info["metadata"]["total_rows"] == 2

folder_info = self.gi.folders.show_folder(self.folder["id"], contents=True)
assert len(folder_info["folder_contents"]) == 0
assert folder_info["metadata"]["total_rows"] == 0
# show folders with contents=False does not respect include_deleted
folder_info = self.gi.folders.show_folder(self.folder["id"])
assert folder_info["item_count"] == 2

self.gi.histories.delete_history(history["id"])

def test_delete_folder(self):
self.sub_folder = self.gi.folders.create_folder(self.folder["id"], self.name)
self.gi.folders.delete_folder(self.sub_folder["id"])
Expand Down
3 changes: 1 addition & 2 deletions bioblend/_tests/TestGalaxyHistories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
"""
""" """

import os
import shutil
Expand Down
3 changes: 1 addition & 2 deletions bioblend/_tests/TestGalaxyTools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
"""
""" """

import os
from typing import (
Expand Down
3 changes: 1 addition & 2 deletions bioblend/_tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
""" General support infrastructure not tied to any particular test.
"""
"""General support infrastructure not tied to any particular test."""

import os
import random
Expand Down
7 changes: 7 additions & 0 deletions bioblend/galaxy/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def show_dataset(self, dataset_id: str, hda_ldda: HdaLdda = "hda") -> Dict[str,
}
return self._get(id=dataset_id, params=params)

# def delete(self, dataset_ids, hda_ldda: HdaLdda = "hda", purge: bool = False) -> Dict[str, Any]:
# datasets = []
# for dataset_id in dataset_ids:
# datasets.append({"src": hda_ldda, "id": dataset_id})
# payload = {"datasets": datasets, "purge": purge}
# return self._delete(payload=payload)

def _initiate_download(
self, dataset_id: str, stream_content: bool, require_ok_state: bool = True, maxwait: float = 12000
) -> Tuple[Dict[str, Any], str, Response]:
Expand Down
88 changes: 85 additions & 3 deletions bioblend/galaxy/folders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Contains possible interactions with the Galaxy library folders
"""

import sys
from typing import (
Any,
Dict,
Iterable,
List,
Literal,
Optional,
overload,
TYPE_CHECKING,
Union,
)
Expand Down Expand Up @@ -45,7 +48,31 @@ def create_folder(self, parent_folder_id: str, name: str, description: Optional[
payload["description"] = description
return self._post(payload=payload, id=parent_folder_id)

def show_folder(self, folder_id: str, contents: bool = False) -> Dict[str, Any]:
@overload
def show_folder(
self,
folder_id: str,
contents: Literal[False] = False,
) -> Dict[str, Any]: ...

@overload
def show_folder(
self,
folder_id: str,
contents: Literal[True],
limit: int = 10,
offset: int = 0,
include_deleted: bool = False,
) -> Dict[str, Any]: ...

def show_folder(
self,
folder_id: str,
contents: bool = False,
limit: int = 10,
offset: int = 0,
include_deleted: bool = False,
) -> Dict[str, Any]:
"""
Display information about a folder.

Expand All @@ -56,11 +83,66 @@ def show_folder(self, folder_id: str, contents: bool = False) -> Dict[str, Any]:
:param contents: True to get the contents of the folder, rather
than just the folder details.

:type limit: int
:param limit: When ``contents=True``, maximum number of items to return.

:type offset: int
:param contents: When ``contents=True``, number of items to skip. Return
contents starting with item offset+1.

:type include_deleted: bool
:param include_deleted: When ``contents=True``, whether to include
deleted items.

:rtype: dict
:return: dictionary including details of the folder
:return: dictionary including details of the folder.
For contents=False the dict contains infos on the folder.
For contents=True the dict contains the keys "metadata" (a dict with
infos on the folder) and "folder_contents" (a list of dicts with info
on the childs).

Notes: For iterating over folder contents there is also contents_iter.
"""
params = {
"limit": limit,
"offset": offset,
"include_deleted": include_deleted,
}
return self._get(id=folder_id, contents=contents, params=params)

def contents_iter(
self,
folder_id: str,
limit: int = 10,
include_deleted: bool = False,
) -> Iterable[Dict[str, Any]]:
"""
Iterate over folder contents.

:type folder_id: str
:param folder_id: the folder's encoded id, prefixed by 'F'

:type limit: int
:param limit: Batch size to be used internally.

return self._get(id=folder_id, contents=contents)
:type include_deleted: bool
:param include_deleted: Whether to include deleted items.

:rtype: dict
:return: A generator for the folder contents
"""
total_rows = sys.maxsize
params = {
"limit": limit,
"offset": 0,
"include_deleted": include_deleted,
}

while params["offset"] <= total_rows:
chunk = self._get(id=folder_id, contents=True, params=params)
total_rows = chunk["metadata"]["total_rows"]
yield from chunk["folder_contents"]
params["offset"] += limit

def delete_folder(self, folder_id: str, undelete: bool = False) -> Dict[str, Any]:
"""
Expand Down