mirror of
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git
synced 2026-10-24 00:48:53 +03:00
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>
This commit is contained in:
committed by
Bartosz Golaszewski
parent
2838c7604a
commit
8c9d5b4fdf
@@ -1,11 +1,18 @@
|
||||
# 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 Optional, Union
|
||||
from typing import TYPE_CHECKING, Optional, Union
|
||||
|
||||
__all__ = ["poll_fd"]
|
||||
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:
|
||||
@@ -17,3 +24,14 @@ def poll_fd(fd: int, timeout: Optional[Union[timedelta, float]] = None) -> bool:
|
||||
|
||||
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
|
||||
|
||||
@@ -8,7 +8,7 @@ from errno import ENOENT
|
||||
from typing import TYPE_CHECKING, Optional, Union, cast
|
||||
|
||||
from . import _ext
|
||||
from ._internal import poll_fd
|
||||
from ._internal import config_iter, poll_fd
|
||||
from .exception import ChipClosedError
|
||||
from .line import Value
|
||||
from .line_request import LineRequest
|
||||
@@ -305,28 +305,24 @@ class Chip:
|
||||
offset_map = dict()
|
||||
global_output_values = list()
|
||||
|
||||
for lines, settings in config.items():
|
||||
for line, settings in config_iter(config):
|
||||
offsets = list()
|
||||
|
||||
if isinstance(lines, int) or isinstance(lines, str):
|
||||
lines = (lines,)
|
||||
offset = self.line_offset_from_id(line)
|
||||
offsets.append(offset)
|
||||
|
||||
for line in lines:
|
||||
offset = self.line_offset_from_id(line)
|
||||
offsets.append(offset)
|
||||
# If there's a global output value for this offset, store it in the
|
||||
# list for later.
|
||||
if mapped_output_values:
|
||||
global_output_values.append(
|
||||
mapped_output_values[offset]
|
||||
if offset in mapped_output_values
|
||||
else Value.INACTIVE
|
||||
)
|
||||
|
||||
# If there's a global output value for this offset, store it in the
|
||||
# list for later.
|
||||
if mapped_output_values:
|
||||
global_output_values.append(
|
||||
mapped_output_values[offset]
|
||||
if offset in mapped_output_values
|
||||
else Value.INACTIVE
|
||||
)
|
||||
|
||||
if isinstance(line, str):
|
||||
name_map[line] = offset
|
||||
offset_map[offset] = line
|
||||
if isinstance(line, str):
|
||||
name_map[line] = offset
|
||||
offset_map[offset] = line
|
||||
|
||||
line_cfg.add_line_settings(
|
||||
offsets, _line_settings_to_ext(settings or LineSettings())
|
||||
|
||||
@@ -6,7 +6,7 @@ from __future__ import annotations
|
||||
from typing import TYPE_CHECKING, Optional, Union, cast
|
||||
|
||||
from . import _ext
|
||||
from ._internal import poll_fd
|
||||
from ._internal import config_iter, poll_fd
|
||||
from .exception import RequestReleasedError
|
||||
from .line_settings import LineSettings, _line_settings_to_ext
|
||||
|
||||
@@ -174,13 +174,9 @@ class LineRequest:
|
||||
line_cfg = _ext.LineConfig()
|
||||
line_settings = {}
|
||||
|
||||
for lines, settings in config.items():
|
||||
if isinstance(lines, int) or isinstance(lines, str):
|
||||
lines = [lines]
|
||||
|
||||
for line in lines:
|
||||
offset = self._line_to_offset(line)
|
||||
line_settings[offset] = settings
|
||||
for line, settings in config_iter(config):
|
||||
offset = self._line_to_offset(line)
|
||||
line_settings[offset] = settings
|
||||
|
||||
for offset in self.offsets:
|
||||
settings = line_settings.get(offset) or LineSettings()
|
||||
|
||||
Reference in New Issue
Block a user