Lazy formatter registry for Rosettes.
Formatters are loaded on-demand using functools.cache for thread-safe memoization.
Design Philosophy:
Mirrors the lexer registry pattern (seerosettes._registry) for consistency:
- Lazy loading: Formatter modules imported only on first use
- Cached instances: functools.cache ensures one instance per formatter
- Alias support: Multiple names resolve to the same formatter
- Thread-safe: cache is thread-safe; formatters are immutable
Architecture:
_FORMATTER_SPECS: Static mapping of names to (module, class) specs
_ALIAS_TO_NAME: Case-insensitive alias lookup
_get_formatter_by_canonical: Cached formatter instantiation
Available Formatters:
html: HTML output with semantic or Pygments CSS classes
terminal: ANSI escape codes for terminal output
null: No-op formatter for benchmarking/testing
Performance:
- First call: ~0.5ms (module import + instantiation)
- Subsequent calls: ~100ns (dict lookup + cache hit)
Common Mistakes:
formatters = {"html": get_formatter("html")}
formatter = get_formatter("html")
See Also:
rosettes._registry: Lexer registry (same pattern)
rosettes._protocol.Formatter: Protocol that formatters implement
rosettes.formatters: Formatter implementations