Skip to content

hooks

Hooks management.

Attributes

SUPPORTED_LANGUAGES = [exts.PYTHON_SCRIPT_FILE_FORMAT, exts.CSHARP_SCRIPT_FILE_FORMAT, exts.VB_SCRIPT_FILE_FORMAT] module-attribute

mlogger = get_logger(__name__) module-attribute

ExtensionEventHook = namedtuple('ExtensionEventHook', ['id', 'name', 'target', 'script', 'syspaths', 'extension_name']) module-attribute

Functions

get_hooks_handler()

Get the hook handler environment variable.

Returns:

Type Description
EventHooks

hook handler

Source code in pyrevitlib/pyrevit/loader/hooks.py
def get_hooks_handler():
    """Get the hook handler environment variable.

    Returns:
        (EventHooks): hook handler
    """
    return envvars.get_pyrevit_env_var(envvars.HOOKSHANDLER_ENVVAR)

set_hooks_handler(handler)

Set the hook handler environment variable.

Parameters:

Name Type Description Default
handler EventHooks

hook handler

required
Source code in pyrevitlib/pyrevit/loader/hooks.py
def set_hooks_handler(handler):
    """Set the hook handler environment variable.

    Args:
        handler (EventHooks): hook handler
    """
    envvars.set_pyrevit_env_var(envvars.HOOKSHANDLER_ENVVAR, handler)

is_valid_hook_script(hook_script)

Check if the given hook script is valid.

Parameters:

Name Type Description Default
hook_script str

hook script path

required

Returns:

Type Description
bool

True if the script is valid, False otherwise

Source code in pyrevitlib/pyrevit/loader/hooks.py
def is_valid_hook_script(hook_script):
    """Check if the given hook script is valid.

    Args:
        hook_script (str): hook script path

    Returns:
        (bool): True if the script is valid, False otherwise
    """
    return op.splitext(op.basename(hook_script))[1] in SUPPORTED_LANGUAGES

get_extension_hooks(extension)

Get the hooks of the given extension.

Parameters:

Name Type Description Default
extension Extension

pyRevit extension

required

Returns:

Type Description
list[ExtensionEventHook]

list of hooks

Source code in pyrevitlib/pyrevit/loader/hooks.py
def get_extension_hooks(extension):
    """Get the hooks of the given extension.

    Args:
        extension (pyrevit.extensions.components.Extension): pyRevit extension

    Returns:
        (list[ExtensionEventHook]): list of hooks
    """
    event_hooks = []
    for hook_script in extension.get_hooks():
        if is_valid_hook_script(hook_script):
            name, target = _get_hook_parts(extension, hook_script)
            if name:
                event_hooks.append(
                    ExtensionEventHook(
                        id=_create_hook_id(extension, hook_script),
                        name=name,
                        target=target,
                        script=hook_script,
                        syspaths=extension.module_paths,
                        extension_name=extension.name,
                    )
                )
    return event_hooks

get_event_hooks()

Get all the event hooks.

Source code in pyrevitlib/pyrevit/loader/hooks.py
def get_event_hooks():
    """Get all the event hooks."""
    hooks_handler = get_hooks_handler()
    return hooks_handler.GetAllEventHooks()

register_hooks(extension)

Register the hooks for the given extension.

Parameters:

Name Type Description Default
extension Extension

pyRevit extension

required
Source code in pyrevitlib/pyrevit/loader/hooks.py
def register_hooks(extension):
    """Register the hooks for the given extension.

    Args:
        extension (pyrevit.extensions.components.Extension): pyRevit extension
    """
    hooks_handler = get_hooks_handler()
    for ext_hook in get_extension_hooks(extension):
        try:
            hooks_handler.RegisterHook(
                uniqueId=ext_hook.id,
                eventName=ext_hook.name,
                eventTarget=ext_hook.target,
                scriptPath=ext_hook.script,
                searchPaths=framework.Array[str](ext_hook.syspaths),
                extensionName=ext_hook.extension_name,
            )
        except Exception as hookEx:
            mlogger.error("Failed registering hook script %s | %s",
                          ext_hook.script, hookEx)

unregister_hooks(extension)

Unregister all hooks for the given extension.

Parameters:

Name Type Description Default
extension Extension

pyRevit extension

required
Source code in pyrevitlib/pyrevit/loader/hooks.py
def unregister_hooks(extension):
    """Unregister all hooks for the given extension.

    Args:
        extension (pyrevit.extensions.components.Extension): pyRevit extension
    """
    hooks_handler = get_hooks_handler()
    for ext_hook in get_extension_hooks(extension):
        hooks_handler.UnRegisterHook(uniqueId=ext_hook.id)

unregister_all_hooks()

Unregister all hooks.

Source code in pyrevitlib/pyrevit/loader/hooks.py
def unregister_all_hooks():
    """Unregister all hooks."""
    hooks_handler = get_hooks_handler()
    hooks_handler.UnRegisterAllHooks(uiApp=HOST_APP.uiapp)

activate()

Activate all event hooks.

Source code in pyrevitlib/pyrevit/loader/hooks.py
def activate():
    """Activate all event hooks."""
    hooks_handler = get_hooks_handler()
    hooks_handler.ActivateEventHooks(uiApp=HOST_APP.uiapp)

deactivate()

Deactivate all event hooks.

Source code in pyrevitlib/pyrevit/loader/hooks.py
def deactivate():
    """Deactivate all event hooks."""
    hooks_handler = get_hooks_handler()
    hooks_handler.DeactivateEventHooks(uiApp=HOST_APP.uiapp)

setup_hooks(session_id=None)

Setup the hooks for the given session.

If no session is specified, use the current one.

Parameters:

Name Type Description Default
session_id str

Session. Defaults to None.

None
Source code in pyrevitlib/pyrevit/loader/hooks.py
def setup_hooks(session_id=None):
    """Setup the hooks for the given session.

    If no session is specified, use the current one.

    Args:
        session_id (str, optional): Session. Defaults to None.
    """
    # make sure session id is availabe
    if not session_id:
        session_id = sessioninfo.get_session_uuid()

    hooks_handler = get_hooks_handler()
    if hooks_handler:
        # deactivate old
        hooks_handler.DeactivateEventHooks(uiApp=HOST_APP.uiapp)
    # setup new
    hooks_handler = EventHooks(session_id)
    set_hooks_handler(hooks_handler)
    unregister_all_hooks()