From 540f8cb62046fcaa5cd743ec402c64f9276c4ffb Mon Sep 17 00:00:00 2001 From: sbkelley Date: Fri, 5 Dec 2025 18:53:57 -0500 Subject: [PATCH] improve readability --- .../interface/molecule/settings_block/pane.py | 40 +++++++++++-------- .../molecule/settings_block/settings_block.py | 6 +-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/obs_ctl/interface/molecule/settings_block/pane.py b/src/obs_ctl/interface/molecule/settings_block/pane.py index 11c8b45..c61d49f 100644 --- a/src/obs_ctl/interface/molecule/settings_block/pane.py +++ b/src/obs_ctl/interface/molecule/settings_block/pane.py @@ -44,7 +44,7 @@ class SettingsType(Enum): return self.value[3] -def pane_factory(cls_name: str, settings_list: list, settings_type_list: list, defaults_list) -> TabPane: +def make_pane_subclass(cls_name: str, settings_list: list, settings_type_list: list, defaults_list) -> TabPane: """Creates a new TabPane subclass with cls_name as its name. settings_list should be a list of names for settings. @@ -59,36 +59,37 @@ def pane_factory(cls_name: str, settings_list: list, settings_type_list: list, d function, which emits a RecordPathChanged message containing a value when the user submits the input.""" row_height = 2 - loop_tuple = list(zip(settings_list, settings_type_list, defaults_list)) - DEFAULT_CSS = f"Grid {{grid-size:2;grid-rows:{row_height};min-height:{len(loop_tuple)*row_height}}}" - def watch_func_factory(watch_id: str): + loop_list = list(zip(settings_list, settings_type_list, defaults_list)) + DEFAULT_CSS = f"Grid {{grid-size:2;grid-rows:{row_height};min-height:{len(loop_list)*row_height}}}" + + def make_watch_reactive(watch_id: str): def watch_func(self, value): input = self.query_exactly_one(watch_id) if type(input) is Input: input.value = str(value) - return watch_func - def on_func_factory(reactive, msg_type, post_msg, _ctrl_id): + + def make_on_control_message(listener_message, listener_id, reactive, post_message): def on_func(self, event): self.set_reactive(reactive, event.value) - self.post_message(post_msg(self, event.value)) + self.post_message(post_message(self, event.value)) + return on(listener_message, listener_id)(on_func) - return on(msg_type, _ctrl_id)(on_func) - def compose_factory(): + def make_compose(): def output_func(self): with ScrollableContainer(): with Grid(): - for setting, control_and_type, setting_default in loop_tuple: + for setting, control_and_type, setting_default in loop_list: ctrl_id = f"{setting_type.widget.func.__name__.lower()}_{setting}" yield Label(setting) yield control_and_type.widget(value=control_and_type.expected_type(setting_default), id=ctrl_id) - return output_func + new_dict = dict() - for setting_name, setting_type, setting_default in loop_tuple: + for setting_name, setting_type, setting_default in loop_list: setting_name: str = setting_name setting_type: SettingsType = setting_type setting_default: Any = setting_default @@ -98,20 +99,25 @@ def pane_factory(cls_name: str, settings_list: list, settings_type_list: list, d on_setting_changed_name: str = f"on_{setting_name}_changed" # For each desired setting (e.g. 'record_path'): - # Set up a reactive named record_path + # Set up a record_path reactive new_dict[setting_name] = reactive(setting_type.type(setting_default)) # Set up a watch_record_path function - new_dict[watch_setting_name] = watch_func_factory(f"#{control_setting_id}") - # Set up a RecordPathChanged for when user interacts with control + new_dict[watch_setting_name] = make_watch_reactive(f"#{control_setting_id}") + # Set up a RecordPathChanged message for when user interacts with control # (see SettingTypes enum) new_dict[SettingChanged_name] = type(SettingChanged_name, (SettingsMSGBase, ), {}) # Set up on_record_path_changed function to update record_path # and post RecordPathChanged - new_dict[on_setting_changed_name] = on_func_factory(new_dict[setting_name], setting_type.msg, new_dict[SettingChanged_name], f"#{control_setting_id}") # on_record_path_changed + new_dict[on_setting_changed_name] = make_on_control_message( + listener_message=setting_type.msg, + listener_id=f"#{control_setting_id}", + reactive=new_dict[setting_name], + post_message=new_dict[SettingChanged_name], + ) # Set up the default CSS new_dict["DEFAULT_CSS"] = DEFAULT_CSS # Set up compose() - new_dict["compose"] = compose_factory() # Adds all the Inputs + new_dict["compose"] = make_compose() # Construct the actual class and return it new_pane = type(cls_name, (TabPane,), new_dict) diff --git a/src/obs_ctl/interface/molecule/settings_block/settings_block.py b/src/obs_ctl/interface/molecule/settings_block/settings_block.py index 9077afd..f4e6c3b 100644 --- a/src/obs_ctl/interface/molecule/settings_block/settings_block.py +++ b/src/obs_ctl/interface/molecule/settings_block/settings_block.py @@ -7,21 +7,21 @@ from . import pane logger = getLogger(__name__) -OBSSettingsPane = pane.pane_factory( +OBSSettingsPane = pane.make_pane_subclass( "OBSSettingsPane", ["record_path"], [pane.SettingsType.STRING, pane.SettingsType.STRING], [""] ) -OBSWebSocketPane = pane.pane_factory( +OBSWebSocketPane = pane.make_pane_subclass( "OBSWebSocketPane", ["host", "port", "password", "timeout"], [pane.SettingsType.STRING, pane.SettingsType.INT, pane.SettingsType.STRING, pane.SettingsType.FLOAT], ["localhost", 4455, "", 1.0] ) -OBSCTLPane = pane.pane_factory( +OBSCTLPane = pane.make_pane_subclass( "OBSCTLPane", ["nothing"], [pane.SettingsType.STRING], -- 2.47.3