Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video lazy controls fixes and tests #29

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions linuxpy/video/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,20 +1059,27 @@ def __getattr__(self, key):
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{key}'")

def __setattr__(self, key, value):
self._init_if_needed()
self[key] = value

def __delattr__(self, key):
self._init_if_needed()
try:
del self[key]
except KeyError as error:
raise AttributeError(key) from error

def __missing__(self, key):
self._init_if_needed()
for v in self.values():
if isinstance(v, BaseControl) and (v.config_name == key):
return v
raise KeyError(key)

def values(self):
self._init_if_needed()
return super().values()

def used_classes(self):
class_map = {v.control_class.id: v.control_class for v in self.values() if isinstance(v, BaseControl)}
return list(class_map.values())
Expand Down
58 changes: 56 additions & 2 deletions tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Device,
Memory,
PixelFormat,
Priority,
VideoCapture,
VideoOutput,
iter_devices,
Expand Down Expand Up @@ -702,12 +703,14 @@ def _():
def _():
with Device(VIVID_TEST_DEVICES[0]) as device:
controls = device.controls

# Brightness
assert controls.brightness is controls["brightness"]
current_value = controls.brightness.value
assert 0 <= current_value < 250
try:
controls.brightness.value = not current_value
assert controls.brightness.value == (not current_value)
controls.brightness.value = 100
assert controls.brightness.value == 100
finally:
controls.brightness.value = current_value

Expand All @@ -719,8 +722,59 @@ def _():
assert controls.brightness in controls.with_class(ControlClass.USER)

assert "<IntegerControl brightness" in repr(controls.brightness)

# String
assert controls.string is controls["string"]
current_value = controls.string.value
try:
controls.string.value = "hell"
assert controls.string.value == "hell"
finally:
controls.string.value = current_value

assert "<CompoundControl string flags=has_payload>" in repr(controls.string)

# 2 element array
assert controls.s32_2_element_array is controls["s32_2_element_array"]
current_value = controls.s32_2_element_array.value
try:
controls.s32_2_element_array.value = [5, -5]
assert controls.s32_2_element_array.value[:] == [5, -5]
finally:
controls.s32_2_element_array.value = current_value

assert "<CompoundControl s32_2_element_array flags=has_payload>" in repr(controls.s32_2_element_array)

# Unknown
with raises(KeyError):
_ = list(controls.with_class("unknown class"))

with raises(ValueError):
_ = list(controls.with_class(55))


@skip(when=not is_vivid_prepared(), reason="vivid is not prepared")
@skip(when=not is_vivid_installed(), reason="vivid is not installed")
@test("info with vivid")
def _():
with Device(VIVID_TEST_DEVICES[0]) as device:
text = repr(device.info)
assert "driver = " in text

assert len(device.info.frame_sizes) > 10
assert len(device.info.formats) > 10

inputs = device.info.inputs
assert len(inputs) > 0
active_input = device.get_input()
assert active_input in {inp.index for inp in inputs}
try:
device.set_input(inputs[-1].index)
assert device.get_input() == inputs[-1].index
finally:
device.set_input(active_input)

outputs = device.info.outputs
assert len(outputs) >= 0

assert isinstance(device.get_priority(), Priority)