bindings: python: improve LineSettings.__repr__()
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 20 Sep 2024 14:32:03 +0000 (16:32 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 27 Sep 2024 08:09:37 +0000 (10:09 +0200)
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>
bindings/python/gpiod/line_settings.py
bindings/python/tests/tests_line_settings.py

index 41712cc0b372ad765cd114127b717d35cfed3556..5e3219438c2812c449d4da84a97ebc420f2b2352 100644 (file)
@@ -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):
-        return "gpiod.LineSettings(direction={}, edge_detection={}, bias={}, drive={}, active_low={}, debounce_period={}, event_clock={}, output_value={})".format(
+        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),
index c6ca7200bc08e90a7ee8bb9bae4550220b99c1e9..83be3d9a90b9391ffe9cb3fb09c1a4a2042468c6 100644 (file)
@@ -69,7 +69,7 @@ class LineSettingsStringRepresentation(TestCase):
     def test_repr(self):
         self.assertEqual(
             repr(self.settings),
-            "gpiod.LineSettings(direction=Direction.OUTPUT, edge_detection=Edge.NONE, bias=Bias.AS_IS, drive=Drive.OPEN_SOURCE, active_low=True, debounce_period=datetime.timedelta(0), event_clock=Clock.MONOTONIC, output_value=Value.INACTIVE)",
+            "gpiod.LineSettings(direction=gpiod.line.Direction.OUTPUT, edge_detection=gpiod.line.Edge.NONE, bias=gpiod.line.Bias.AS_IS, drive=gpiod.line.Drive.OPEN_SOURCE, active_low=True, debounce_period=datetime.timedelta(0), event_clock=gpiod.line.Clock.MONOTONIC, output_value=gpiod.line.Value.INACTIVE)",
         )
 
         cmp = eval(repr(self.settings))