3838from rich .segment import Segment , Segments
3939from rich .traceback import Traceback
4040
41- from . import Logger , LogGroup , LogVerbosity , actions , events , log , messages
41+ from . import actions , Logger , LogGroup , LogVerbosity , events , log , messages
4242from ._animator import DEFAULT_EASING , Animatable , Animator , EasingFunction
4343from ._ansi_sequences import SYNC_END , SYNC_START
4444from ._callback import invoke
4747from ._filter import LineFilter , Monochrome
4848from ._path import _make_path_object_relative
4949from ._typing import Final , TypeAlias
50+ from .actions import SkipAction
5051from .await_remove import AwaitRemove
5152from .binding import Binding , Bindings
5253from .css .query import NoMatches
@@ -1752,7 +1753,7 @@ async def check_bindings(self, key: str, priority: bool = False) -> bool:
17521753 ):
17531754 binding = bindings .keys .get (key )
17541755 if binding is not None and binding .priority == priority :
1755- if await self .action (binding .action , namespace ) in ( True , None ) :
1756+ if await self .action (binding .action , namespace ):
17561757 return True
17571758 return False
17581759
@@ -1822,30 +1823,41 @@ async def action(
18221823 async def _dispatch_action (
18231824 self , namespace : object , action_name : str , params : Any
18241825 ) -> bool :
1826+ """Dispatch an action to an action method.
1827+
1828+ Args:
1829+ namespace (object): Namespace (object) of action.
1830+ action_name (str): Name of the action.
1831+ params (Any): Action parameters.
1832+
1833+ Returns:
1834+ bool: True if handled, otherwise False.
1835+ """
1836+ _rich_traceback_guard = True
1837+
18251838 log (
18261839 "<action>" ,
18271840 namespace = namespace ,
18281841 action_name = action_name ,
18291842 params = params ,
1830- )
1831- _rich_traceback_guard = True
1832-
1833- public_method_name = f"action_{ action_name } "
1834- private_method_name = f"_{ public_method_name } "
1835-
1836- private_method = getattr (namespace , private_method_name , None )
1837- public_method = getattr (namespace , public_method_name , None )
1843+ )
18381844
1839- if private_method is None and public_method is None :
1845+ try :
1846+ private_method = getattr (namespace , f"_action_{ action_name } " , None )
1847+ if callable (private_method ):
1848+ await invoke (private_method , * params )
1849+ return True
1850+ public_method = getattr (namespace , f"action_{ action_name } " , None )
1851+ if callable (public_method ):
1852+ await invoke (public_method , * params )
1853+ return True
18401854 log (
1841- f"<action> { action_name !r} has no target. Couldn't find methods { public_method_name !r} or { private_method_name !r} "
1855+ f"<action> { action_name !r} has no target."
1856+ f" Could not find methods '_action_{ action_name } ' or 'action_{ action_name } '"
18421857 )
1843-
1844- if callable (private_method ):
1845- return await invoke (private_method , * params )
1846- elif callable (public_method ):
1847- return await invoke (public_method , * params )
1848-
1858+ except SkipAction :
1859+ # The action method raised this to explicitly not handle the action
1860+ log ("<action> {action_name!r} skipped." )
18491861 return False
18501862
18511863 async def _broker_event (
@@ -1856,7 +1868,7 @@ async def _broker_event(
18561868 Args:
18571869 event_name (str): _description_
18581870 event (events.Event): An event object.
1859- default_namespace (object | None): TODO: _description_
1871+ default_namespace (object | None): The default namespace, where one isn't supplied.
18601872
18611873 Returns:
18621874 bool: True if an action was processed.
0 commit comments