"""Core functionality for Muse."""from__future__importannotationsfromtypingimportTYPE_CHECKING,Optionalfromih_muse.ih_museimportPyMuse,TimestampResolutionfromih_muse.protoimportMetricPayloadifTYPE_CHECKING:fromih_muse.configimportConfigfromih_muse.protoimportMetricQuery
[docs]classMuse:"""The main class for interacting with the Muse system in Python. :param Config config: The configuration object for initializing Muse. ```python # Example usage: from ih_muse import Muse, Config config = Config(...) muse = Muse(config) await muse.initialize(timeout=5.0) ``` """_muse:PyMusedef__init__(self,config:Config)->None:"""Initialize the Muse instance."""self._muse=PyMuse(config._config)
[docs]asyncdefinitialize(self,timeout:Optional[float]=None)->None:"""Initialize the Muse client and starts background tasks. :param Optional[float] timeout: Optional timeout in seconds for the initialization process. :raises MuseInitializationTimeoutError: If initialization times out. """awaitself._muse.initialize(timeout)
[docs]@classmethodasyncdefcreate(cls:type[Muse],config:Config,timeout:Optional[float]=None)->Muse:"""Create and initialize a Muse instance. :param Config config: The configuration object. :param Optional[float] timeout: Optional timeout in seconds for initialization. :return: An initialized Muse instance. """instance=cls(config)awaitinstance.initialize(timeout)returninstance
@propertydefis_initialized(self)->bool:"""Check whether the Muse client is initialized. :return: `True` if initialized, `False` otherwise. """returnself._muse.is_initialized@propertydeffinest_resolution(self)->TimestampResolution:"""Get the finest resolution stored by the Poet. :return: The current TimestampResolution stored in the poet. """returnself._muse.finest_resolution
[docs]defget_remote_element_id(self,local_elem_id:str)->Optional[int]:"""Retrieve the remote Element ID associated with a local Element ID. :param str local_elem_id: The local Element ID to query. :return: The corresponding remote Element ID, or `None` if not registered. """returnself._muse.get_remote_element_id(local_elem_id)
[docs]asyncdefregister_element(self,kind_code:str,name:str,metadata:dict[str,str],parent_id:Optional[str]=None,)->str:"""Register a new element with the Muse system. :param str kind_code: The kind code of the element. :param str name: The name of the element. :param dict[str, str] metadata: Metadata associated with the element. :param Optional[int] parent_id: The parent element ID, if any. :return: The local element ID assigned to the registered element. :raises MuseError: If registration fails. """local_elem_id=awaitself._muse.register_element(kind_code,name,metadata,parent_id,)returnlocal_elem_id
[docs]asyncdefsend_metric(self,local_elem_id:str,metric_code:str,value:float,)->None:"""Send a metric value associated with an element. :param int local_elem_id: The local ID of the element. :param str metric_code: The code identifying the metric. :param float value: The value of the metric to send. :raises MuseError: If sending the metric fails. """awaitself._muse.send_metric(local_elem_id,metric_code,value,)
[docs]asyncdefget_metrics(self,query:MetricQuery)->list[MetricPayload]:"""Retrieve metrics based on a query. :param MetricQuery query: The query specifying the criteria for retrieving metrics. :return: A list of MetricPayload matching the query. :raises MuseError: If retrieving the metrics fails. """raw_metrics=awaitself._muse.get_metrics(query._metric_query)return[MetricPayload.from_py_metric_payload(metric)formetricinraw_metrics]
[docs]asyncdefreplay(self,replay_path:str)->None:"""Replays events from a recording file. :param str replay_path: The file path to the recording. :raises MuseError: If replaying the events fails. """awaitself._muse.replay(replay_path)