#!/usr/bin/env python3

# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license

import os
import tomllib

with open("pyproject.toml", "rb") as pp:
    pyproject = tomllib.load(pp)


def maybe_simplify(dep: str):
    i = dep.find(";")
    if i >= 0:
        return dep[:i]
    else:
        return dep


FEATURES = "dns/_features.py"
NEW_FEATURES = FEATURES + ".new"
skip = False
with open(FEATURES, "r") as input:
    with open(NEW_FEATURES, "w") as output:
        for l in input.readlines():
            l = l.rstrip()
            if l.startswith("    ### BEGIN generated requirements"):
                print(l, file=output)
                for name, deps in pyproject["project"]["optional-dependencies"].items():
                    if name == "dev":
                        continue
                    deps = [maybe_simplify(dep) for dep in deps]
                    print(
                        f"    {repr(name)}: {repr(deps)},",
                        file=output,
                    )
                skip = True
            elif l.startswith("    ### END generated requirements"):
                skip = False
            if not skip:
                print(l, file=output)
os.rename(NEW_FEATURES, FEATURES)
