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.8.

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-6.0.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

aio_usb_hotplug-6.0.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aio_usb_hotplug-6.0.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.0 CPython/3.11.5 Linux/6.2.0-1011-azure

File hashes

Hashes for aio_usb_hotplug-6.0.0.tar.gz
Algorithm Hash digest
SHA256 544e9e4289f9c34b71a49054c7b1744291b8f55a1681847aae89a062d95c0b1a
MD5 6039743cb016dfeb3e7d82c0802f5c1c
BLAKE2b-256 6e88f86cdf626ef3a40bee73c6b3ff2148aba0936dc9594fc57116bea75e9f73

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aio_usb_hotplug-6.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.0 CPython/3.11.5 Linux/6.2.0-1011-azure

File hashes

Hashes for aio_usb_hotplug-6.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fa402ea0e60ed0bebdc38cf61ee0d3095252544bed0c2489fbeb2851f7d0dea
MD5 bf7d6c8837a5726ebc24c0560e1d923b
BLAKE2b-256 441499a0025277d0fd4d602d1aea6a2da4951a6e6603bcea3f47a5f3d17ff66a

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