@@ -7,6 +7,8 @@ Python bindings for libgpiod.
This module wraps the native C API of libgpiod in a set of python classes.
"""
+from typing import Optional, Union
+
from . import (
_ext,
chip,
@@ -83,7 +85,13 @@ def is_gpiochip_device(path: str) -> bool:
return _ext.is_gpiochip_device(path)
-def request_lines(path: str, *args, **kwargs) -> LineRequest:
+def request_lines(
+ path: str,
+ config: dict[Union[tuple[Union[int, str], ...], int, str], Optional[LineSettings]],
+ consumer: Optional[str] = None,
+ event_buffer_size: Optional[int] = None,
+ output_values: Optional[dict[Union[int, str], line.Value]] = None,
+) -> LineRequest:
"""
Open a GPIO chip pointed to by 'path', request lines according to the
configuration arguments, close the chip and return the request object.
@@ -99,4 +107,9 @@ def request_lines(path: str, *args, **kwargs) -> LineRequest:
Returns a new LineRequest object.
"""
with Chip(path) as chip:
- return chip.request_lines(*args, **kwargs)
+ return chip.request_lines(
+ config=config,
+ consumer=consumer,
+ event_buffer_size=event_buffer_size,
+ output_values=output_values,
+ )
@@ -1,9 +1,12 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
+from __future__ import annotations
+
from collections import Counter
from datetime import timedelta
from errno import ENOENT
+from types import TracebackType
from typing import Optional, Union
from . import _ext
@@ -66,14 +69,19 @@ class Chip:
"""
return True if self._chip else False
- def __enter__(self):
+ def __enter__(self) -> Chip:
"""
Controlled execution enter callback.
"""
self._check_closed()
return self
- def __exit__(self, exc_type, exc_value, traceback) -> None:
+ def __exit__(
+ self,
+ exc_type: Optional[type[BaseException]],
+ exc_value: Optional[BaseException],
+ traceback: Optional[TracebackType],
+ ) -> None:
"""
Controlled execution exit callback.
"""
@@ -17,7 +17,7 @@ class ChipInfo:
label: str
num_lines: int
- def __str__(self):
+ def __str__(self) -> str:
return '<ChipInfo name="{}" label="{}" num_lines={}>'.format(
self.name, self.label, self.num_lines
)
@@ -39,7 +39,7 @@ class EdgeEvent:
object.__setattr__(self, "global_seqno", global_seqno)
object.__setattr__(self, "line_seqno", line_seqno)
- def __str__(self):
+ def __str__(self) -> str:
return "<EdgeEvent type={} timestamp_ns={} line_offset={} global_seqno={} line_seqno={}>".format(
self.event_type,
self.timestamp_ns,
@@ -9,7 +9,7 @@ class ChipClosedError(Exception):
Error raised when an already closed chip is used.
"""
- def __init__(self):
+ def __init__(self) -> None:
super().__init__("I/O operation on closed chip")
@@ -18,5 +18,5 @@ class RequestReleasedError(Exception):
Error raised when a released request is used.
"""
- def __init__(self):
+ def __init__(self) -> None:
super().__init__("GPIO lines have been released")
@@ -30,7 +30,7 @@ class InfoEvent:
object.__setattr__(self, "timestamp_ns", timestamp_ns)
object.__setattr__(self, "line_info", line_info)
- def __str__(self):
+ def __str__(self) -> str:
return "<InfoEvent type={} timestamp_ns={} line_info={}>".format(
self.event_type, self.timestamp_ns, self.line_info
)
@@ -15,7 +15,7 @@ class Value(Enum):
INACTIVE = _ext.VALUE_INACTIVE
ACTIVE = _ext.VALUE_ACTIVE
- def __bool__(self):
+ def __bool__(self) -> bool:
return self == self.ACTIVE
@@ -58,7 +58,7 @@ class LineInfo:
self, "debounce_period", timedelta(microseconds=debounce_period_us)
)
- def __str__(self):
+ def __str__(self) -> str:
return '<LineInfo offset={} name="{}" used={} consumer="{}" direction={} active_low={} bias={} drive={} edge_detection={} event_clock={} debounced={} debounce_period={}>'.format(
self.offset,
self.name,
@@ -1,8 +1,11 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
+from __future__ import annotations
+
from collections.abc import Iterable
from datetime import timedelta
+from types import TracebackType
from typing import Optional, Union
from . import _ext
@@ -38,14 +41,19 @@ class LineRequest:
"""
return True if self._req else False
- def __enter__(self):
+ def __enter__(self) -> LineRequest:
"""
Controlled execution enter callback.
"""
self._check_released()
return self
- def __exit__(self, exc_type, exc_value, traceback):
+ def __exit__(
+ self,
+ exc_type: Optional[type[BaseException]],
+ exc_value: Optional[BaseException],
+ traceback: Optional[TracebackType],
+ ) -> None:
"""
Controlled execution exit callback.
"""
@@ -77,7 +85,7 @@ class LineRequest:
"""
return self.get_values([line])[0]
- def _check_line_name(self, line):
+ def _check_line_name(self, line: Union[int, str]) -> bool:
if isinstance(line, str):
if line not in self._name_map:
raise ValueError("unknown line name: {}".format(line))
@@ -212,7 +220,7 @@ class LineRequest:
return self._req.read_edge_events(max_events)
- def __str__(self):
+ def __str__(self) -> str:
"""
Return a user-friendly, human-readable description of this request.
"""
@@ -27,7 +27,7 @@ class LineSettings:
# __repr__ generated by @dataclass uses repr for enum members resulting in
# an unusable representation as those are of the form: <NAME: $value>
- def __repr__(self):
+ def __repr__(self) -> str:
return "gpiod.LineSettings(direction=gpiod.line.{}, edge_detection=gpiod.line.{}, bias=gpiod.line.{}, drive=gpiod.line.{}, active_low={}, debounce_period={}, event_clock=gpiod.line.{}, output_value=gpiod.line.{})".format(
str(self.direction),
str(self.edge_detection),
@@ -39,7 +39,7 @@ class LineSettings:
str(self.output_value),
)
- def __str__(self):
+ def __str__(self) -> str:
return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(
self.direction,
self.edge_detection,
Explicitly define the arguments for `gpiod.request_lines` so there is a clearer linkage with the underlying `Chip.request_lines` interface. Signed-off-by: Vincent Fazio <vfazio@xes-inc.com> --- bindings/python/gpiod/__init__.py | 17 +++++++++++++++-- bindings/python/gpiod/chip.py | 12 ++++++++++-- bindings/python/gpiod/chip_info.py | 2 +- bindings/python/gpiod/edge_event.py | 2 +- bindings/python/gpiod/exception.py | 4 ++-- bindings/python/gpiod/info_event.py | 2 +- bindings/python/gpiod/line.py | 2 +- bindings/python/gpiod/line_info.py | 2 +- bindings/python/gpiod/line_request.py | 16 ++++++++++++---- bindings/python/gpiod/line_settings.py | 4 ++-- 10 files changed, 46 insertions(+), 17 deletions(-)