Skip to content

Commit 60e05f6

Browse files
committed
Don't parse mike versions while sorting
There are 2 main reasons for this: 1. Parsing the mike versions is less future proof, as if more fields are added to the JSON in the future, we'll remove them from the output, making the sorting more bug prone and more costly to maintain. 2. Avoiding the extra wrapping in `sort_mike_versions()` makes it easier to test, as otherwise we need to do the extra wrapping in tests, wrapping that is really not related to sorting so it adds noise to the tests. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 02de9fe commit 60e05f6

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/frequenz/repo/config/cli/version/mike/sort.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,35 @@
44
"""Sort `mike`'s `version.json` file with a custom order."""
55

66

7-
import dataclasses
87
import json
98
import sys
10-
from typing import TextIO
9+
from typing import Any, TextIO
1110

1211
from .... import github
13-
from ....mkdocs.mike import MikeVersionInfo, sort_mike_versions
12+
from ....mkdocs.mike import sort_mike_versions
1413

1514

16-
def _load_and_sort_versions_from(stream: TextIO) -> list[MikeVersionInfo]:
17-
"""Load the versions from the given stream.
15+
def _load_and_sort_versions_from(stream: TextIO) -> dict[str, dict[str, Any]]:
16+
"""Load the versions from the given stream and sort them.
1817
1918
Args:
2019
stream: The stream to read the versions from.
2120
2221
Returns:
23-
The loaded versions.
22+
The sorted loaded versions.
2423
"""
25-
versions = [MikeVersionInfo(**v) for v in json.load(stream)]
26-
return sort_mike_versions(versions)
24+
versions = {v["version"]: v for v in json.load(stream)}
25+
return {v: versions[v] for v in sort_mike_versions(list(versions.keys()))}
2726

2827

29-
def _dump_versions_to(versions: list[MikeVersionInfo], stream: TextIO) -> None:
28+
def _dump_versions_to(versions: dict[str, dict[str, Any]], stream: TextIO) -> None:
3029
"""Dump the versions to the given stream.
3130
3231
Args:
3332
versions: The versions to dump.
3433
stream: The stream to write the versions to.
3534
"""
36-
json.dump([dataclasses.asdict(v) for v in versions], stream, separators=(",", ":"))
35+
json.dump(list(versions.values()), stream, separators=(",", ":"))
3736

3837

3938
def main() -> None:

src/frequenz/repo/config/mkdocs/mike.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ def compare_mike_version(version1: str, version2: str) -> int:
210210
return -1 if version1 < version2 else 1
211211

212212

213-
def sort_mike_versions(
214-
versions: list[MikeVersionInfo], *, reverse: bool = True
215-
) -> list[MikeVersionInfo]:
213+
def sort_mike_versions(versions: list[str], *, reverse: bool = True) -> list[str]:
216214
"""Sort `mike`'s `version.json` file with a custom order.
217215
218216
The `version` keys are expected as follows:
@@ -246,9 +244,5 @@ def sort_mike_versions(
246244
Returns:
247245
The sorted list of versions.
248246
"""
249-
250-
def compare(ver1: MikeVersionInfo, ver2: MikeVersionInfo) -> int:
251-
return compare_mike_version(ver1.version, ver2.version)
252-
253-
versions.sort(key=functools.cmp_to_key(compare), reverse=reverse)
247+
versions.sort(key=functools.cmp_to_key(compare_mike_version), reverse=reverse)
254248
return versions

0 commit comments

Comments
 (0)