Skip to main content

Sitecustomize-module that executes all correctly registered ``sitecustomize``-entypoints.

Project description

Testing Linting Read the Docs PyPi Package MIT License

sitecustomize-entrypoints

Overview

sitecustomize-entrypoints is a library that installs a python-module called sitecustomize, and allows you to define and register any callable as a sitecustomize-entrypoint in your project's setup.py or pyproject.toml.

These callables will be then executed automatically whenever sitecustomize is imported during python-startup. This provides a simple & modular way to patch or extend the functionality of any python-code installed in your environment without modifying its source code.

sitecustomize is a special python-module that can be used to customize the python environment at startup. When the python-interpreter starts, it looks for a sitecustomize.py-file in the site-packages directory or any other directory specified in the PYTHONPATH. The sitecustomize.py-file is executed before any other Python code, allowing you to make customizations that will be applied to the entire python environment. For more information, check the official site.py-documentation.

Installation

You can install sitecustomize-entrypoints using pip:

> bin/pip install sitecustomize-entrypoints

Or add it to your poetry-based project:

> poetry add sitecustomize-entrypoints

Usage

To use sitecustomize-entrypoints, you need to define and register one or more entrypoints in your project's setup.py or pyproject.toml file. Here's an example:

[tool.poetry.plugins."sitecustomize"]
"my-action" = "my_project.action:my_action"

In this example, we're registering an entrypoint called my-action in the sitecustomize-group. This entrypoint points to the my_action-function in the my_project.actions-module.

Once you've registered your entrypoints, they will be executed automatically when the sitecustomize-module is imported.

import sitecustomize

Ordering

Entrypoints are sorted by name.

The ordering in which the entrypoints are defined in your setup.py or pyproject.toml are unfortunately not the order in which they are registered internally.

The entrypoints are first ordered alphanumerically by name.

So defined entrypoints are re-ordered from

[tool.poetry.plugins."sitecustomize"]
foo = "my_project.action:action_foo"
bar = "my_project.action:action_bar"

into

[tool.poetry.plugins."sitecustomize"]
bar = "my_project.action:action_bar"
foo = "my_project.action:action_foo"

This can cause issues when there are dependencies between your entrypoints.

TIP: You can use integer-prefixes to the name to enforce the ordering in which you define your entrypoints.:

[tool.poetry.plugins."sitecustomize"]
10-foo = "my_project.action:action_foo"
20-bar = "my_project.action:action_bar"

Cancel entrypoints registered by third-party packages

Imagine a third-party package has registered an entrypoint as follows:

[tool.poetry.plugins."sitecustomize"]
their-action = "external_module:their_action"

But it does not match the ordering you'd like.

You can cancel the execution of this plugin, by registering an entrypoint with the same name in your own setup.py or pyproject.toml file, and pointing to the no-action sitecustomize.cancel-function.

[tool.poetry.plugins."sitecustomize"]
 their-action = "sitecustomize:cancel"

By registering an entrypoint with the same name it overrides the previous registered action. Subsequently you can re-register this entrypoint by a different name to change the order of execution.

Overriding and re-ordering entrypoints

You can override and re-order registered entrypoints by defining your own entrypoints with the same name in your setup.py or pyproject.toml file. When sitecustomize executes registered entrypoints, it uses a FIFO-approach (first in, first out): Only the last registered entrypoint with a given name is executed. Any prior entrypoints with the same name are filtered out (overriden by the last).

To re-order entrypoints, you can use the cancel-function provided by sitecustomize-entrypoints. The cancel-function allows you to cancel out a previously registered entrypoint, so you can re-register it in a different order.

Here's an example to enforce a specific order of execution:

[tool.poetry.plugins."sitecustomize"]
"their-action = external_module:their_action"
[tool.poetry.plugins."sitecustomize"]
"their-action = sitecustomize:cancel"
"10-my-action = my_project.action:my_action"
"20-their-action = external_module:their_action"

In this example, we're re-ordering an entrypoint registered by a third-party module to be executed after our own. First we re-register it with the same name to override and cancel its action, and subsequently we re-register it under a different name to ensure it get executed after our own entrypoints, even if all entrypoints are sorted alphabetically

Display all registered entrypoint in order of execution

The sitecustomize.print_entrypoints-function provided by sitecustomize-entrypoints allows you to display a list of all registered entrypoints. By default, it will display all registered entrypoints in the sitecustomize-group in order of execution. You can optionally pass a different entrypoint group name as an argument. If you pass filtered=True as an argument, the function will also filter out any duplicate entrypoints and display only the last instance of each entrypoint. Here's an example usage:

import sitecustomize

# Display all registered entrypoints in the sitecustomize group
sitecustomize.print_entrypoints()

# More explicit equivalent
sitecustomize.print_entrypoints(group_name="sitecustomize")

# Display only the first instance of each registered entrypoint in the sitecustomize group
sitecustomize.print_entrypoints(filtered=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sitecustomize-entrypoints-1.1.0.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

sitecustomize_entrypoints-1.1.0-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file sitecustomize-entrypoints-1.1.0.tar.gz.

File metadata

  • Download URL: sitecustomize-entrypoints-1.1.0.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0b3 CPython/3.10.2 Darwin/19.6.0

File hashes

Hashes for sitecustomize-entrypoints-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8bf9c9f3572e1709331f435a9bffd465320be77d65adda0f82b504594dc507b4
MD5 b61da094185a364af0b2abb3326945ba
BLAKE2b-256 2588c135ebdef2af797042a55fba89c79ca05b601f4dc36157a67a224d8090ca

See more details on using hashes here.

File details

Details for the file sitecustomize_entrypoints-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sitecustomize_entrypoints-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca80648c58d9ebcad31de46a4726213ae322fe829b285a0105da0eae3f74cb53
MD5 1178b7ed389f419185b1b3185fe32fcb
BLAKE2b-256 6711412160ea4a304ee65babef67254833408d0ac7145334406bb1a8bff6f3cb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page