Skip to content

Commit 94e27d5

Browse files
author
Alistair Turnbull
committed
Factor out find_root()
1 parent f1112c1 commit 94e27d5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

nancy/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,18 @@ def __init__(
159159
raise ValueError("build path must be relative")
160160
self.build_path = build_path
161161

162-
def find_object(self, obj: Path) -> Optional[Path]:
163-
"""Find the leftmost input tree containing `obj`.
162+
def find_root(self, obj: Path) -> Optional[Path]:
163+
"""Find the leftmost of `inputs` that contains `obj`."""
164+
for root in self.inputs:
165+
if (root / obj).exists():
166+
return root
167+
return None
164168

165-
Returns:
166-
Optional[Path]: the filesystem path of `obj`
167-
"""
169+
def find_object(self, obj: Path) -> Optional[Path]:
170+
"""Returns `find_root(obj) / obj` or `None`."""
168171
debug(f"find_object {obj} {self.inputs}")
169-
for o in (root / obj for root in self.inputs):
170-
debug(f"considering {o}")
171-
if o.exists():
172-
return o
173-
return None
172+
root = self.find_root(obj)
173+
return None if root is None else root / obj
174174

175175
def scandir(self, obj: Path) -> list[str]:
176176
"""Returns the child names of overlaid input directory `obj`."""
@@ -189,10 +189,10 @@ def process_path(self, obj: Path) -> None:
189189
Args:
190190
obj (Path): the `inputs`-relative `Path` to scan.
191191
"""
192-
found = self.find_object(obj)
193-
if found is None:
192+
root = self.find_root(obj)
193+
if root is None:
194194
raise ValueError(f"'{obj}' matches no path in the inputs")
195-
if found.is_dir():
195+
if (root / obj).is_dir():
196196
if self.output == Path("-"):
197197
raise ValueError("cannot output multiple files to stdout ('-')")
198198
debug(f"Entering directory '{obj}'")
@@ -202,8 +202,8 @@ def process_path(self, obj: Path) -> None:
202202
for child in self.scandir(obj):
203203
if child[0] != "." or self.process_hidden:
204204
self.process_path(obj / child)
205-
elif found.is_file():
206-
Expand(self, obj, found).process_file()
205+
elif (root / obj).is_file():
206+
Expand(self, obj, root / obj).process_file()
207207
else:
208208
raise ValueError(f"'{obj}' is not a file or directory")
209209

0 commit comments

Comments
 (0)