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/line_settings.py
Bartosz Golaszewski f496cb942e bindings: python: improve LineSettings.__repr__()
Currently, for the output of LineSettings.__repr__() to be eval()able,
the user must have pulled all the relevant definitions from gpiod.line
within the scope where it is used. Modify the output so that all the user
needs is `import gpiod`.

Reported-by: Vincent Fazio <vfazio@gmail.com>
Link: https://lore.kernel.org/r/20240920143203.6377-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-09-27 10:14:04 +02:00

65 lines
2.2 KiB
Python

# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
from . import _ext
from dataclasses import dataclass
from datetime import timedelta
from gpiod.line import Direction, Bias, Drive, Edge, Clock, Value
__all__ = "LineSettings"
@dataclass(repr=False)
class LineSettings:
"""
Stores a set of line properties.
"""
direction: Direction = Direction.AS_IS
edge_detection: Edge = Edge.NONE
bias: Bias = Bias.AS_IS
drive: Drive = Drive.PUSH_PULL
active_low: bool = False
debounce_period: timedelta = timedelta()
event_clock: Clock = Clock.MONOTONIC
output_value: Value = Value.INACTIVE
# __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):
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),
str(self.bias),
str(self.drive),
self.active_low,
repr(self.debounce_period),
str(self.event_clock),
str(self.output_value),
)
def __str__(self):
return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(
self.direction,
self.edge_detection,
self.bias,
self.drive,
self.active_low,
self.debounce_period,
self.event_clock,
self.output_value,
)
def _line_settings_to_ext(settings: LineSettings) -> _ext.LineSettings:
return _ext.LineSettings(
direction=settings.direction.value,
edge_detection=settings.edge_detection.value,
bias=settings.bias.value,
drive=settings.drive.value,
active_low=settings.active_low,
debounce_period=int(settings.debounce_period.total_seconds() * 1000000),
event_clock=settings.event_clock.value,
output_value=settings.output_value.value,
)