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] in pyproject.toml first, then console_scripts under [options.entry_points] in setup.cfg. The dynamic setup.py form cannot be parsed statically and is ignored.

Parameters:

directory (Path) – the project root, holding the packaging metadata.

Return type:

dict[str, str]

Returns:

a mapping of each console-script name to its module:function target. Empty when the project declares none.

click_extra.project.entry_point_package(target)[source]

The top-level package an entry point target lives in.

orchard.cli:main and orchard:main both answer orchard: the module path before the colon, cut at its first dot.

Parameters:

target (str) – the entry point, as module:function.

Return type:

str

Returns:

the top-level package name.

click_extra.project.locate_package_root(directory, top_package)[source]

Find which directory must be on sys.path to import top_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}.py all count.

Parameters:
  • directory (Path) – the project root.

  • top_package (str) – the top-level package name, as entry_point_package() reads it off an entry point.

Return type:

Path | None

Returns:

the directory to prepend to sys.path, or None when the package cannot be located on disk.

class click_extra.project.ProjectEntryPoint(target, root)[source]

Bases: NamedTuple

The console-script entry point a project directory resolves to.

Create new instance of ProjectEntryPoint(target, root)

target: str

The module:function target the console scripts name.

root: Path | None

The directory to put on sys.path to import the target, or None when its package is not on disk.

property module: str

The module path of the target, before its colon.

property function: str

The function name of the target, after its colon.

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 with locate_package_root().

The ambiguity rule: the console scripts must name a single module:function target. 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 its pyproject.toml or setup.cfg.

Return type:

ProjectEntryPoint

Returns:

the entry point, whose root is None when 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" gives pkg), so the convert-to-myst command can run bare from a project root. The package is located under the project root or its src/ folder, see locate_package_root(), and assumed under the project root when it is in neither.

Parameters:

directory (Path | None) – The project root, holding its pyproject.toml or setup.cfg. Defaults to the current working directory.

Return type:

Path

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__.py of each package a project’s console scripts enter.

Reads the console scripts of the project in directory with read_project_scripts(), takes the top-level package of each entry point and locates it on disk with locate_package_root(), so a src-layout project resolves like a flat one.

Parameters:

directory (Path | None) – the project root. None reads the current directory.

Return type:

list[Path]

Returns:

each __init__.py found, once, in entry point order. Empty when the project declares no console script, or none of their packages is found.