click_extra.project module¶
Find a local project’s entry point and packages from its packaging metadata.
read_project_scripts() lists the console scripts a project declares,
entry_point_package() names the top-level package of an entry point, and
locate_package_root() finds the directory a package imports from, whether
the project uses the flat or the src layout.
resolve_entry_point() combines them into the one entry point of a project,
under one ambiguity rule. The wrap command runs a project directory through
it, and detect_source_package() finds the package the convert-to-myst
command converts. discover_package_init_files() finds the __init__.py
of every package the scripts enter, for the prebake commands to bake.
- click_extra.project.read_project_scripts(directory)[source]¶
Read the console-script entry points declared by a local project.
Inspects PEP 621
[project.scripts]inpyproject.tomlfirst, thenconsole_scriptsunder[options.entry_points]insetup.cfg. The dynamicsetup.pyform cannot be parsed statically and is ignored.
- click_extra.project.entry_point_package(target)[source]¶
The top-level package an entry point target lives in.
orchard.cli:mainandorchard:mainboth answerorchard: the module path before the colon, cut at its first dot.
- click_extra.project.locate_package_root(directory, top_package)[source]¶
Find which directory must be on
sys.pathto importtop_package.Handles the two common project layouts: the flat layout (the package sits directly under the project root) and the src layout (under a
src/subdirectory). A package directory, with or without an__init__.py, and a single-module{top_package}.pyall count.- Parameters:
directory (
Path) – the project root.top_package (
str) – the top-level package name, asentry_point_package()reads it off an entry point.
- Return type:
- Returns:
the directory to prepend to
sys.path, orNonewhen the package cannot be located on disk.
- class click_extra.project.ProjectEntryPoint(target, root)[source]¶
Bases:
NamedTupleThe console-script entry point a project directory resolves to.
Create new instance of ProjectEntryPoint(target, root)
- root: Path | None¶
The directory to put on
sys.pathto import the target, orNonewhen its package is not on disk.
- property package: str¶
The top-level package of the target, see
entry_point_package().
- click_extra.project.resolve_entry_point(directory)[source]¶
Resolve the one console-script entry point of the project in
directory.Reads the console scripts with
read_project_scripts(), and locates the package of their target withlocate_package_root().The ambiguity rule: the console scripts must name a single
module:functiontarget. Several script names for one target, like a long name and its short alias, count as one. Two targets are ambiguous even when they live in one package: nothing tells which function the project means.- Parameters:
directory (
Path) – the project root, holding itspyproject.tomlorsetup.cfg.- Return type:
- Returns:
the entry point, whose
rootisNonewhen its package is not on disk.- Raises:
ValueError – When the project declares no console script, or scripts naming several targets.
- click_extra.project.detect_source_package(directory=None)[source]¶
Locate the project’s source package from its script entry point.
Resolves the entry point with
resolve_entry_point(), under its ambiguity rule, and derives the top-level package of its target ("pkg.cli:main"givespkg), so theconvert-to-mystcommand can run bare from a project root. The package is located under the project root or itssrc/folder, seelocate_package_root(), and assumed under the project root when it is in neither.- Parameters:
directory (
Path|None) – The project root, holding itspyproject.tomlorsetup.cfg. Defaults to the current working directory.- Return type:
- Returns:
Path of the detected package directory.
- Raises:
ValueError – When the project declares no script entry point, or scripts naming several targets.
- click_extra.project.discover_package_init_files(directory=None)[source]¶
Discover the
__init__.pyof each package a project’s console scripts enter.Reads the console scripts of the project in
directorywithread_project_scripts(), takes the top-level package of each entry point and locates it on disk withlocate_package_root(), so a src-layout project resolves like a flat one.