Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve extension discovery using custom setuptools entry points #691

Open
arthurlm opened this issue Apr 6, 2022 · 0 comments
Open

Improve extension discovery using custom setuptools entry points #691

arthurlm opened this issue Apr 6, 2022 · 0 comments

Comments

@arthurlm
Copy link

arthurlm commented Apr 6, 2022

Problem description

It would be great if smart_open could be extended using setuptools / entry_points capabilities.
For now, if user install a package that provide new smart_open transport mechanism, it cannot be automatically registered.

Example use case

Please note: schema is not relevant here.

In package smart_ext_transport I have following file:

# smart_ext_transport/custom_transport.py
SCHEMA = "custom"
...

Current situation

The only way to load extension for now are listed bellow:

Option 1: using register_transport everywhere and every time it might be required 😢

register_transport("smart_ext_transport.custom_transport")

Option 2: using __init__.py and import statement:

Adding:

# smart_ext_transport/__init__.py
register_transport("smart_ext_transport.custom_transport")

Then every time custom schema might be needed:

import smart_ext_transport

Improved situation with setuptools

In setup.py from smart_ext_transport we may add:

from setuptools import setup

setup(
    name="smart_ext_transport",
    packages=["smart_ext_transport"],
    entry_points={"smart_open_transport": [
        "custom = smart_ext_transport.custom_transport"],
    },
)

Once package is installed, smart_open can automatically load every plugin that is registered using setuptools entry points.
User will no longer have to import package to register extension manually.

This is how pytest / setuptools CLI / flake8 / jupyter nb convert works for example.

Possible implementation

Feature can be easily implemented.

It would require adding following lines between: https://github.com/RaRe-Technologies/smart_open/blob/84e9aac271653e029d68b090b02c545e7b43efcb/smart_open/transport.py#L103-L105

from importlib import metadata

for ep in metadata.entry_points()['smart_open_transport']:
    try:
        register_transport(ep.value)
    except Exception:
        logger.warning("Fail to load smart_open transport extension: %s", ep.name)

I can provide PR for this if needed 😁.

Versions

Linux-4.15.0-175-generic-x86_64-with-glibc2.27
Python 3.8.0 (default, Dec  9 2021, 17:53:27) 
[GCC 8.4.0]
smart_open 5.2.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants