Skip to content

Commit 80263e9

Browse files
author
Alistair Turnbull
committed
Replace file_path with root / path: available as input_path()
1 parent 94e27d5 commit 80263e9

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

nancy/__init__.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def process_path(self, obj: Path) -> None:
203203
if child[0] != "." or self.process_hidden:
204204
self.process_path(obj / child)
205205
elif (root / obj).is_file():
206-
Expand(self, obj, root / obj).process_file()
206+
Expand(self, root, obj).process_file()
207207
else:
208208
raise ValueError(f"'{obj}' is not a file or directory")
209209

@@ -224,13 +224,13 @@ class Expand:
224224
225225
Fields:
226226
trees (Trees):
227-
path (Path): the `inputs`-relative `Path`
228-
file_path (Path): the filesystem input `Path`
227+
root (Path): one of `trees.inputs`
228+
path (Path): the `root`-relative `Path`
229229
"""
230230

231231
trees: Trees
232+
root: Path
232233
path: Path
233-
file_path: Path
234234

235235
# The output file relative to `trees.output`.
236236
# `None` while the filename is being expanded.
@@ -243,12 +243,13 @@ class Expand:
243243
def __init__(
244244
self,
245245
trees: Trees,
246+
root: Path,
246247
path: Path,
247-
file_path: Path,
248248
):
249+
assert root in trees.inputs, (root, trees)
249250
self.trees = trees
251+
self.root = root
250252
self.path = path
251-
self.file_path = file_path
252253
self._output_path = None
253254
self._stack = []
254255
self._macros = Macros(self)
@@ -262,6 +263,10 @@ def __init__(
262263
output_path = os.fsdecode(self.expand(bytes(output_path)))
263264
self._output_path = Path(output_path)
264265

266+
def input_file(self):
267+
"""Returns the filesystem input `Path`."""
268+
return self.root / self.path
269+
265270
def output_file(self):
266271
"""Returns the (computed) filesystem output `Path`.
267272
@@ -402,23 +407,23 @@ def include(self, file_path):
402407

403408
def process_file(self) -> None:
404409
"""Expand, copy or ignore the file."""
405-
debug(f"Processing file '{self.file_path}'")
410+
debug(f"Processing file '{self.input_file()}'")
406411
os.makedirs(self.output_file().parent, exist_ok=True)
407-
if re.search(TEMPLATE_REGEX, self.file_path.name):
412+
if re.search(TEMPLATE_REGEX, self.input_file().name):
408413
debug(f"Expanding '{self.path}' to '{self.output_file()}'")
409-
output = self.include(self.file_path)
414+
output = self.include(self.input_file())
410415
if not re.search(NO_COPY_REGEX, str(self.output_file())):
411416
if self.trees.output == Path("-"):
412417
sys.stdout.buffer.write(output)
413418
else:
414419
with open(self.output_file(), "wb") as fh:
415420
fh.write(output)
416-
elif not re.search(NO_COPY_REGEX, self.file_path.name):
421+
elif not re.search(NO_COPY_REGEX, self.input_file().name):
417422
if self.trees.output == Path("-"):
418-
file_contents = self.file_path.read_bytes()
423+
file_contents = self.input_file().read_bytes()
419424
sys.stdout.buffer.write(file_contents)
420425
else:
421-
shutil.copyfile(self.file_path, self.output_file())
426+
shutil.copyfile(self.input_file(), self.output_file())
422427

423428

424429
class Macros:
@@ -444,7 +449,7 @@ def realpath(self, args: Optional[list[bytes]], input: Optional[bytes]) -> bytes
444449
raise ValueError("$realpath does not take arguments")
445450
if input is not None:
446451
raise ValueError("$realpath does not take an input")
447-
return bytes(self._expand.file_path)
452+
return bytes(self._expand.input_file())
448453

449454
def outputpath(self, args: Optional[list[bytes]], input: Optional[bytes]) -> bytes:
450455
if args is not None:

0 commit comments

Comments
 (0)