Skip to main content

Asynchronous generators yielding detected hotplug events on the USB buses

Project description

aio-usb-hotplug

aio-usb-hotplug is a Python library that provides asynchronous generators yielding detected hotplug events on the USB buses.

Requires Python >= 3.9.

Works with asyncio and trio.

Installation

Use the package manager pip to install aio-usb-hotplug.

pip install aio-usb-hotplug

aio-usb-hotplug depends on PyUSB, which in turn requires libusb or openusb. An easy way to satisfy this requirement is to install libusb-package, which supplies pre-compiled binaries for most platforms:

pip install libusb-package

aio-usb-hotplug will make use of libusb-package if it is installed in the current Python environment.

Usage

Dump all hotplug events related to a specific USB device

from aio_usb_hotplug import HotplugDetector
from trio import run  # ...or asyncio

async def dump_events():
    detector = HotplugDetector.for_device(vid="1050", pid="0407")
    async for event in detector.events():
        print(repr(event))

trio.run(dump_events)

Run an async task for each USB device matching a VID/PID pair

from aio_usb_hotplug import HotplugDetector
from trio import sleep_forever


async def handle_device(device):
    print("Handling device:", repr(device))
    try:
        # Do something meaningful with the device. The task gets cancelled
        # when the device is unplugged.
        await sleep_forever()
    finally:
        # Device unplugged or an exception happened
        print("Stopped handling device:", repr(device))


async def handle_detected_devices():
    detector = HotplugDetector.for_device(vid="1050", pid="0407")
    await detector.run_for_each_device(handle_device)


trio.run(handle_detected_devices)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

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

aio_usb_hotplug-7.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

aio_usb_hotplug-7.0.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file aio_usb_hotplug-7.0.1.tar.gz.

File metadata

  • Download URL: aio_usb_hotplug-7.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aio_usb_hotplug-7.0.1.tar.gz
Algorithm Hash digest
SHA256 1eb3a74262d07de29f5b121437db53ef569d79c586bae7803144e3f0b9ae566f
MD5 3184be1853bc18640976d7e5f4570dc0
BLAKE2b-256 c85cd5f9eb7c2dad83d9297c53f42ec7b56e7540ffb128073c0a53ca5de1f28f

See more details on using hashes here.

File details

Details for the file aio_usb_hotplug-7.0.1-py3-none-any.whl.

File metadata

  • Download URL: aio_usb_hotplug-7.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aio_usb_hotplug-7.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f8dcd83754d17508f94408d4875b9a191b68ad5a28ebe67331178da2ff12331
MD5 c86d6a29f3e7e29e8724fc14788d03d6
BLAKE2b-256 b04c28cf8148ef5829f60d51fb38b1a893ca7995b26c33d49388eed6aff2d667

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