# encoding: utf-8
# api: pep517
# title: flit backend
# description: wraps flit_core.buildapi
# version: 0.2
# depends: python:flit (>=3.0, <4.0)
# license: BSD-3-Clause
# priority: extra
# src: ~/.local/lib/python3.8/site-packages/flit_core/
#
# This is supposed to become an alternative to pluginconf.setup,
# using flit as pep517 build backend. But adding automagic field
# lookup of course.
#
# It can be invoked per `flit-pluginconfig build` and requires
# a `pyproject.toml` like:
#
#       [build-system]
#       requires = ["flit_core", "pluginconf"]
#       build-backend = "pluginconf.flit"
#
#       [project]
#       name = "foobar"
#       #dynamic = ["version", "description"]
#
# Injecting attributes between ini reading and parameter collection
# turned out easier than expanding on flit_core.buildapi functions.
# And lastly, this just chains to flit.main() to handle setup and
# build steps.
#

import sys
import re
import functools

import pluginconf
import pluginconf.setup as psetup

import flit_core.common


#-- patchy patch
def inject(where):
    def wrapped(func):
        setattr(where, func.__name__, func)
    return wrapped

@inject(flit_core.common)
def read_flit_config(path):
    """ read_flit_config() with preset dynamic fields """
    d = tomllib.loads(path.read_text('utf-8'))
    
    # make fields dynamic
    if not "dynamic" in d["project"]:
        d["project"]["dynamic"] = []
    for dyn in ['description', 'version']: 
        if dyn in d["project"]:
            del d["project"][dyn]
        if not dyn in d["project"]["dynamic"]:
            d["project"]["dynamic"].append(dyn)
    print(d)

    # turn it into LoadedConfig
    return prep_toml_config(d, path)

# override make_metadata
@inject(flit_core.common)
def make_metadata(module, ini_info):
    meta = {
        "name": module.name,
        "provides": [module.name]
    }
    meta.update(ini_info.metadata)
    meta.update(
        pmd_meta(
            pluginconf.plugin_meta(filename=module.file),
            ini_info
        )
    )
    return flit_core.common.Metadata(meta)

# map plugin meta to flit Metadata
def pmd_meta(pmd, ini):
    """ enjoin PMD fields with flit meta data """
    meta = dict(
        summary = pmd.get("description"),
        version = pmd.get("version"),
        home_page = pmd.get("url"),
        author = pmd.get("author"),  # should split this into mail and name
        author_email = None,
        maintainer = None,
        maintainer_email = None,
        license = pmd.get("license"),
        keywords = psetup._keywords(pmd),
        download_url = None,
        requires_python = psetup._python_requires(pmd).get("python_requires", ">= 2.7"),
        platform = pmd.get("architecture"),
        supported_platform = (),
        classifiers = list(psetup._classifiers(pmd))
                    + psetup._trove_license(pmd)
                    + psetup._trove_status(pmd),
        provides = (),
        requires = psetup._install_requires(pmd).get("install_requires") or (),
        obsoletes = (),
        project_urls = [f"{k}, {v}" for k,v in psetup._project_urls(pmd).items()],
        provides_dist = (),
        requires_dist = psetup._install_requires(pmd).get("install_requires") or (),
        obsoletes_dist = (),
        requires_external = (),
        provides_extra = (),
    )

    # comment/readme
    for docs in psetup._plugin_doc(pmd), psetup._get_readme():
        if docs["long_description"]:
            meta.update({
                k[5:]: v for k,v in docs.items()
            })

    # entry_points are in ini file
    for section, entries in psetup._entry_points(pmd).items():
        ini.entrypoints[section] = ini.entrypoints.get(section, {})
        for e in entries:
            ini.entrypoints[section].update(
                dict(re.findall("(.+)=(.+)", e))
            )
    print(ini.entrypoints)
    
    # strip empty entries
    return {k: v for k, v in meta.items() if v}



#-- buildapi
from flit_core.buildapi import (     # These have to be late imports; else they'll
    get_requires_for_build_wheel,    # bind with the original buildapi functions.
    get_requires_for_build_sdist,
    get_requires_for_build_editable,
    prepare_metadata_for_build_wheel,
    prepare_metadata_for_build_editable,
    build_wheel,
    build_editable,
    build_sdist,
)

#-- invocation point
from flit import main

if __name__ == "__main__":
    main(sys.argv)
