refactor: replace CameraWidget with dictionary-based config handling in GPhotoCamera
This commit is contained in:
@@ -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):
|
class GPhotoCamera(BaseCamera):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.camera = None
|
self.camera = None
|
||||||
self.widgets: List[CameraWidget] = []
|
self.configs: List[dict] = []
|
||||||
|
|
||||||
def connect(self) -> bool:
|
def connect(self) -> bool:
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
@@ -46,13 +32,13 @@ class GPhotoCamera(BaseCamera):
|
|||||||
self.camera.init()
|
self.camera.init()
|
||||||
config = self.camera.get_config()
|
config = self.camera.get_config()
|
||||||
self.read_config(config)
|
self.read_config(config)
|
||||||
for widget in self.widgets:
|
for config in self.configs:
|
||||||
print(widget)
|
print(config)
|
||||||
widget = self.get_setting_by_name("iso")
|
config_iso = self.get_config_by_name("iso")
|
||||||
self.set_setting_by_id(widget.id, "400")
|
self.set_config_by_id(config_iso['id'], "400")
|
||||||
self.set_setting_by_id(widget.id, "100")
|
self.set_config_by_id(config_iso['id'], "100")
|
||||||
self.set_setting_by_id(widget.id, "1600")
|
self.set_config_by_id(config_iso['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}"
|
||||||
@@ -63,7 +49,7 @@ class GPhotoCamera(BaseCamera):
|
|||||||
if self.camera:
|
if self.camera:
|
||||||
self.camera.exit()
|
self.camera.exit()
|
||||||
self.camera = None
|
self.camera = None
|
||||||
self.widgets.clear()
|
self.configs.clear()
|
||||||
|
|
||||||
def get_frame(self):
|
def get_frame(self):
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
@@ -83,60 +69,60 @@ class GPhotoCamera(BaseCamera):
|
|||||||
self.error_msg = f"[GPHOTO2] {e}"
|
self.error_msg = f"[GPHOTO2] {e}"
|
||||||
return (False, None)
|
return (False, None)
|
||||||
|
|
||||||
def get_setting_by_id(self, id: int) -> CameraWidget:
|
def get_config_by_id(self, id: int):
|
||||||
return next(w for w in self.widgets if w.id == id)
|
return next(w for w in self.configs if w['id'] == id)
|
||||||
|
|
||||||
def get_setting_by_name(self, name: str) -> CameraWidget:
|
def get_config_by_name(self, name: str):
|
||||||
return next(w for w in self.widgets if w.name == name)
|
return next(w for w in self.configs if w['name'] == name)
|
||||||
|
|
||||||
def set_setting(self, widget:CameraWidget, value):
|
def set_config(self, config, value):
|
||||||
if value not in widget.choices:
|
if value not in config['choices']:
|
||||||
return
|
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):
|
config['config'].set_value(value) # type: ignore
|
||||||
widget = self.get_setting_by_id(id)
|
if self._save_config(config):
|
||||||
|
config['value'] = value
|
||||||
|
|
||||||
if value not in widget.choices:
|
def set_config_by_id(self, id: int, value: str):
|
||||||
return
|
config = self.get_config_by_id(id)
|
||||||
|
|
||||||
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.set_config(config, value)
|
||||||
def parse_widget(self, config):
|
|
||||||
temp_widget = CameraWidget(
|
def set_config_by_name(self, name: str):
|
||||||
id=config.get_id(),
|
config = self.get_config_by_name(name)
|
||||||
name=config.get_name(),
|
|
||||||
label=config.get_label(),
|
self.set_config(config, name)
|
||||||
type=camera_widget_types[config.get_type()],
|
|
||||||
config=config
|
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:
|
try:
|
||||||
temp_widget.value = config.get_value()
|
new_config["value"] = config.get_value()
|
||||||
except gp.GPhoto2Error:
|
except gp.GPhoto2Error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
temp_widget.choices = list(config.get_choices())
|
new_config["choices"] = list(config.get_choices())
|
||||||
except gp.GPhoto2Error:
|
except gp.GPhoto2Error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return temp_widget
|
return new_config
|
||||||
|
|
||||||
def read_config(self, 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()):
|
for i in range(config.count_children()):
|
||||||
child = config.get_child(i)
|
child = config.get_child(i)
|
||||||
|
|||||||
Reference in New Issue
Block a user