1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-25 12:39:18 +03:00
Files
libgpiod/bindings/python/gpiod/_internal.py
Vincent Fazio 8c9d5b4fdf bindings: python: make config iteration consistent
Previously, Chip.request_lines and LineRequest.reconfigure_lines
iterated over a config object but did so in subtly different ways.

Create a generator that iterates over a config object and returns line +
settings pairs. Update the aforementioned functions to use the generator
to consolidate and condense code.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
Link: https://lore.kernel.org/r/20251009130516.3729433-2-vfazio@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2025-10-13 17:24:07 +02:00

38 lines
1.1 KiB
Python

# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
from __future__ import annotations
from datetime import timedelta
from select import select
from typing import TYPE_CHECKING, Optional, Union
if TYPE_CHECKING:
from collections.abc import Generator, Iterable
from .line_settings import LineSettings
__all__ = ["poll_fd", "config_iter"]
def poll_fd(fd: int, timeout: Optional[Union[timedelta, float]] = None) -> bool:
sec: Union[float, None]
if isinstance(timeout, timedelta):
sec = timeout.total_seconds()
else:
sec = timeout
readable, _, _ = select([fd], [], [], sec)
return True if fd in readable else False
def config_iter(
config: dict[Union[Iterable[Union[int, str]], int, str], Optional[LineSettings]],
) -> Generator[tuple[Union[int, str], Optional[LineSettings]]]:
for key, settings in config.items():
if isinstance(key, int) or isinstance(key, str):
yield key, settings
else:
for subkey in key:
yield subkey, settings