Skip to main content

spec.module_path

spec.module_path(root, source_dir, package, name)

The repository-relative path of the module of a unit, derived from its name.

That is <source_dir>/<a>/<b>/__init__.py if that file exists below root, else <source_dir>/<a>/<b>.py for the name <a>.<b>, which starts with package; the package itself is its __init__.py. A package directory therefore wins over a module of the same name. The path is POSIX, normalised (source_dir "src/" and "./src" are src; "." adds nothing) and never starts with ./. The file need not exist: the check reports a unit whose module is missing.

Parameters

Name Type Description Default
root Path The repository root, where a package directory is looked for. required
source_dir str The source_dir of the architecture. required
package str The top-level package. required
name str The dotted module name, which starts with package. required

Returns

Name Type Description
str str The repository-relative POSIX path.

Raises

Name Type Description
SpecError If source_dir is empty, absolute or leaves the repository.

Examples

from pathlib import Path

from babregen.spec import module_path

print(module_path(Path("."), "src/", "pkg", "pkg.core.calc"))
src/pkg/core/calc.py