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

Expose custom 'walkers' to CLI. #551

Merged
merged 6 commits into from
Aug 25, 2023
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
35 changes: 34 additions & 1 deletion tiled/_tests/test_directory_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
import tifffile

from ..catalog import in_memory
from ..catalog.register import identity, register, strip_suffixes
from ..catalog.register import (
identity,
register,
skip_all,
strip_suffixes,
tiff_sequence,
)
from ..client import Context, from_context
from ..examples.generate_files import data, df1, generate_files
from ..server.app import build_app
Expand Down Expand Up @@ -112,3 +118,30 @@ def detect_mimetype(path, mimetype):
)
client = from_context(context)
assert set(client) == {"a0", "a.0.asfwoeijviojefeiofw", "c.csv"}


@pytest.mark.asyncio
async def test_skip_all_in_combination(tmpdir):
"Using skip_all should override defaults, but not hinder other walkers"
df1.to_csv(Path(tmpdir, "a.csv"))
Path(tmpdir, "one").mkdir()
df1.to_csv(Path(tmpdir, "one", "a.csv"))

for i in range(2):
tifffile.imwrite(Path(tmpdir, "one", f"image{i:05}.tif"), data)

tree = in_memory()
# By default, both file and tiff sequence are registered.
with Context.from_app(build_app(tree)) as context:
await register(tree, tmpdir)
client = from_context(context)
assert "a" in client
assert "a" in client["one"]
assert "image" in client["one"]

# With skip_all, directories and tiff sequence are registered, but individual files are not
with Context.from_app(build_app(tree)) as context:
await register(tree, tmpdir, walkers=[tiff_sequence, skip_all])
client = from_context(context)
assert list(client) == ["one"]
assert "image" in client["one"]
25 changes: 22 additions & 3 deletions tiled/catalog/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ async def register(
filter=filter,
)
path = Path(path)
if walkers is None:
walkers = DEFAULT_WALKERS
parsed_walkers = []
for walker in walkers or []:
parsed_walkers.append(import_object(walker))
parsed_walkers.extend(DEFAULT_WALKERS)
prefix_parts = [segment for segment in prefix.split("/") if segment]
for segment in prefix_parts:
child_catalog = await catalog.lookup_adapter([segment])
Expand All @@ -176,7 +178,7 @@ async def register(
await _walk(
catalog,
Path(path),
walkers,
parsed_walkers,
settings=settings,
)
else:
Expand Down Expand Up @@ -385,6 +387,23 @@ async def tiff_sequence(
return unhandled_files, unhandled_directories


async def skip_all(
catalog,
path,
files,
directories,
settings,
):
"""
Skip all files and directories without processing them.

This can be used to override the DEFAULT_WALKERS.
"""
for item in files:
logger.info(" SKIP ALL: Nothing yet handled file '%s'", item)
return [], directories


DEFAULT_WALKERS = [tiff_sequence, one_node_per_item]


Expand Down
12 changes: 12 additions & 0 deletions tiled/commandline/_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ def register(
"Specify here as 'mimetype=package.module:function'"
),
),
walkers: List[str] = typer.Option(
None,
"--walker",
help=(
"ADVANCED: Custom Tiled Walker for traversing directories and "
"grouping files. This is used in conjunction with Adapters that operate "
"on groups of files. "
"Specify here as 'package.module:function'"
),
),
):
from ..catalog.utils import SCHEME_PATTERN

Expand Down Expand Up @@ -190,6 +200,7 @@ def register(
mimetype_detection_hook=mimetype_detection_hook,
mimetypes_by_file_ext=mimetypes_by_file_ext,
adapters_by_mimetype=adapters_by_mimetype,
walkers=walkers,
key_from_filename=key_from_filename,
)
)
Expand All @@ -202,6 +213,7 @@ def register(
mimetype_detection_hook=mimetype_detection_hook,
mimetypes_by_file_ext=mimetypes_by_file_ext,
adapters_by_mimetype=adapters_by_mimetype,
walkers=walkers,
key_from_filename=key_from_filename,
)
)
Expand Down
12 changes: 12 additions & 0 deletions tiled/commandline/_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def serve_directory(
"Specify here as 'mimetype=package.module:function'"
),
),
walkers: List[str] = typer.Option(
None,
"--walker",
help=(
"ADVANCED: Custom Tiled Walker for traversing directories and "
"grouping files. This is used in conjunction with Adapters that operate "
"on groups of files. "
"Specify here as 'package.module:function'"
),
),
host: str = typer.Option(
"127.0.0.1",
help=(
Expand Down Expand Up @@ -189,6 +199,7 @@ async def walk_and_serve():
mimetype_detection_hook=mimetype_detection_hook,
mimetypes_by_file_ext=mimetypes_by_file_ext,
adapters_by_mimetype=adapters_by_mimetype,
walkers=walkers,
key_from_filename=key_from_filename,
)
)
Expand All @@ -211,6 +222,7 @@ async def walk_and_serve():
mimetype_detection_hook=mimetype_detection_hook,
mimetypes_by_file_ext=mimetypes_by_file_ext,
adapters_by_mimetype=adapters_by_mimetype,
walkers=walkers,
key_from_filename=key_from_filename,
)
)
Expand Down