refactor gphoto_camera

This commit is contained in:
2025-09-21 20:51:37 +02:00
parent 19e2c7977c
commit 35576986c9

View File

@@ -25,7 +25,7 @@ class CameraWidget:
name: str
label: str
type: str
widget: object
config: object
value: Optional[str] = None
choices: List[str] = field(default_factory=list)
@@ -44,9 +44,15 @@ class GPhotoCamera(BaseCamera):
try:
self.camera = gp.Camera() # type: ignore
self.camera.init()
self.read_config()
config = self.camera.get_config()
self.read_config(config)
for widget in self.widgets:
print(widget)
widget = self.get_setting_by_name("iso")
self.set_setting_by_id(widget.id, "400")
self.set_setting_by_id(widget.id, "100")
self.set_setting_by_id(widget.id, "1600")
return True
except Exception as e:
self.error_msg = f"[GPHOTO2] {e}"
@@ -83,44 +89,55 @@ class GPhotoCamera(BaseCamera):
def get_setting_by_name(self, name: str) -> CameraWidget:
return next(w for w in self.widgets if w.name == name)
def set_setting(self, widget:CameraWidget, value):
if value not in widget.choices:
return
widget.config.set_value(value) # type: ignore
self.camera.set_single_config(widget.name, widget.config) # type: ignore
widget.value = value
new_config = self.camera.get_single_config(widget.name)
print(f"old: {widget}")
print(f"new: {new_config}")
def set_setting_by_id(self, id: int, value: str):
widget = self.get_setting_by_id(id)
if value in widget.choices:
widget.widget.set_value(value) # type: ignore
self.camera.set_single_config(widget.name, widget.widget) # type: ignore
def read_config(self):
if not self.camera:
if value not in widget.choices:
return
widget.config.set_value(value) # type: ignore
self.camera.set_single_config(widget.name, widget.config) # type: ignore
# widget.value = value
new_config = self.parse_widget( self.camera.get_single_config(widget.name) )
print(f"old: {widget}")
print(f"new: {new_config}")
self.widgets.clear()
def parse_widget(self, config):
temp_widget = CameraWidget(
id=config.get_id(),
name=config.get_name(),
label=config.get_label(),
type=camera_widget_types[config.get_type()],
config=config
)
def parse_widget(widget):
temp_widget = CameraWidget(
id=widget.get_id(),
name=widget.get_name(),
label=widget.get_label(),
type=camera_widget_types[widget.get_type()],
widget=widget
)
try:
temp_widget.value = config.get_value()
except gp.GPhoto2Error:
pass
try:
temp_widget.value = widget.get_value()
except gp.GPhoto2Error:
pass
try:
temp_widget.choices = list(config.get_choices())
except gp.GPhoto2Error:
pass
try:
temp_widget.choices = list(widget.get_choices())
except gp.GPhoto2Error:
pass
return temp_widget
def read_config(self, config):
self.widgets.append(self.parse_widget(config))
self.widgets.append(temp_widget)
for i in range(widget.count_children()):
parse_widget(widget.get_child(i))
config = self.camera.get_config()
parse_widget(config)
for wid in self.widgets:
print(wid)
for i in range(config.count_children()):
child = config.get_child(i)
self.read_config(child)