Skip to content

Commit 8d962f8

Browse files
authored
Add filters as_major_minor, package_namespace_path (#82)
1 parent 773c080 commit 8d962f8

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed

cookieplone/cli.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def validate_extra_context(value: list[str] | None = None) -> list[str]:
3131
return value
3232

3333

34-
def parse_extra_content(value: list[str]) -> dict:
34+
def parse_extra_context(value: list[str]) -> dict:
3535
"""Parse extra content and return a dictionary with options."""
3636
if not value:
3737
return {}
@@ -151,17 +151,20 @@ def cli(
151151
if not output_dir:
152152
output_dir = Path().cwd()
153153

154+
# Annotate extra_context
155+
## We do this to always get the latest information about the repository
156+
## and template being used
157+
extra_context = parse_extra_context(extra_context)
158+
extra_context["__generator_sha"] = internal.repo_sha(repo_path)
159+
extra_context["__generator_signature"] = internal.signature_md(repo_path)
160+
extra_context["__cookieplone_repository_path"] = f"{repo_path}"
161+
extra_context["__cookieplone_template"] = f"{template}"
162+
154163
replay_file = files.resolve_path(replay_file) if replay_file else replay_file
155164
if replay_file and replay_file.exists():
156165
# Use replay_file
157166
replay = replay_file
158-
elif not replay:
159-
# Annotate extra_context
160-
extra_context = parse_extra_content(extra_context)
161-
extra_context["__generator_sha"] = internal.repo_sha(repo_path)
162-
extra_context["__generator_signature"] = internal.signature_md(repo_path)
163-
extra_context["__cookieplone_repository_path"] = f"{repo_path}"
164-
extra_context["__cookieplone_template"] = f"{template}"
167+
165168
# Run generator
166169
try:
167170
generate(

cookieplone/filters/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def package_namespaces(v) -> str:
3535
return ", ".join(f'"{item}"' for item in result)
3636

3737

38+
@simple_filter
39+
def package_namespace_path(v: str) -> str:
40+
"""Return path to the package namespace including the src directory."""
41+
top_namespace = v.split(".")[0]
42+
return f"src/{top_namespace}"
43+
44+
3845
@simple_filter
3946
def package_path(v) -> str:
4047
"""Return path to the package code within the src directory."""
@@ -139,3 +146,9 @@ def as_semver(v: str) -> str:
139146
def unscoped_package_name(v: str) -> str:
140147
"""Return the unscoped name for a given package."""
141148
return npm.unscoped_package_name(v)
149+
150+
151+
@simple_filter
152+
def as_major_minor(v: str) -> str:
153+
"""Return a version in the major.minor format."""
154+
return versions.format_as_major_minor(v)

cookieplone/utils/versions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ def python_version_for_plone(plone_version: str) -> str:
145145
"""Return the latest supported Python version for a given Plone version."""
146146
version_support = python_versions_for_plone(plone_version)
147147
return version_support.latest
148+
149+
150+
def format_as_major_minor(version: str) -> str:
151+
"""Format a list of versions as major.minor."""
152+
# Handle "versions" used in tests / constraints
153+
if "-" in version:
154+
version = version.split("-")[0]
155+
v = Version(version)
156+
return f"{v.major}.{v.minor}"

news/+as_major_minor.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `as_major_minor` filter. @ericof

news/+extra_context.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Parse extra-context information even if we are running with a replay file. @ericof
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `package_namespace_path` filter. @ericof

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
(["foo=1", "bar=2"], {"foo": "1", "bar": "2"}),
1313
],
1414
)
15-
def test_parse_extra_content(value: list[str], expected: dict):
16-
func = cli.parse_extra_content
15+
def test_parse_extra_context(value: list[str], expected: dict):
16+
func = cli.parse_extra_context
1717
assert func(value) == expected
1818

1919

tests/test_filters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ def func(filter_: str) -> Path:
7474
"{{'@plone/volto' | unscoped_package_name}}",
7575
"volto",
7676
],
77+
["as_major_minor", "{{'1.0.0a0' | as_major_minor}}", "1.0"],
78+
["as_major_minor", "{{'6.1.1rc1' | as_major_minor}}", "6.1"],
79+
["as_major_minor", "{{'1' | as_major_minor}}", "1.0"],
80+
["package_namespace_path", "{{'foo' | package_namespace_path}}", "src/foo"],
81+
["package_namespace_path", "{{'foo.bar' | package_namespace_path}}", "src/foo"],
82+
[
83+
"package_namespace_path",
84+
"{{'foo.bar.foobar' | package_namespace_path}}",
85+
"src/foo",
86+
],
87+
[
88+
"package_namespace_path",
89+
"{{'collective.addon' | package_namespace_path}}",
90+
"src/collective",
91+
],
92+
[
93+
"package_namespace_path",
94+
"{{'collective_addon' | package_namespace_path}}",
95+
"src/collective_addon",
96+
],
7797
],
7898
)
7999
def test_filters(generate_context_file, filter_: str, raw: str, expected: str):

tests/utils/test_versions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,25 @@ def test_convert_pep440_semver(pep440: str, expected: str):
275275
func = versions.convert_pep440_semver
276276
result = func(pep440)
277277
assert result == expected
278+
279+
280+
@pytest.mark.parametrize(
281+
"version,expected",
282+
[
283+
["1.0.0a0", "1.0"],
284+
["1.0.0b1", "1.0"],
285+
["1.0.0rc1", "1.0"],
286+
["1.0.0rc1.dev0", "1.0"],
287+
["1.0.0", "1.0"],
288+
["6.1.1.dev0", "6.1"],
289+
["6.1.1.post0", "6.1"],
290+
["6.1-dev", "6.1"],
291+
["6.1-latest", "6.1"],
292+
["202503.1", "202503.1"],
293+
["202512.1", "202512.1"],
294+
],
295+
)
296+
def test_format_as_major_minor(version: str, expected: str):
297+
func = versions.format_as_major_minor
298+
result = func(version)
299+
assert result == expected

0 commit comments

Comments
 (0)