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