o |nh@sdZddlmZddlZddlZddlmZmZmZm Z m Z m Z ddl m Z ddlmZe r4ddlmZeeZGd d d eZdS) zCement core hooks module.) annotationsN)AnyCallableDictList Generator TYPE_CHECKING)exc)minimal_logger)Appc@sNeZdZdZd!ddZd"d d Zd#d dZd$ddZd%d&ddZd'ddZ d S)( HookManagerz Manages the hook system to define, get, run, etc hooks within the the Cement Framework and applications Built on Cement (tm). appr returnNonecCs||_i|_dS)N)r __hooks__)selfrrH/usr/local/bin/dhwp/env/lib/python3.10/site-packages/cement/core/hook.py__init__s zHookManager.__init__ List[str]cCst|jS)zu List all defined hooks. Returns: hooks (list): List of registered hook labels. )listrkeys)rrrrrszHookManager.listnamestrcCs<td|d||jvrtd|dg|j|<dS)a Define a hook namespace that the application and plugins can register hooks in. Args: name (str): The name of the hook, stored as hooks['name'] Raises: cement.core.exc.FrameworkError: If the hook name is already defined Example: .. code-block:: python from cement import App with App('myapp') as app: app.hook.define('my_hook_name') zdefining hook '' Hook name 'z' already defined!N)LOGdebugrr FrameworkError)rrrrrdefine$s zHookManager.define hook_nameboolcCs||jvrdSdS)a Test whether a hook name is defined. Args: hook_name (str): The name of the hook. I.e. ``my_hook_does_awesome_things``. Returns: bool: ``True`` if the hook is defined, ``False`` otherwise. Example: .. code-block:: python from cement import App with App('myapp') as app: app.hook.defined('some_hook_name'): # do something about it pass TF)r)rr!rrrdefined?s zHookManager.definedrfuncrweightintcCsX||jvrtd|ddStd|j|j|f|j|t||j|fdS)a Register a function to a hook. The function will be called, in order of weight, when the hook is run. Args: name (str): The name of the hook to register too. I.e. ``pre_setup``, ``post_run``, etc. func (function): The function to register to the hook. This is an *un-instantiated*, non-instance method, simple function. Keywork Args: weight (int): The weight in which to order the hook function. Returns: bool: ``True`` if hook is registered successfully, ``False`` otherwise. Example: .. code-block:: python from cement import App def my_hook_func(app): # do something with app? return True with App('myapp') as app: app.hook.define('my_hook_name') app.hook.register('my_hook_name', my_hook_func) z hook name 'z' is not defined! ignoring...Fz.registering hook '%s' from %s into hooks['%s']T)rrr__name__ __module__appendr&)rrr$r%rrrregister[s  zHookManager.registerargsrkwargsrc os||jvrtd|d|j|jtdd|j|D]0}td|d|dd|dj|d|i|}t |t j rM|D]}|VqFq |Vq d S) a Run all defined hooks in the namespace. Args: name (str): The name of the hook function. args (tuple): Additional arguments to be passed to the hook functions. kwargs (dict): Additional keyword arguments to be passed to the hook functions. Yields: The result of each hook function executed. Raises: cement.core.exc.FrameworkError: If the hook ``name`` is not defined Example: .. code-block:: python from cement import App def my_hook_func(app): # do something with app? return True with App('myapp') as app: app.hook.define('my_hook_name') app.hook.register('my_hook_name', my_hook_func) for res in app.hook.run('my_hook_name', app): # do something with the result? pass rz' is not defined!r)keyzrunning hook 'z' (r z) from N) rr rsortoperator itemgetterrrr( isinstancetypes GeneratorType)rrr+r,hookres_resrrrruns $& zHookManager.runN)rr rr)rr)rrrr)r!rrr")r)rrr$rr%r&rr")rrr+rr,rrr) r'r( __qualname____doc__rrr r#r*r7rrrrr s    +r )r9 __future__rr/r2typingrrrrrrcorer utils.miscr core.foundationr r'robjectr rrrrs