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

GH-87: Improve DirPathCalc and its relatives #88

Merged
merged 3 commits into from
Feb 3, 2024
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: 20 additions & 15 deletions src/anchovy/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pathlib import Path

from .core import Context, ContextDir, Matcher, PathCalc
from .custody import CONTEXT_DIR_KEYS


T = t.TypeVar('T')
Expand Down Expand Up @@ -51,16 +52,23 @@
extension information for the input paths, allowing for meaningful work
with extensions that `pathlib.Path` does not reflect, like `.tar.gz`.
"""
def __init__(self, dest: Path, ext: str | None = None, transform: t.Callable[[Path], Path] | None = None):
def __init__(self,
dest: Path | ContextDir,
ext: str | None = None,
transform: t.Callable[[Path], Path] | None = None):
self.dest = dest
self.ext = ext
self.transform = transform

def __call__(self, context: Context, path: Path, match: T) -> Path:
return _to_dir_inner(self.dest, self.ext, context, path, match, self.transform)
if self.dest in CONTEXT_DIR_KEYS:
dest = context[self.dest]
else:
dest = Path(self.dest)

Check warning on line 67 in src/anchovy/paths.py

View check run for this annotation

Codecov / codecov/patch

src/anchovy/paths.py#L67

Added line #L67 was not covered by tests
return _to_dir_inner(dest, self.ext, context, path, match, self.transform)


class OutputDirPathCalc(PathCalc[T]):
class OutputDirPathCalc(DirPathCalc[T]):
"""
PathCalc which makes its input paths children of the Context's output
directory. If @ext is specified, it will replace the extension of input
Expand All @@ -69,15 +77,13 @@
meaningful work with extensions that `pathlib.Path` does not reflect, like
`.tar.gz`.
"""
def __init__(self, ext: str | None = None, transform: t.Callable[[Path], Path] | None = None):
self.ext = ext
self.transform = transform

def __call__(self, context: Context, path: Path, match: T) -> Path:
return _to_dir_inner(context['output_dir'], self.ext, context, path, match, self.transform)
def __init__(self,
ext: str | None = None,
transform: t.Callable[[Path], Path] | None = None):
super().__init__('output_dir', ext, transform)


class WorkingDirPathCalc(PathCalc[T]):
class WorkingDirPathCalc(DirPathCalc[T]):
"""
PathCalc which makes its input paths children of the Context's working
directory. If @ext is specified, it will replace the extension of input
Expand All @@ -86,12 +92,11 @@
meaningful work with extensions that `pathlib.Path` does not reflect, like
`.tar.gz`.
"""
def __init__(self, ext: str | None = None, transform: t.Callable[[Path], Path] | None = None):
self.ext = ext
self.transform = transform
def __init__(self,
ext: str | None = None,
transform: t.Callable[[Path], Path] | None = None):
super().__init__('working_dir', ext, transform)

def __call__(self, context: Context, path: Path, match: T) -> Path:
return _to_dir_inner(context['working_dir'], self.ext, context, path, match, self.transform)


class REMatcher(Matcher[re.Match | None]):
Expand Down
16 changes: 8 additions & 8 deletions test/artifacts/basic_site.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
"path",
{
"sha1": "1658ebaece18a76250c123fdeac8e6ad38b3d795",
"m_time": 1703199125.7663765,
"m_time": 1706995610.3326533,
"size": 506
}
],
"input_dir/index.md": [
"path",
{
"sha1": "2d329383d6bafbbd203c0fd3d90cfcfb675db587",
"m_time": 1703197638.1800685,
"m_time": 1703960959.0897603,
"size": 267
}
],
Expand All @@ -78,7 +78,7 @@
"path",
{
"sha1": "013e15a825ed0690e6cd9ad479f4185566d020f8",
"m_time": 1703199125.7738748,
"m_time": 1706995610.3371997,
"size": 276
}
],
Expand All @@ -94,7 +94,7 @@
"path",
{
"sha1": "203990dbc21656099ebf8e06339897ff59b1e569",
"m_time": 1703199125.7778723,
"m_time": 1706995610.3462749,
"size": 890709
}
],
Expand All @@ -110,7 +110,7 @@
"path",
{
"sha1": "9548f6b06d5a74ecc70a02ec1d6ee024396dc912",
"m_time": 1703199125.7903874,
"m_time": 1706995610.3808444,
"size": 706821
}
],
Expand All @@ -126,7 +126,7 @@
"path",
{
"sha1": "3b94e6a05cfb68faf1d4e15f82760ec306990295",
"m_time": 1703199125.8130672,
"m_time": 1706995610.4031894,
"size": 679302
}
],
Expand All @@ -142,7 +142,7 @@
"path",
{
"sha1": "d2b3364f1f5be5153295518a5b1ee61bd52cffe3",
"m_time": 1703199125.821573,
"m_time": 1706995610.4122894,
"size": 482152
}
],
Expand All @@ -158,7 +158,7 @@
"path",
{
"sha1": "23e6ad2efc9cb2ec65a010b43032f7569afab574",
"m_time": 1703199125.83395,
"m_time": 1706995610.4249547,
"size": 447542
}
],
Expand Down
38 changes: 19 additions & 19 deletions test/artifacts/code_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,23 @@
"path",
{
"sha1": "",
"m_time": 1703199123.4892223,
"m_time": 1706995623.6526809,
"size": 4096
}
],
"working_dir/basic_site.py": [
"path",
{
"sha1": "c4983436e017a238d94796250a749d74cbc76835",
"m_time": 1703199123.4882276,
"m_time": 1706995623.6526809,
"size": 1071
}
],
"working_dir/gallery.py": [
"path",
{
"sha1": "3cdd5e3cc41664211b63f98b94a0791282f1ea4d",
"m_time": 1703199123.4892223,
"m_time": 1706995623.6526809,
"size": 1407
}
],
Expand All @@ -160,7 +160,7 @@
"path",
{
"sha1": "c361ccf60628e76f571f8229d9c896603f7042ab",
"m_time": 1703199123.4967394,
"m_time": 1706995623.6602678,
"size": 298
}
],
Expand All @@ -176,7 +176,7 @@
"path",
{
"sha1": "23b855543fd50b49b743413cc221e1bee266c6c9",
"m_time": 1703199123.5022552,
"m_time": 1706995623.6736958,
"size": 306
}
],
Expand All @@ -192,7 +192,7 @@
"path",
{
"sha1": "16d39823b42dfc58eb0b12d04cc54c233575f398",
"m_time": 1703199123.5092297,
"m_time": 1706995623.681307,
"size": 5551
}
],
Expand All @@ -208,7 +208,7 @@
"path",
{
"sha1": "0cc6ab8a6e988e4f3ff88a30dca17f4b8087a37b",
"m_time": 1703199123.5142365,
"m_time": 1706995623.6852465,
"size": 36
}
],
Expand All @@ -224,23 +224,23 @@
"path",
{
"sha1": "bf995c9fb6202933eb3d6b433b38bae484e2f7d1",
"m_time": 1703199123.5333054,
"m_time": 1706995623.7074897,
"size": 1184
}
],
"working_dir/gallery.md": [
"path",
{
"sha1": "e03d87bee6bd515e50c752acae7a24cc6700aef7",
"m_time": 1703199123.536311,
"m_time": 1706995623.7105067,
"size": 1514
}
],
"working_dir/index.html": [
"path",
{
"sha1": "e653663027dfc58550559eaa9d2ba816dd94b8e0",
"m_time": 1703199123.545324,
"m_time": 1706995623.7190287,
"size": 427
}
],
Expand All @@ -257,15 +257,15 @@
"path",
{
"sha1": "5620127ab0d2261762dac0c30d1fe79e86dcac03",
"m_time": 1703199123.550546,
"m_time": 1706995623.7240794,
"size": 5859
}
],
"working_dir/basic_site.html": [
"path",
{
"sha1": "ffe41a786d2d6e21e277aa1db53db621384aa76f",
"m_time": 1703199123.6589215,
"m_time": 1706995623.8251395,
"size": 4852
}
],
Expand All @@ -281,39 +281,39 @@
"path",
{
"sha1": "607b78c4b55e9b7598d74c7dae26847536932758",
"m_time": 1703199123.668445,
"m_time": 1706995623.8331532,
"size": 6386
}
],
"output_dir/static/css_pack.css": [
"path",
{
"sha1": "a8253d222c06b0cf4391d9b95471bc67d4033663",
"m_time": 1703199123.6757824,
"size": 2794
"sha1": "c363895ad4c4f75aff899690c56eef2e300980be",
"m_time": 1706995623.8389888,
"size": 2800
}
],
"output_dir/index.html": [
"path",
{
"sha1": "239ba6c1e7802f77005bcef7ce3eae4ef59df4ae",
"m_time": 1703199123.722618,
"m_time": 1706995623.8761384,
"size": 263
}
],
"output_dir/basic_site.html": [
"path",
{
"sha1": "f217f536a707d91b90b6d50ab94fb5e9c310a5f2",
"m_time": 1703199123.7368248,
"m_time": 1706995623.9063706,
"size": 4439
}
],
"output_dir/gallery.html": [
"path",
{
"sha1": "ddac506b836ee7fffaed7f1dad18967bd11acf0e",
"m_time": 1703199123.7426379,
"m_time": 1706995623.9233155,
"size": 5887
}
]
Expand Down
Loading
Loading