USB Manager¶
usb ¶
Classes¶
USBDevice ¶
Bases: NamedTuple
Represents a USB storage device.
Attributes:
| Name | Type | Description |
|---|---|---|
device_node | str | Device node path (e.g., /dev/sdb1 on Linux, E: on Windows). |
mount_point | str | Where the device is mounted (e.g., /media/usb or E:). |
label | str | None | Volume label of the device. |
fs_type | str | None | Filesystem type (e.g., vfat, exfat, ntfs). |
size_bytes | int | None | Total size in bytes. |
vendor | str | None | Device vendor name. |
model | str | None | Device model name. |
USBExportTask ¶
Bases: NamedTuple
Represents a file/directory export task.
Attributes:
| Name | Type | Description |
|---|---|---|
source | Path | Source file or directory path. |
dest_name | str | Destination name (relative to USB root). |
is_directory | bool | Whether the source is a directory. |
USBBackend ¶
LinuxUSBBackend ¶
Bases: USBBackend
Linux USB backend using pyudev.
Source code in audex/lib/usb.py
Functions¶
list_devices ¶
list_devices() -> list[USBDevice]
List all currently connected USB storage devices.
Source code in audex/lib/usb.py
start_monitoring async ¶
Start monitoring for USB device events.
Source code in audex/lib/usb.py
stop_monitoring async ¶
Stop monitoring for USB device events.
WindowsUSBBackend ¶
Bases: USBBackend
Windows USB backend using win32api and WMI.
Source code in audex/lib/usb.py
Functions¶
list_devices ¶
list_devices() -> list[USBDevice]
List all currently connected USB storage devices.
Source code in audex/lib/usb.py
start_monitoring async ¶
Start monitoring for USB device events using WMI.
Source code in audex/lib/usb.py
stop_monitoring async ¶
Stop monitoring for USB device events.
Source code in audex/lib/usb.py
USBManager ¶
Bases: LoggingMixin, AsyncLifecycleMixin
Cross-platform USB storage device manager for file export.
This manager monitors USB device connections and provides functionality to export files and directories to connected USB drives. It automatically selects the appropriate backend (Linux/Windows) based on the platform.
Example
# Create manager
manager = USBManager()
# Add export tasks
manager.add_export_task(
source="/var/log/app.log",
dest_name="logs/app.log",
)
manager.add_export_task(
source="/data/recordings",
dest_name="recordings",
is_directory=True,
)
# Start monitoring
await manager.start()
# Export to specific device manually
devices = manager.list_devices()
if devices:
await manager.export(devices[0])
# Stop monitoring
await manager.stop()
Source code in audex/lib/usb.py
Functions¶
add_export_task ¶
add_export_task(source: str | Path | PathLike[str], dest_name: str, *, is_directory: bool = False) -> None
Add a file or directory to export when USB is connected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source | str | Path | PathLike[str] | Source file or directory path. | required |
dest_name | str | Destination name on USB (e.g., "logs/app.log" or "data"). | required |
is_directory | bool | Whether the source is a directory. | False |
Example
Source code in audex/lib/usb.py
remove_export_task ¶
Remove an export task by destination name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dest_name | str | The destination name of the task to remove. | required |
Returns:
| Type | Description |
|---|---|
bool | True if task was removed, False if not found. |
Source code in audex/lib/usb.py
clear_export_tasks ¶
list_devices ¶
list_devices() -> list[USBDevice]
List all currently connected USB storage devices.
Returns:
| Type | Description |
|---|---|
list[USBDevice] | List of connected USB storage devices. |
Example
Source code in audex/lib/usb.py
export async ¶
export(device: USBDevice, *, tasks: list[USBExportTask] | None = None) -> dict[str, bool]
Export files/directories to a USB device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device | USBDevice | Target USB device. | required |
tasks | list[USBExportTask] | None | List of export tasks. If None, uses self.export_tasks. | None |
Returns:
| Type | Description |
|---|---|
dict[str, bool] | Dictionary mapping dest_name to success status. |
Example
Source code in audex/lib/usb.py
start async ¶
Start monitoring for USB device connections.
Will automatically detect when USB devices are connected.
Example
Source code in audex/lib/usb.py
stop async ¶
Stop monitoring for USB device connections.
options: show_root_heading: true show_source: true heading_level: 2 members_order: source show_signature_annotations: true separate_signature: true