<commit_before># SPDX-License-Identifier: MIT

"""
Query the Read The Docs API and transform all active versions into
a mkdocs-material-theme-compatible versions.json file.

Needs to run after each tag.
"""

import json
import os
import sys

from typing import Any, TypedDict

import urllib3


def main(project: str, token: str) -> None:
    client = urllib3.PoolManager(
        headers={"Authorization": "Token " + token},
    )

    versions = collect_versions(client, project)
    with open("docs/versions.json", "w") as f:
        json.dump(versions, f, indent=2)
        f.write("\n")

    client.clear()  # type: ignore[no-untyped-call]


class Version(TypedDict):
    title: str
    aliases: list[str]
    version: str


def sieve_versions(vers: list[dict[str, Any]]) -> list[Version]:
    rv: list[Version] = []
    latest: Version | None = None
    for v in vers:
        vn = v["verbose_name"]
        if vn == "stable" or not v["active"]:
            continue

        path = f"{v['slug']}"
        if vn == "latest":
            latest = {
                "title": "Unreleased",
                "aliases": ["main", "latest"],
                "version": path,
            }
            continue

        rv.append({"title": vn, "version": path, "aliases": []})

    if latest:
        rv.insert(1, latest)

    return rv


def collect_versions(
    client: urllib3.PoolManager, project: str<commit_msg>Simplify path creation<commit_after># SPDX-License-Identifier: MIT

"""
Query the Read The Docs API and transform all active versions into
a mkdocs-material-theme-compatible versions.json file.

Needs to run after each tag.
"""

import json
import os
import sys

from typing import Any, TypedDict

import urllib3


def main(project: str, token: str) -> None:
    client = urllib3.PoolManager(
        headers={"Authorization": "Token " + token},
    )

    versions = collect_versions(client, project)
    with open("docs/versions.json", "w") as f:
        json.dump(versions, f, indent=2)
        f.write("\n")

    client.clear()  # type: ignore[no-untyped-call]


class Version(TypedDict):
    title: str
    aliases: list[str]
    version: str


def sieve_versions(vers: list[dict[str, Any]]) -> list[Version]:
    rv: list[Version] = []
    latest: Version | None = None
    for v in vers:
        vn = v["verbose_name"]
        if vn == "stable" or not v["active"]:
            continue

        path = v["slug"]
        if vn == "latest":
            latest = {
                "title": "Unreleased",
                "aliases": ["main", "latest"],
                "version": path,
            }
            continue

        rv.append({"title": vn, "version": path, "aliases": []})

    if latest:
        rv.insert(1, latest)

    return rv


def collect_versions(
    client: urllib3.PoolManager, project: str
{'fraction_non_alphanumeric': 0.13135593220338984, 'fraction_numerical': 0.0035310734463276836, 'mean_word_length': 2.4091456077015643, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3714928', 'n_tokens_mistral': 895, 'n_tokens_neox': 870, 'n_words': 255}