Skip to main content

Type hints for the Wolpi Python extension API

Project description

wolpi-extension-api

Type hints for writing Wolpi Python extensions.

Install this package to get type checking and editor IntelliSense for Wolpi APIs. The types cover all the values accessible via the wolpi, wolpi.errors, and java modules, as well as all types passed into or returned from the extension hooks. Inside Wolpi/GraalPy, the real wolpi and java modules are injected by the runtime; this package is for local type checking and editor support.

Install

python -m pip install wolpi-extension-api

Or add it to your development environment with your preferred tool, for example:

uv add --dev wolpi-extension-api

Once the package is installed in the environment used by Pyright, Pylance, mypy, or another type checker, no extra configuration is required.

Examples

Class-based Extensions

The WolpiExtension base class models the required hooks and provides typed defaults for the optional ones.

from wolpi import ExtensionInfo, WolpiExtension, HttpHeaders


class Extension(WolpiExtension):
    def info(self) -> ExtensionInfo:
        return {
            "apiVersion": 1,
            "name": "Example",
            "description": "Typed hooks",
        }

    def cleanup(self) -> None:
        pass

    def authorize(
        self,
        identifier: str,
        headers: HttpHeaders,
        client_ip: str,
    ) -> bool:
        return client_ip == "127.0.0.1"

Dict-based Extensions

from wolpi import ExtensionInfo, WolpiExtensionDict, HttpHeaders


def info() -> ExtensionInfo:
    return {
        "apiVersion": 1,
        "name": "Example",
        "description": "Dict-based extension",
    }


def cleanup() -> None:
    pass


def authorize(
    identifier: str,
    headers: HttpHeaders,
    client_ip: str,
) -> bool:
    return client_ip == "127.0.0.1"


def wolpi_extension() -> WolpiExtensionDict:
    return {
        "info": info,
        "cleanup": cleanup,
        "authorize": authorize,
    }

Using java.type

Use java.type() when you need to resolve a Java class for static helpers, factories, enums, or other host APIs that are not already passed into the hook as values.

For example, image-processing hooks already receive a VImage instance as the image parameter, but you can still resolve the VImage class itself to call static vips-ffm helpers such as VImage.text(...).

import java
from wolpi import (
    ImageApiRequest,
    ImageInfo,
    ExtensionInfo,
    VImage,
    WolpiExtensionDict,
    vipsArena,
)


VImageClass = java.type("app.photofox.vipsffm.VImage")
VipsInteresting = java.type("app.photofox.vipsffm.enums.VipsInteresting")
VipsEnumOption = java.type("app.photofox.vipsffm.VipsOption.Enum")


# Inserts the image identifier as a text watermark in the upper right corner
def pre_process_image(
    image: VImage,
    identifier: str,
    image_info: ImageInfo,
    request: ImageApiRequest,
) -> VImage | None:
    watermark = VImageClass.text(vipsArena, identifier)
    return image.insert(watermark, 16, 16)


# Adds a custom `most-interesting` cropping parameter syntax that uses vips'
# smartcrop feature to select the most interesting square region of the image
def pre_crop(
    image: VImage,
    identifier: str,
    image_info: ImageInfo,
    request: ImageApiRequest,
) -> VImage | None:
    if request.cropSpec != "most-interesting":
        return None
    smallest_dim = min(image_info.nativeSize.width, image_info.nativeSize.height)
    return image.smartcrop(
        smallest_dim,
        smallest_dim,
        VipsEnumOption("interesting", VipsInteresting.INTERESTING_ATTENTION),
    )


def info() -> ExtensionInfo:
    return {
        "apiVersion": 1,
        "name": "smartcrop-watermark",
        "description": "Example extension",
    }


def wolpi_extension() -> WolpiExtensionDict:
    return {
        "info": info,
        "cleanup": lambda: None,
        "pre_process_image": pre_process_image,
        "pre_crop": pre_crop,
    }

The image parameter is already a VImage host object, so you call instance methods on it directly. The java.type() for VImage is only needed here for the static helper on the Java class.

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

wolpi_extension_api-0.2.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wolpi_extension_api-0.2.0-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file wolpi_extension_api-0.2.0.tar.gz.

File metadata

  • Download URL: wolpi_extension_api-0.2.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wolpi_extension_api-0.2.0.tar.gz
Algorithm Hash digest
SHA256 92ed44e760b54a0f8fa0072826c715db6c17c2046c828c581c9a0a8fd98780ed
MD5 c6f93423a7be18743367aa2331076d03
BLAKE2b-256 47d7c1409bf91e3214fd7df158f33c35657fc29acf056bd1e8d68536924046ce

See more details on using hashes here.

File details

Details for the file wolpi_extension_api-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: wolpi_extension_api-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wolpi_extension_api-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 180ca0f40a8f278b2a2c3f45abf3b8be136d8537595042237e5b0277bd4e3911
MD5 bd8e85028bc0afd0b22a1a8bc7bdb054
BLAKE2b-256 4618200e4903402287cbb8f2e0739f87cdfb4e5ebe6a3ce59c8bd843b102b0ea

See more details on using hashes here.

Supported by

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