Hatch build hook plugin for Polylith
Project description
Hatch Build Hook for Polylith
A plugin for Hatch and the Polylith Architecture.
This build hook will look for Polylith bricks
in pyproject.toml
and optionally re-write the imports made in the source code.
Installation
[build-system]
requires = ["hatchling", "hatch-polylith-bricks"]
build-backend = "hatchling.build"
But why re-write code?
Building libraries is supported in the Python tools for the Polylith Architecture, but you will need to consider that code will share the same top namespace with any other library built from the same monorepo.
This can be a problem when more than one of your libraries are installed into the same virtual environment. Python libraries by default are installed in a "flat" folder structure, two libraries with the same top namespace will collide.
A Solution: add a custom top namespace during packaging of the library with Hatch and this build hook plugin.
How is this done?
The code in this repo uses AST (Abstract Syntax Tree) parsing to modify source code.
The Python built-in ast
module is used to parse and un-parse Python code.
What's the output from this plugin?
Without any custom namespace in the configuration: no changes in the code. Building and packaging as-is.
With a Top Namespace configuration
[tool.hatch.build.hooks.polylith-bricks]
top-namespace = "my_custom_namespace"
my_custom_namespace/
my_namespace/
/my_package
__init__.py
my_module.py
Before:
from my_namespace.my_package import my_function
After:
from my_custom_namespace.my_namespace.my_package import my_function
Usage
Key | Default | Description |
---|---|---|
work-dir | .polylith_tmp | The temporary working directory for copying and re-writing source code. |
top-namespace | None | A custom top namespace. When set, Polylith bricks will be updated using this namespace. |
This Plugin expects to find Polylith Bricks in the pyproject.toml
:
[tool.polylith.bricks]
"../../bases/my_namespace/my_base" = "my_namespace/my_base"
"../../components/my_namespace/my_component" = "my_namespace/my_component
Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hatch_polylith_bricks-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 500a9f80d5c3edab5d9d4279b39bf6f89a490feefcdc95637b639ea953dcc64a |
|
MD5 | 4ff1cc6cbbb6ee7b22d69ee3517896c3 |
|
BLAKE2b-256 | f58433c7496bd7a7b20be7f5f3c99294873f520e6636a0999a3ae264e1a2ad79 |
Hashes for hatch_polylith_bricks-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d14fc170c53d88bf3bd5b9ba7022a50664e34fbad31bdd06b06afdce623613bb |
|
MD5 | 5aee5347ebd8390d99961818a31e0341 |
|
BLAKE2b-256 | 950239fe033881ebbd670655a42521c364b4b34e6f3001df984e835739347edf |