# Path: src/setuptools_scm/utils.py
# def data_from_mime(path):
#     with open(path, encoding="utf-8") as fp:
#         content = fp.read()
#     trace("content", repr(content))
#     # the complex conditions come from reading pseudo-mime-messages
#     data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x)
#     trace("data", data)
#     return data
# 
# Path: src/setuptools_scm/utils.py
# def trace(*k) -> None:
#     if DEBUG:
#         print(*k, file=sys.stderr, flush=True)
# 
# Path: src/setuptools_scm/version.py
# def meta(
#     tag,
#     distance: "int|None" = None,
#     dirty: bool = False,
#     node: "str|None" = None,
#     preformatted: bool = False,
#     branch: "str|None" = None,
#     config: "Configuration|None" = None,
#     **kw,
# ) -> ScmVersion:
#     if not config:
#         warnings.warn(
#             "meta invoked without explicit configuration,"
#             " will use defaults where required."
#         )
#     parsed_version = _parse_tag(tag, preformatted, config)
#     trace("version", tag, "->", parsed_version)
#     assert parsed_version is not None, "Can't parse version %s" % tag
#     return ScmVersion(
#         parsed_version, distance, node, dirty, preformatted, branch, config, **kw
#     )
# 
# Path: src/setuptools_scm/version.py
# def tag_to_version(tag, config: "Configuration | None" = None):
#     """
#     take a tag that might be prefixed with a keyword and return only the version part
#     :param config: optional configuration object
#     """
#     trace("tag", tag)
# 
#     if not config:
#         config = Configuration()
# 
#     tagdict = _parse_version_tag(tag, config)
#     if not isinstance(tagdict, dict) or not tagdict.get("version", None):
#         warnings.warn(f"tag {tag!r} no version found")
#         return None
# 
#     version = tagdict["version"]
#     trace("version pre parse", version)
# 
#     if tagdict.get("suffix", ""):
#         warnings.warn(
#             "tag {!r} will be stripped of its suffix '{}'".format(
#                 tag, tagdict["suffix"]
#             )
#         )
# 
#     version = config.version_cls(version)
#     trace("version", repr(version))
# 
#     return version


import os
from .utils import data_from_mime
from .utils import trace
from .version import meta
from .version import tag_to_version

def parse_pkginfo(root, config=None):

    pkginfo = os.path.join(root, "PKG-INFO")
    trace("pkginfo", pkginfo)
    data = data_from_mime(pkginfo)
    version = data.get("Version")
    if version != "UNKNOWN":
        return meta(version, preformatted=True, config=config)


def parse_pip_egg_info(root, config=None):
    pipdir = os.path.join(root, "pip-egg-info")
    if not os.path.isdir(pipdir):
        return
    items = os.listdir(pipdir)
    trace("pip-egg-info", pipdir, items)
    if not items:
        return
    return parse_pkginfo(os.path.join(pipdir, items[0]), config=config)


def fallback_version(root, config=None):
    if config.parentdir_prefix_version is not None:
        _, parent_name = os.path.split(os.path.abspath(root))
        if parent_name.startswith(config.parentdir_prefix_version):
            version = tag_to_version(
                parent_name[len(config.parentdir_prefix_version) :], config
            )
            if version is not None:

# Path: src/setuptools_scm/utils.py
# def data_from_mime(path):
#     with open(path, encoding="utf-8") as fp:
#         content = fp.read()
#     trace("content", repr(content))
#     # the complex conditions come from reading pseudo-mime-messages
#     data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x)
#     trace("data", data)
#     return data
# 
# Path: src/setuptools_scm/utils.py
# def trace(*k) -> None:
#     if DEBUG:
#         print(*k, file=sys.stderr, flush=True)
# 
# Path: src/setuptools_scm/version.py
# def meta(
#     tag,
#     distance: "int|None" = None,
#     dirty: bool = False,
#     node: "str|None" = None,
#     preformatted: bool = False,
#     branch: "str|None" = None,
#     config: "Configuration|None" = None,
#     **kw,
# ) -> ScmVersion:
#     if not config:
#         warnings.warn(
#             "meta invoked without explicit configuration,"
#             " will use defaults where required."
#         )
#     parsed_version = _parse_tag(tag, preformatted, config)
#     trace("version", tag, "->", parsed_version)
#     assert parsed_version is not None, "Can't parse version %s" % tag
#     return ScmVersion(
#         parsed_version, distance, node, dirty, preformatted, branch, config, **kw
#     )
# 
# Path: src/setuptools_scm/version.py
# def tag_to_version(tag, config: "Configuration | None" = None):
#     """
#     take a tag that might be prefixed with a keyword and return only the version part
#     :param config: optional configuration object
#     """
#     trace("tag", tag)
# 
#     if not config:
#         config = Configuration()
# 
#     tagdict = _parse_version_tag(tag, config)
#     if not isinstance(tagdict, dict) or not tagdict.get("version", None):
#         warnings.warn(f"tag {tag!r} no version found")
#         return None
# 
#     version = tagdict["version"]
#     trace("version pre parse", version)
# 
#     if tagdict.get("suffix", ""):
#         warnings.warn(
#             "tag {!r} will be stripped of its suffix '{}'".format(
#                 tag, tagdict["suffix"]
#             )
#         )
# 
#     version = config.version_cls(version)
#     trace("version", repr(version))
# 
#     return version


# Path: src/setuptools_scm/hacks.py
import os
from .utils import data_from_mime
from .utils import trace
from .version import meta
from .version import tag_to_version


def parse_pkginfo(root, config=None):

    pkginfo = os.path.join(root, "PKG-INFO")
    trace("pkginfo", pkginfo)
    data = data_from_mime(pkginfo)
    version = data.get("Version")
    if version != "UNKNOWN":
        return meta(version, preformatted=True, config=config)


def parse_pip_egg_info(root, config=None):
    pipdir = os.path.join(root, "pip-egg-info")
    if not os.path.isdir(pipdir):
        return
    items = os.listdir(pipdir)
    trace("pip-egg-info", pipdir, items)
    if not items:
        return
    return parse_pkginfo(os.path.join(pipdir, items[0]), config=config)


def fallback_version(root, config=None):
    if config.parentdir_prefix_version is not None:
        _, parent_name = os.path.split(os.path.abspath(root))
        if parent_name.startswith(config.parentdir_prefix_version):
            version = tag_to_version(
                parent_name[len(config.parentdir_prefix_version) :], config
            )
            if version is not None:

                return meta(str(version), preformatted=True, config=config)