Access file paths from pyproject.toml
Project description
project-paths
Access file paths from pyproject.toml
Thanks to @Madoshakalaka for the idea!
# pyproject.toml
[tool.project-paths]
readme = "README.md"
# app.py
from project_paths import paths
# paths.readme is a pathlib.Path object:
print(paths.readme.read_text())
Install
pip install project-paths
Usage
Does your application have a bunch of configurable file paths? Do you wish you just had one place to configure list them all?
Add paths to [tool.project-paths]
With this module, define your paths in your pyproject.toml
file under
the [tool.project-paths]
table:
[tool.project-paths]
docs = "path/to/my/docs"
settings = "path/to/my/settings.py"
config = "/opt/path/to/my/config
# Add as many paths as you want!
Anything string defined with [tool.project-paths]
will be made
available. Relative paths are relative to pyproject.toml
.
Access paths using project_paths.paths.<path name>
Now you can access all the paths listed in pyproject.toml
with
project_paths.paths
. Every path is returned as
a pathlib.Path
object:
from project_paths import paths
print(paths.docs.glob("*.md"))
assert paths.config.exists()
exec(paths.settings.read_text())
# Or anything you want!
Caveats
Names in [tool.project-paths]
should be a valid Python identifier
and the names cannot have a leading underscore. If a name has
a leading underscore, a warning is issued and the name is inaccessible:
[tool.project-paths]
# BAD: paths that start with a '_' cannot be used
_my_path = "path/to/wherever"
# GOOD: path is a valid Python identifier!
my_path = "path/to/wherever"
Errors
All intentional errors inherit from project_paths.ProjectPathError
.
PyProjectNotFoundError
Raised when project_paths
cannot find an appropriate pyproject.toml
for the caller. This can happen if the caller simply does not have
a pyproject.toml
file, or when it's not possible to determine the
caller's filename.
NOTE: if accessing paths from the interactive console,
project_paths
will find the appropriatepyproject.toml
relative to the current working directory!
ConfigurationNotFoundError
Raised when project_paths
cannot find the [tool.project-paths]
table
within the pyproject.toml
file. Perhaps there is a typo or perhaps the
project_paths
inferred the incorrect module path.
License
2021 © Eddie Antonio Santos. MIT Licensed.
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
File details
Details for the file project-paths-0.2.0.tar.gz
.
File metadata
- Download URL: project-paths-0.2.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 773987e16cb39a32b01728b760cb28098ea29be6d2f091438724eb03b5069fc1 |
|
MD5 | 448b8d96202bf07763520ea924eb0b31 |
|
BLAKE2b-256 | 0978cc38cc6d02ecc943ec0228a3a2fb96b2da150a35cef0fc43c73a70cec0cc |
File details
Details for the file project_paths-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: project_paths-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69b7f99c36dc1825cc13a02d2324553faef08e5fb47dfb70b857498af1c67347 |
|
MD5 | f088773a1b4267d327ae9143632319f5 |
|
BLAKE2b-256 | f5ec50ac21a8971f9d9fd5697c480a689bbfbf628d48b815aa0b3983789013cd |