from annotypes import Anno, add_call_types

from malcolm.core import PartRegistrar
from malcolm.modules.builtin.hooks import AContext
from malcolm.modules.builtin.parts import AMri, APartName, ChildPart

from .. import hooks

with Anno("Whether to raise a ValueError for a bad status"):
    AErrorOnFail = bool


class DirectoryMonitorPart(ChildPart):
    """Part for checking a directoryMonitor Manager is happy"""

    def __init__(
        self, name: APartName, mri: AMri, error_on_fail: AErrorOnFail = True
    ) -> None:
        super().__init__(name, mri, initial_visibility=True)
        self.error_on_fail = error_on_fail

    def setup(self, registrar: PartRegistrar) -> None:
        super().setup(registrar)
        # Hooks
        registrar.hook(hooks.ConfigureHook, self.check_directories)

    @add_call_types
    def check_directories(self, context: AContext) -> None:
        child = context.block_view(self.mri)
        # Perform check
        getattr(child, "managerCheck")()

        # Get status
        status = getattr(child, "managerStatus").value
        if not status:
            hostname = getattr(child, "managerHostname").value
            bad_status_string = (
                f"{self.mri}: bad directory monitor status for server {hostname}"
            )
            self.log.error(bad_status_string)
            if self.error_on_fail:
                raise ValueError(bad_status_string)

@@ -935,48 +935,14 @@
     
-# Perform check%0A        getattr(
 child
-, %22
+.
 mana
@@ -953,35 +953,12 @@
 heck
-%22)
 ()%0A%0A
-        # Get status%0A
     
@@ -970,32 +970,22 @@
 tatus = 
-getattr(
 child
-, %22
+.
 managerS
@@ -989,18 +989,16 @@
 erStatus
-%22)
 .value%0A 
@@ -1046,24 +1046,14 @@
 e = 
-getattr(
 child
-, %22
+.
 mana
@@ -1063,18 +1063,16 @@
 Hostname
-%22)
 .value%0A 

refactor getting child attributes