From abc07fd08de5e4e76e45084acf570c78424ceffb Mon Sep 17 00:00:00 2001 From: bartool Date: Sun, 21 Sep 2025 21:43:44 +0200 Subject: [PATCH] refactor: replace CameraWidget with dictionary-based config handling in GPhotoCamera --- core/camera/gphoto_camera.py | 108 +++++++++++++++-------------------- 1 file changed, 47 insertions(+), 61 deletions(-) diff --git a/core/camera/gphoto_camera.py b/core/camera/gphoto_camera.py index 30ee416..bb925ac 100644 --- a/core/camera/gphoto_camera.py +++ b/core/camera/gphoto_camera.py @@ -19,25 +19,11 @@ camera_widget_types = { } -@dataclass -class CameraWidget: - id: int - name: str - label: str - type: str - config: object - value: Optional[str] = None - choices: List[str] = field(default_factory=list) - - def __str__(self) -> str: - return f"[{self.id} - {self.type}] '{self.name}' {self.label}\n\tvalue = {self.value} | choices = {self.choices}" - - class GPhotoCamera(BaseCamera): def __init__(self) -> None: super().__init__() self.camera = None - self.widgets: List[CameraWidget] = [] + self.configs: List[dict] = [] def connect(self) -> bool: self.error_msg = None @@ -46,13 +32,13 @@ class GPhotoCamera(BaseCamera): self.camera.init() 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") - + for config in self.configs: + print(config) + config_iso = self.get_config_by_name("iso") + self.set_config_by_id(config_iso['id'], "400") + self.set_config_by_id(config_iso['id'], "100") + self.set_config_by_id(config_iso['id'], "1600") + return True except Exception as e: self.error_msg = f"[GPHOTO2] {e}" @@ -63,7 +49,7 @@ class GPhotoCamera(BaseCamera): if self.camera: self.camera.exit() self.camera = None - self.widgets.clear() + self.configs.clear() def get_frame(self): self.error_msg = None @@ -83,60 +69,60 @@ class GPhotoCamera(BaseCamera): self.error_msg = f"[GPHOTO2] {e}" return (False, None) - def get_setting_by_id(self, id: int) -> CameraWidget: - return next(w for w in self.widgets if w.id == id) + def get_config_by_id(self, id: int): + return next(w for w in self.configs if w['id'] == id) - def get_setting_by_name(self, name: str) -> CameraWidget: - return next(w for w in self.widgets if w.name == name) + def get_config_by_name(self, name: str): + return next(w for w in self.configs if w['name'] == name) - def set_setting(self, widget:CameraWidget, value): - if value not in widget.choices: + def set_config(self, config, value): + if value not in config['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) + config['config'].set_value(value) # type: ignore + if self._save_config(config): + config['value'] = 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.parse_widget( self.camera.get_single_config(widget.name) ) - print(f"old: {widget}") - print(f"new: {new_config}") + def set_config_by_id(self, id: int, value: str): + config = self.get_config_by_id(id) - - 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 - ) + self.set_config(config, value) + + def set_config_by_name(self, name: str): + config = self.get_config_by_name(name) + + self.set_config(config, name) + + def _save_config(self, config): + if not self.camera: + return False + + self.camera.set_single.config(config['name'], config['widget']) + return True + + def parse_config(self, config): + new_config = { + "id": config.get_id(), + "name": config.get_name(), + "label": config.get_label(), + "type": camera_widget_types[config.get_type()], + "widget": config + } try: - temp_widget.value = config.get_value() + new_config["value"] = config.get_value() except gp.GPhoto2Error: pass try: - temp_widget.choices = list(config.get_choices()) + new_config["choices"] = list(config.get_choices()) except gp.GPhoto2Error: pass - return temp_widget - + return new_config + def read_config(self, config): - self.widgets.append(self.parse_widget(config)) + self.configs.append(self.parse_config(config)) for i in range(config.count_children()): child = config.get_child(i)