Skip to content

Commit

Permalink
Implement new filters to manipulate Python versions (Fixes #69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericof committed Feb 27, 2025
1 parent d2eaba9 commit 5683eb8
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 9 deletions.
21 changes: 21 additions & 0 deletions cookieplone/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ def latest_plone(use_prerelease_versions: str) -> str:
return versions.latest_plone(allow_prerelease=allow_prerelease)


@simple_filter
def python_versions(plone_version: str) -> list[str]:
"""Return a list of supported Python versions for a Plone version."""
versions_info = versions.python_versions_for_plone(plone_version)
return versions_info.supported


@simple_filter
def python_version_oldest(plone_version: str) -> str:
"""Return a list of supported Python versions for a Plone version."""
versions_info = versions.python_versions_for_plone(plone_version)
return versions_info.oldest


@simple_filter
def python_version_latest(plone_version: str) -> str:
"""Return a list of supported Python versions for a Plone version."""
versions_info = versions.python_versions_for_plone(plone_version)
return versions_info.latest


@simple_filter
def node_version_for_volto(volto_version: str) -> int:
"""Return the Node Version to be used."""
Expand Down
38 changes: 36 additions & 2 deletions cookieplone/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# SPDX-FileCopyrightText: 2024-present Plone Foundation <[email protected]>
#
# SPDX-License-Identifier: MIT
from dataclasses import dataclass


@dataclass
class PythonVersionSupport:
"""Python version support for Plone."""

supported: list[str]
oldest: str
latest: str


PLONE_MIN_VERSION = "6"

SUPPORTED_PYTHON_VERSIONS = [
Expand All @@ -10,6 +22,28 @@
"3.13",
]

PLONE_PYTHON = {
"6.0": PythonVersionSupport(
[
"3.10",
"3.11",
"3.12",
],
"3.10",
"3.12",
),
"6.1": PythonVersionSupport(
[
"3.10",
"3.11",
"3.12",
"3.13",
],
"3.10",
"3.13",
),
}

DEFAULT_NODE = 22
SUPPORTED_NODE_VERSIONS = [
"20",
Expand All @@ -23,12 +57,12 @@
}
MIN_DOCKER_VERSION = "20.10"

## DEFAULT
# DEFAULT
COOKIEPLONE_REPO = "https://github.com/plone/cookieplone"
TEMPLATES_REPO = "https://github.com/plone/cookieplone-templates"
REPO_DEFAULT = "gh:plone/cookieplone-templates"

## Config
# Config
QUIET_MODE_VAR = "COOKIEPLONE_QUIET_MODE_SWITCH"
REPO_LOCATION = "COOKIEPLONE_REPOSITORY"
REPO_TAG = "COOKIEPLONE_REPOSITORY_TAG"
Expand Down
24 changes: 20 additions & 4 deletions cookieplone/utils/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
def get_npm_package_versions(package: str) -> list[str]:
"""Get versions for a NPM package."""
url: str = f"https://registry.npmjs.org/{package}"
resp = requests.get(url, headers={"Accept": "application/vnd.npm.install-v1+json"}) # noQA: S113
resp = requests.get( # noQA: S113
url, headers={"Accept": "application/vnd.npm.install-v1+json"}
)
data = resp.json()
return list(data["dist-tags"].values())

Expand Down Expand Up @@ -40,7 +42,7 @@ def is_valid_version(
return status


def latest_version(
def version_latest(
versions: list[str],
min_version: str | None = None,
max_version: str | None = None,
Expand All @@ -66,7 +68,7 @@ def latest_volto(
) -> str | None:
"""Return the latest volto version."""
versions = get_npm_package_versions("@plone/volto")
return latest_version(
return version_latest(
versions,
min_version=min_version,
max_version=max_version,
Expand All @@ -81,7 +83,7 @@ def latest_plone(
) -> str | None:
"""Return the latest Plone version."""
versions = get_pypi_package_versions("Plone")
return latest_version(
return version_latest(
versions,
min_version=min_version,
max_version=max_version,
Expand All @@ -93,3 +95,17 @@ def node_version_for_volto(volto_version: str) -> int:
"""Return the Node Version to be used with Volto."""
major = Version(volto_version).major
return settings.VOLTO_NODE.get(major, settings.DEFAULT_NODE)


def python_versions_for_plone(plone_version: str) -> settings.PythonVersionSupport:
"""Return the Python version information for a given Plone version."""
major = Version(plone_version).major
minor = Version(plone_version).minor
version = f"{major}.{minor}"
return settings.PLONE_PYTHON.get(version)


def python_version_for_plone(plone_version: str) -> str:
"""Return the Node Version to be used with Volto."""
version_support = python_versions_for_plone(plone_version)
return version_support.latest
1 change: 1 addition & 0 deletions news/69.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement new filters to manipulate Python versions [@ericof]
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def tmp_repo(tmp_path):

@pytest.fixture()
def no_repo(tmp_path):
sub_path = "".join(random.choice(string.ascii_lowercase) for _ in range(20)) # noQA:S311
letters = string.ascii_lowercase
sub_path = "".join(random.choice(letters) for _ in range(20)) # noQA: S311
path = tmp_path / sub_path
path.mkdir(parents=True)
return path
Expand Down
14 changes: 14 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ def func(filter_: str) -> Path:
["locales_language_code", "{{'es-mx' | locales_language_code}}", "es_MX"],
["image_prefix", "{{'github' | image_prefix}}", "ghcr.io/"],
["image_prefix", "{{'bitbucket' | image_prefix}}", ""],
[
"python_versions",
"{{'6.0' | python_versions}}",
"['3.10', '3.11', '3.12']",
],
[
"python_versions",
"{{'6.1' | python_versions}}",
"['3.10', '3.11', '3.12', '3.13']",
],
["python_version_oldest", "{{'6.0' | python_version_oldest}}", "3.10"],
["python_version_oldest", "{{'6.1' | python_version_oldest}}", "3.10"],
["python_version_latest", "{{'6.0' | python_version_latest}}", "3.12"],
["python_version_latest", "{{'6.1' | python_version_latest}}", "3.13"],
],
)
def test_filters(generate_context_file, filter_: str, raw: str, expected: str):
Expand Down
39 changes: 37 additions & 2 deletions tests/utils/test_versions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from cookieplone.settings import PythonVersionSupport
from cookieplone.utils import versions


Expand Down Expand Up @@ -168,14 +169,14 @@ def test_get_pypi_package_versions():
["17", "17.99", True, "17.15.5"],
],
)
def test_latest_version(
def test_version_latest(
volto_versions,
min_version: str,
max_version: str,
allow_prerelease: bool,
expected: str,
):
func = versions.latest_version
func = versions.version_latest
assert func(volto_versions, min_version, max_version, allow_prerelease) == expected


Expand Down Expand Up @@ -220,3 +221,37 @@ def test_latest_plone(
):
func = versions.latest_plone
assert func(min_version, max_version, allow_prerelease) == expected


@pytest.mark.parametrize(
"plone_version,supported,oldest,latest",
[
["6.0", ["3.10", "3.11", "3.12"], "3.10", "3.12"],
["6.0.14", ["3.10", "3.11", "3.12"], "3.10", "3.12"],
["6.1.0", ["3.10", "3.11", "3.12", "3.13"], "3.10", "3.13"],
],
)
def test_python_versions_for_plone(
plone_version: str, supported: list[str], oldest: str, latest: str
):
func = versions.python_versions_for_plone
result = func(plone_version)
assert isinstance(result, PythonVersionSupport)
assert result.supported == supported
assert result.oldest == oldest
assert result.latest == latest


@pytest.mark.parametrize(
"plone_version,expected",
[
["6.0", "3.12"],
["6.0.14", "3.12"],
["6.1.0", "3.13"],
],
)
def test_python_version_for_plone(plone_version: str, expected: str):
func = versions.python_version_for_plone
result = func(plone_version)
assert isinstance(result, str)
assert result == expected

0 comments on commit 5683eb8

Please sign in to comment.