Get Started with Ftw Patch Protocols Module

The protocols module defines the structural interfaces of the framework. Instead of rigid inheritance, we use Protocol to implement “Duck Typing”. This ensures that different components can work together as long as they provide the required attributes and methodes.

The LineLike Protocol

The most important protocol is protocols.LineLike. It defines what an object must look like to be processed by the framework’s output systems, such as the base.TerminalColorMixin.

An object satisfies this protocol if it has these three attributes:

  1. _color_map: A dictionary mapping prefix strings to color keys.

  2. prefix: A string (or None) used as the index for the map.

  3. orig_line: The actual text content of the line.

Let’s verify this with a test:

>>> from fitzzftw.patch.protocols import LineLike
>>> from fitzzftw.patch.base import TerminalColorMixin

We can create a simple class that implements the required structure:

>>> class MySimpleLine:
...     def __init__(self, text):
...         self._color_map = {"!": "red"}
...         self.prefix = "!"
...         self.orig_line = text

Since LineLike is decorated with runtime_checkable(), we can use isinstance() to verify the implementation:

>>> simple_line = MySimpleLine("Danger!")
>>> isinstance(simple_line, LineLike)
True

Configuration Protocols

The framework also uses protocols to handle configuration options. This makes it easy to swap different option sources (like CLI arguments or config files).

These protocols are mainly used for static type checking to ensure that functions receive exactly the settings they need.