Find and load distribution metadata for the installed package containing this file.
Source code in src/cli_app/utils/meta.py
| @classmethod
def load_from_installed_package(cls) -> Self:
"""Find and load distribution metadata for the installed package containing this file."""
pkg_file = Path(__file__).resolve()
# For editable installs uv_build uses a .pth file and omits top_level.txt,
# so packages_distributions() won't find us. Instead, find the distribution
# whose direct_url.json points to a directory that contains this file.
dist_name = cls._find_dist_name_from_direct_url(pkg_file)
# Fall back to packages_distributions() for non-editable installs.
if dist_name is None:
top_level_pkg = __package__.split(".")[0] if __package__ else None
if top_level_pkg:
dist_names = metadata.packages_distributions().get(top_level_pkg)
if dist_names:
dist_name = dist_names[0]
if dist_name is None:
return cls(data=cls._get_fallback_meta("Unknown"))
try:
meta_json = metadata.metadata(dist_name).json
author_email = cast(str, meta_json.pop("author_email", ""))
requires_dist = meta_json.pop("requires_dist", [])
meta_dict_keys = MetaDict.__annotations__.keys()
meta = cast(MetaDict, {k: v for k, v in meta_json.items() if k in meta_dict_keys})
authors: list[ContactDict] = []
for author_string in author_email.split(","):
name, email = parseaddr(author_string)
authors.append({"name": name, "email": email})
meta["authors"] = authors
meta["dependencies"] = cls._get_installed_dependencies(cast(list[str], requires_dist))
return cls(data=meta)
except metadata.PackageNotFoundError:
return cls(data=cls._get_fallback_meta(dist_name))
|