5from textual.widgets
import Static
6from textual.message
import Message
8from daqconf.cider.data_structures.structured_configuration
import StructuredConfiguration
9from daqconf.cider.data_structures.configuration_handler
import ConfigurationHandler
10from daqconf.cider.data_structures.selection_interface_factory
import SelectionInterfaceFactory
11from daqconf.cider.data_structures.selection_interface
import SelectionInterface
14 """Controller widget for the full configuration. In principal this is
15 where all communication with the configuration is actually done!
17 BINDINGS = [(
"ctrl+s",
"save_configuration",
"Save Configuration")]
19 _handler: StructuredConfiguration |
None =
None
20 _selection_interfaces: Dict[str, SelectionInterface] = {}
21 _current_selected_object =
None
24 self.
_logger = self.app.query_one(
"RichLogWError")
28 """Swap currently selected DAL object via its unique ID and class
31 new_id -- UID of new DAL
32 new_class -- Class of DAL
39 """Get current selected dal
45 """Set the current dal via a pre-existing dal
48 new_dal -- New dal object
55 """Update an attribute of the currently loaded dal object.
56 NOTE This does not update the database file itself
59 attr_name -- Attribute to update
60 update_value -- New value for attribute
63 self.
_logger.write_error(
"No handler has been setup")
69 except Exception
as e:
71 self.
_logger.write_error(f
"\nCould not update [yellow]{attr_name}[/yellow] to [yellow]{update_value}[/yellow] for {self.generate_rich_string(self._current_selected_object)}")
74 """Set new handler object by file name
77 file_name -- New database to load
80 self.
_handler = StructuredConfiguration(file_name)
81 except Exception
as e:
86 def handler(self)->StructuredConfiguration | None:
87 """Return the configuration handler
90 ConfigurationHandler instance
95 def handler(self, new_handler: StructuredConfiguration):
99 new_handler -- New handler object
105 """Return current configuration
108 Access the raw configuration
112 return self.
_handler.configuration_handler.configuration
116 """Generate a rich string for a DAL object, shouldn't live here but :shrug:"""
118 return f
"[grey]{getattr(dal_obj, 'id')}[/grey]@[grey]{dal_obj.className()}[/grey] [bold red]DISABLED[/bold red]"
120 return f
"[yellow]{getattr(dal_obj, 'id')}[/yellow]@[green]{dal_obj.className()}[/green]"
124 """get all interface objects. The interface defines an "ordering" for objects
135 SelectionInterfaceFactory.get_interface(interface_label, self.
_handler)
139 """Save configuration with a message to database
141 self.
_handler.configuration_handler.commit(message)
142 self.
_logger.write(f
"[green]Saved configuration with message:[/green] [red]{message}[/red]")
145 """Rename the currently selected object [NOT TESTED]
151 """Add new object to configuration
153 self.
_handler.configuration_handler.add_new_conf_obj(class_id, uid)
154 self.
_logger.write(f
"[green]Added new configuration object[/green] [red]{class_id}[/red]@[yellow]{uid}[/yellow]")
157 """Destroy object in configuration
159 self.
_handler.configuration_handler.destroy_conf_obj(class_id, uid)
160 self.
_logger.write(f
"[green]Destroyed configuration object[/green] [red]{class_id}[/red]@[yellow]{uid}[/yellow]")
168 """Check if current object is capable of being disabled
171 bool -- True if object can be disabled
174 self.
_logger.write_error(
"No object selected")
178 self.
_logger.write_error(f
"Cannot disable {self.generate_rich_string(self._current_selected_object)} must inherit from [red]Component[/red]!")
185 """Disable current object in configuration
188 self.
_logger.write(
"\n[red]=============================")
191 for session, toggle_enable
in selection_menu:
192 session_disabled_elements = session.disabled
203 self.
_logger.write(f
"Enabling {self.generate_rich_string(self._current_selected_object)} in {self.generate_rich_string(session)}")
207 self.
_logger.write(f
"Disabling {self.generate_rich_string(self._current_selected_object)} in {self.generate_rich_string(session)}")
212 session.disabled = session_disabled_elements
213 self.
_handler.configuration_handler.configuration.update_dal(session)
214 self.
_logger.write(
"[red]=============================\n")
218 return [top_object
for top_object
in self.
_handler.relational_graph.top_level_nodes\
219 if top_object.className() ==
"Session"]
222 """Check if object is disabled in any session
227 """Raise error if no handler is setup"""
229 self.
_logger.write_error(
"Handler not initialised, this could be")
233 """Notify if/when configuration is changed"""
242 relationship_name, updated_value, append)
256 if dal_to_remove
is None:
257 raise Exception(
"Relationship is already emptied")
261 relationships = relationship_dict[relationship_name]
263 if not relationship_dict[
'rel_info'][
'multivalue']:
269 dal_idx = relationships.index(dal_to_remove)
270 relationships.pop(dal_idx)
272 except Exception
as e:
277 return self.
_handler.configuration_handler.get_conf_objects_class(dal_class)
280 return list(self.
_handler.configuration_handler.get_all_conf_classes().keys())
290 for rel
in relations:
291 if list(rel.keys())[0] != relation_name:
297 raise RuntimeError(f
"Error cannot find relation: {relation_name} in {self.generate_rich_string(self._current_selected_object)}")