Skip to content

Patch module

Examples

Patch Rich's Color.parse at runtime to add CSS name and 3-digit hex support:

from rich_color_ext.patch import install, uninstall, is_installed

install()
assert is_installed()

# ... use Rich with CSS colours here ...

uninstall()
assert not is_installed()

API reference

rich-color-ext.patch.py

Monkey-patching support for rich.color.Color.parse.

install()

Install the monkey patch. After this call, rich.color.Color.parse will support 3-digit hex and CSS colour names. Safe to call multiple times.

Source code in src/rich_color_ext/patch.py
def install() -> None:
    """
    Install the monkey patch. After this call, rich.color.Color.parse will
    support 3-digit hex and CSS colour names. Safe to call multiple times.
    """
    global INSTALLED  # pylint: disable=global-statement
    if INSTALLED:
        return
    setattr(Color, "parse", _patched_parse)
    INSTALLED = True

is_installed()

Return True if the monkey patch is currently installed.

Source code in src/rich_color_ext/patch.py
def is_installed() -> bool:
    """
    Return True if the monkey patch is currently installed.
    """
    return INSTALLED

uninstall()

Uninstall the monkey patch, restoring the original rich.color.Color.parse. Safe to call multiple times.

Source code in src/rich_color_ext/patch.py
def uninstall() -> None:
    """
    Uninstall the monkey patch, restoring the original rich.color.Color.parse.
    Safe to call multiple times.
    """
    global INSTALLED  # pylint: disable=global-statement
    if not INSTALLED:
        return
    setattr(Color, "parse", _ORIGINAL_PARSE)
    INSTALLED = False