Skip to content

file

dev_tool.event.handler.file

log = logging.getLogger(__name__) module-attribute

FileEventHandler

A handler class for file system events.

This class manages callbacks for file changes and executes them when corresponding events are received.

The constructor for the FileEventHandler class.

Initializes empty callback dictionaries for file events.

Source code in dev_tool/event/handler/file.py
def __init__(self) -> None:
    """
    The constructor for the FileEventHandler class.

    Initializes empty callback dictionaries for file events.
    """

    self._callbacks: dict[str, list[Callable[[Path], None]]] = {}

handle

A method that handles file system events.

Parameters:

  • event (Event) –

    The event object to handle.

Source code in dev_tool/event/handler/file.py
def handle(self, event: Event) -> None:
    """
    A method that handles file system events.

    :param event: The event object to handle.
    """

    path_value = event.data.get('path', '')

    if not path_value or not isinstance(path_value, str):
        return

    path_str: str = path_value
    path = Path(path_str)

    callbacks = self._callbacks.get(path_str, [])

    for callback in callbacks:
        try:
            callback(path)
        except Exception:
            log.exception(f'File event callback failed for {path_str}')

register_callback

A method that registers a callback for file changes.

Parameters:

  • path (Path) –

    The file path to watch.

  • callback (Callable[[Path], None]) –

    The callback function to execute.

Source code in dev_tool/event/handler/file.py
def register_callback(self, path: Path, callback: Callable[[Path], None]) -> None:
    """
    A method that registers a callback for file changes.

    :param path: The file path to watch.
    :param callback: The callback function to execute.
    """

    path_str = str(path.absolute())

    if path_str not in self._callbacks:
        self._callbacks[path_str] = []

    if callback not in self._callbacks[path_str]:
        self._callbacks[path_str].append(callback)

unregister_callback

A method that unregisters a callback for file changes.

Parameters:

  • path (Path) –

    The file path to stop watching.

  • callback (Callable[[Path], None] | None, default: None ) –

    The specific callback to remove, or None to remove all.

Source code in dev_tool/event/handler/file.py
def unregister_callback(self, path: Path, callback: Callable[[Path], None] | None = None) -> None:
    """
    A method that unregisters a callback for file changes.

    :param path: The file path to stop watching.
    :param callback: The specific callback to remove, or None to remove all.
    """

    path_str = str(path.absolute())

    if path_str not in self._callbacks:
        return

    if callback is None:
        del self._callbacks[path_str]
        return

    if callback in self._callbacks[path_str]:
        self._callbacks[path_str].remove(callback)

        if not self._callbacks[path_str]:
            del self._callbacks[path_str]

get_event_type staticmethod

A method that returns the event types this handler processes.

Returns:

Source code in dev_tool/event/handler/file.py
@staticmethod
def get_event_type() -> list[AppEventType]:
    """
    A method that returns the event types this handler processes.

    :return: A list of event types.
    """

    return [
        AppEventType.FILE_CHANGED,
        AppEventType.FILE_CREATED,
        AppEventType.FILE_DELETED,
    ]