Metadata-Version: 2.1
Name: aio-usb-hotplug
Version: 1.0.0
Summary: UNKNOWN
Home-page: https://github.com/ntamas/aio-usb-hotplug
Author: Tamás Nepusz
Author-email: ntamas@gmail.com
License: UNKNOWN
Platform: UNKNOWN
Requires-Python: >=3.5
Description-Content-Type: text/markdown
Requires-Dist: anyio (>=1.2.0)
Requires-Dist: async-generator (>=1.10)
Requires-Dist: pyusb (>=1.0.2)
Requires-Dist: pyudev (>=0.21.0) ; platform_system == "Linux"
Provides-Extra: dev
Requires-Dist: curio (>=0.9) ; extra == 'dev'
Requires-Dist: pytest (>=5.2.4) ; extra == 'dev'
Requires-Dist: pytest-cov (>=2.0.1) ; extra == 'dev'
Requires-Dist: trio (>=0.13.0) ; extra == 'dev'
Requires-Dist: twine (>=3.0.0) ; extra == 'dev'

# 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.5.

Works with [`asyncio`](https://docs.python.org/3/library/asyncio.html),
[`curio`](https://curio.readthedocs.io/en/latest/) and
[`trio`](https://trio.readthedocs.io/en/stable/).

## Installation

Use the package manager [pip](https://pip.pypa.io/en/stable/) to install
`aio-usb-hotplug`.

```bash
pip install aio-usb-hotplug
```

## Usage

### Dump all hotplug events related to a specific USB device

```python
from aio_usb_hotplug import HotplugDetector
from trio import run  # ...or asyncio, or curio

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

```python
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](https://choosealicense.com/licenses/mit/)


