Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions caterva2/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def get_download_url(path, urlbase):
return f"{urlbase}/api/download/{path}"


def get_handle_url(path, urlbase):
# Get the root in path (first element in path)
# root = path.split("/")[0]
# return f"{urlbase}/roots/{path}?roots={root}"
# We don't want to show other datasets in the same root
return f"{urlbase}/roots/{path}"


def b2_unpack(filepath):
schunk = blosc2.open(filepath)
outfile = filepath.with_suffix("")
Expand Down
18 changes: 16 additions & 2 deletions caterva2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def _format_paths(urlbase, path=None):
return urlbase, path


def _looks_like_slice(s: str) -> bool:
"""Return True if `s` parses as an index/slice expression for `np.index_exp[...]`."""
if not isinstance(s, str) or not s.strip():
return False
try:
# restrict eval environment to only numpy (no builtins)
eval(f"np.index_exp[{s}]", {"np": np, "__builtins__": {}}, {})
return True
except Exception:
return False


class Root:
def __init__(self, client, name):
"""
Expand Down Expand Up @@ -998,11 +1010,13 @@ def get_slice(self, path, key=None, as_blosc2=True, field=None):
as_blosc2=as_blosc2,
timeout=self.timeout,
)
if isinstance(key, str): # A filter has been passed
if isinstance(key, str):
# The key can still be a slice expression in string format (like for CLI utils)
params = {"slice_": key} if _looks_like_slice(key) else {"filter": key}
return api_utils.fetch_data(
path,
urlbase,
{"filter": key},
params=params,
auth_cookie=self.cookie,
as_blosc2=as_blosc2,
timeout=self.timeout,
Expand Down
Loading