From d480d22b8ac39601838218d03235771fc75ddd8a Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Thu, 1 Oct 2026 17:27:27 -0400 Subject: [PATCH] feat(files): let files.paths take a list of paths and globs files.find already did; files.paths raised a TypeError on a list. Both now share the target and glob helpers. --- priv/python/albedo_plugins/files.py | 64 ++++++++++++++++++----------- robot-docs/files.md | 2 +- test/e2e/python_test.py | 12 ++++++ 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/priv/python/albedo_plugins/files.py b/priv/python/albedo_plugins/files.py index 7415c191c..2704e4421 100644 --- a/priv/python/albedo_plugins/files.py +++ b/priv/python/albedo_plugins/files.py @@ -275,10 +275,7 @@ class Files: hidden: bool, ) -> Rows: albedo_trace.note("search", pattern) - targets = [ - Path(item).expanduser() - for item in ([path] if isinstance(path, str) else path) - ] + targets = _targets(path) if not _which("rg"): return _fallback_find( pattern, @@ -301,7 +298,7 @@ class Files: flags.append("-i") if hidden: flags.append("--hidden") - for value in [glob] if isinstance(glob, str) else list(glob or []): + for value in _globs(glob): flags += ["-g", value] command = " ".join( shlex.quote(part) @@ -339,15 +336,15 @@ class Files: def paths( self, pattern: str | None = None, - path: str = ".", + path: str | Sequence[str] = ".", *, - glob: str | None = None, + glob: str | Sequence[str] | None = None, max_results: int = 100, hidden: bool = False, ) -> Search[Rows]: """File names, not contents; the same ripgrep-or-Python split. A pattern with *, ? or [ is a glob over names; other text matches anywhere in the path. - Await it: `await files.paths(pattern)`.""" + `path` may be one path or a list. Await it: `await files.paths(pattern)`.""" return Search( "files.paths", lambda: self._paths(pattern, path, glob, max_results, hidden) ) @@ -355,20 +352,23 @@ class Files: async def _paths( self, pattern: str | None, - path: str, - glob: str | None, + path: str | Sequence[str], + glob: str | Sequence[str] | None, max_results: int, hidden: bool, ) -> Rows: - target = Path(path).expanduser() + targets = _targets(path) + globs = _globs(glob) if not _which("rg"): - return _fallback_paths(pattern, target, glob, max_results, hidden) + return _fallback_paths(pattern, targets, globs, max_results, hidden) flags = ["--files"] if hidden: flags.append("--hidden") - if glob: - flags += ["-g", glob] - command = " ".join(shlex.quote(part) for part in ["rg", *flags, str(target)]) + for value in globs: + flags += ["-g", value] + command = " ".join( + shlex.quote(part) for part in ["rg", *flags, *map(str, targets)] + ) _, output = await _run(command) matches = _name_matcher(pattern) found = [line for line in output.splitlines() if line and matches(line)] @@ -433,14 +433,11 @@ def _fallback_find( ) -> Rows: flags = 0 if case_sensitive else re.IGNORECASE matcher = re.compile(re.escape(pattern) if literal else pattern, flags) - globs = [glob] if isinstance(glob, str) else list(glob or []) + globs = _globs(glob) results, matched = [], 0 for target in targets: for file in _walk(target, hidden): - if globs and not any( - fnmatch.fnmatch(str(file), value) or fnmatch.fnmatch(file.name, value) - for value in globs - ): + if not _globbed(file, globs): continue try: lines = file.read_text(encoding="utf-8", errors="replace").splitlines() @@ -471,6 +468,24 @@ def _fallback_find( return Rows(results) +def _targets(path: str | Sequence[str]) -> list[Path]: + return [ + Path(item).expanduser() for item in ([path] if isinstance(path, str) else path) + ] + + +def _globs(glob: str | Sequence[str] | None) -> list[str]: + return [glob] if isinstance(glob, str) else list(glob or []) + + +def _globbed(file: Path, globs: list[str]) -> bool: + """True when there is no glob, or one matches the path or the file name.""" + return not globs or any( + fnmatch.fnmatch(str(file), value) or fnmatch.fnmatch(file.name, value) + for value in globs + ) + + def _name_matcher(pattern: str | None) -> Callable[[str], bool]: """A pattern with *, ? or [ is a glob over the file name (or the whole path when it has a /); anything else matches as case-insensitive text anywhere @@ -485,12 +500,15 @@ def _name_matcher(pattern: str | None) -> Callable[[str], bool]: return lambda path: folded in path.lower() -def _fallback_paths(pattern, path: Path, glob, max_results, hidden) -> Rows: +def _fallback_paths( + pattern, targets: list[Path], globs: list[str], max_results, hidden +) -> Rows: matches = _name_matcher(pattern) found = [ str(file) - for file in _walk(path, hidden) - if (glob is None or fnmatch.fnmatch(file.name, glob)) and matches(str(file)) + for target in targets + for file in _walk(target, hidden) + if _globbed(file, globs) and matches(str(file)) ] return Rows(found[:max_results], truncated=len(found) > max_results) diff --git a/robot-docs/files.md b/robot-docs/files.md index 9a45577bc..ae7dc7443 100644 --- a/robot-docs/files.md +++ b/robot-docs/files.md @@ -29,7 +29,7 @@ files.write("notes/plan.md", "...") ## search -`await find(pattern, path=".", glob=..., context=0, max_results=50, literal=False, case_sensitive=None, hidden=False)` searches contents and returns rows that print as `path:line: text`. `path` may be a list of paths. `context=N` adds up to N lines around each match, printed grep-style as `path-line- text` and marked `context=True`; `max_results` counts matches, not context lines. Slicing or indexing before the await applies to the rows, so `await find(...)[:10]` reads as intended. `await paths(pattern=None, path=".", glob=...)` searches file names. Both bound their results and say when the list was cut. +`await find(pattern, path=".", glob=..., context=0, max_results=50, literal=False, case_sensitive=None, hidden=False)` searches contents and returns rows that print as `path:line: text`. `path` may be a list of paths. `context=N` adds up to N lines around each match, printed grep-style as `path-line- text` and marked `context=True`; `max_results` counts matches, not context lines. Slicing or indexing before the await applies to the rows, so `await find(...)[:10]` reads as intended. `await paths(pattern=None, path=".", glob=...)` searches file names; like `find`, `path` and `glob` may each be a list. Both bound their results and say when the list was cut. ## await diff --git a/test/e2e/python_test.py b/test/e2e/python_test.py index b6d684037..1ed1e1f1b 100644 --- a/test/e2e/python_test.py +++ b/test/e2e/python_test.py @@ -60,6 +60,18 @@ class PythonToolsTests(unittest.TestCase): self.assertIn("before", output) self.assertIn("after", output) + def test_paths_searches_a_list_of_directories_with_a_list_of_globs(self): + for directory, name in [("a", "one.md"), ("b", "two.txt"), ("c", "three.md")]: + (self.app.workspace / directory).mkdir() + (self.app.workspace / directory / name).write_text("x") + output = self.execute( + "rows = await files.paths(None, ['a', 'b'], glob=['*.md', '*.txt'])\n" + "print(sorted(str(row) for row in rows))" + ) + self.assertIn("one.md", output) + self.assertIn("two.txt", output) + self.assertNotIn("three.md", output) + def test_a_cell_reports_how_long_it_ran(self): session = self.app.session() slept = self.run_cell("import asyncio\nawait asyncio.sleep(0.3)", session) -- knotmirror 0.0.1