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:
_color_map: A dictionary mapping prefix strings to color keys.prefix: A string (orNone) used as the index for the map.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).
protocols.BackupOptions:Handles settings for file backups and extensions.
protocols.WhitespaceOptions:Defines rules for whitespace normalization.
protocols.ArgParsOptions:A master protocol that combines all available application options.
These protocols are mainly used for static type checking to ensure that functions receive exactly the settings they need.