|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Sort `mike`'s `version.json` file with a custom order.""" |
| 5 | + |
| 6 | + |
| 7 | +import dataclasses |
| 8 | +import json |
| 9 | +import sys |
| 10 | +from typing import TextIO |
| 11 | + |
| 12 | +from ....mkdocs.mike import MikeVersionInfo, sort_mike_versions |
| 13 | + |
| 14 | + |
| 15 | +def _sort(stream_in: TextIO, stream_out: TextIO) -> None: |
| 16 | + """Sort the versions in the given in stream to the given out stream. |
| 17 | +
|
| 18 | + Args: |
| 19 | + stream_in: The stream to read the versions from. |
| 20 | + stream_out: The stream to write the sorted versions to. |
| 21 | + """ |
| 22 | + versions = json.load(stream_in) |
| 23 | + sorted_versions = sort_mike_versions([MikeVersionInfo(**v) for v in versions]) |
| 24 | + json.dump(sorted_versions, stream_out, separators=(",", ":")) |
| 25 | + |
| 26 | + |
| 27 | +def _load_and_sort_versions_from(stream: TextIO) -> list[MikeVersionInfo]: |
| 28 | + """Load the versions from the given stream. |
| 29 | +
|
| 30 | + Args: |
| 31 | + stream: The stream to read the versions from. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + The loaded versions. |
| 35 | + """ |
| 36 | + versions = [MikeVersionInfo(**v) for v in json.load(stream)] |
| 37 | + return sort_mike_versions(versions) |
| 38 | + |
| 39 | + |
| 40 | +def _dump_versions_to(versions: list[MikeVersionInfo], stream: TextIO) -> None: |
| 41 | + """Dump the versions to the given stream. |
| 42 | +
|
| 43 | + Args: |
| 44 | + versions: The versions to dump. |
| 45 | + stream: The stream to write the versions to. |
| 46 | + """ |
| 47 | + json.dump([dataclasses.asdict(v) for v in versions], stream, separators=(",", ":")) |
| 48 | + |
| 49 | + |
| 50 | +def main() -> None: |
| 51 | + """Sort `mike`'s `version.json` file with a custom order. |
| 52 | +
|
| 53 | + The versions are sorted using `sort_versions()`. |
| 54 | +
|
| 55 | + If no arguments are given, then the contents are read from stdin and the sorted |
| 56 | + versions are printed to stdout. |
| 57 | +
|
| 58 | + If one argument is given, then the contents of the file are replaced with the sorted |
| 59 | + versions. |
| 60 | +
|
| 61 | + If more than one argument is given, then an error is printed to stderr and the |
| 62 | + program exits with a non-zero exit code. |
| 63 | + """ |
| 64 | + match len(sys.argv): |
| 65 | + case 1: |
| 66 | + _dump_versions_to(_load_and_sort_versions_from(sys.stdin), sys.stdout) |
| 67 | + case 2: |
| 68 | + with open(sys.argv[1], "r", encoding="utf8") as stream_in: |
| 69 | + versions = _load_and_sort_versions_from(stream_in) |
| 70 | + with open(sys.argv[1], "w", encoding="utf8") as stream_out: |
| 71 | + _dump_versions_to(versions, stream_out) |
| 72 | + |
| 73 | + case _: |
| 74 | + print( |
| 75 | + f"""\ |
| 76 | +Usage: {sys.argv[0]} [<versions.json>] |
| 77 | +
|
| 78 | +If <versions.json> is given, the contents will be replaced with the sorted versions. |
| 79 | +Otherwise, the contents are read from stdin and the sorted versions are printed to stdout. |
| 80 | +""", |
| 81 | + file=sys.stderr, |
| 82 | + ) |
| 83 | + sys.exit(2) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments