Re-upload: CI infrastructure issue resolved, all tests verified passing
This commit is contained in:
132
envschema/formatters.py
Normal file
132
envschema/formatters.py
Normal file
@@ -0,0 +1,132 @@
|
||||
"""Output formatters for validation results."""
|
||||
|
||||
import json
|
||||
|
||||
from envschema.core import ValidationResult
|
||||
|
||||
|
||||
class TextFormatter:
|
||||
"""Formatter for human-readable text output."""
|
||||
|
||||
RED = "\033[91m"
|
||||
GREEN = "\033[92m"
|
||||
YELLOW = "\033[93m"
|
||||
RESET = "\033[0m"
|
||||
BOLD = "\033[1m"
|
||||
|
||||
@staticmethod
|
||||
def format(result: ValidationResult, color: bool = True, ci_mode: bool = False) -> str:
|
||||
"""Format validation result as text.
|
||||
|
||||
Args:
|
||||
result: Validation result to format.
|
||||
color: Whether to use ANSI colors.
|
||||
ci_mode: CI mode (cleaner output without special chars).
|
||||
|
||||
Returns:
|
||||
Formatted text.
|
||||
"""
|
||||
lines = []
|
||||
|
||||
if ci_mode:
|
||||
return TextFormatter._format_ci(result)
|
||||
|
||||
if result.is_valid:
|
||||
lines.append(f"{TextFormatter._color(TextFormatter.GREEN, '✓', color)} Validation passed")
|
||||
if result.warnings:
|
||||
lines.append("")
|
||||
lines.append("Warnings:")
|
||||
for warning in result.warnings:
|
||||
lines.append(f" {TextFormatter._color(TextFormatter.YELLOW, '⚠', color)} {warning}")
|
||||
else:
|
||||
lines.append(f"{TextFormatter._color(TextFormatter.RED, '✗', color)} Validation failed")
|
||||
lines.append("")
|
||||
|
||||
if result.missing_required:
|
||||
lines.append("Missing required variables:")
|
||||
for var in result.missing_required:
|
||||
lines.append(f" {TextFormatter._color(TextFormatter.RED, '✗', color)} {var}")
|
||||
lines.append("")
|
||||
|
||||
if result.type_errors:
|
||||
lines.append("Type errors:")
|
||||
for error in result.type_errors:
|
||||
lines.append(f" {TextFormatter._color(TextFormatter.RED, '✗', color)} {error.var_name}: {error.message}")
|
||||
lines.append("")
|
||||
|
||||
if result.pattern_errors:
|
||||
lines.append("Pattern errors:")
|
||||
for error in result.pattern_errors:
|
||||
lines.append(f" {TextFormatter._color(TextFormatter.RED, '✗', color)} {error.var_name}: {error.message}")
|
||||
lines.append("")
|
||||
|
||||
if result.warnings:
|
||||
lines.append("Warnings:")
|
||||
for warning in result.warnings:
|
||||
lines.append(f" {TextFormatter._color(TextFormatter.YELLOW, '⚠', color)} {warning}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _format_ci(result: ValidationResult) -> str:
|
||||
"""Format result for CI mode."""
|
||||
lines = []
|
||||
|
||||
if result.is_valid:
|
||||
lines.append("Validation passed")
|
||||
else:
|
||||
lines.append("Validation failed")
|
||||
|
||||
if result.missing_required:
|
||||
lines.append("Missing required variables: " + ", ".join(result.missing_required))
|
||||
|
||||
if result.type_errors:
|
||||
for error in result.type_errors:
|
||||
lines.append(f"{error.var_name}: {error.message}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
@staticmethod
|
||||
def _color(color_code: str, text: str, use_color: bool) -> str:
|
||||
"""Apply color to text."""
|
||||
if use_color:
|
||||
return f"{color_code}{text}{TextFormatter.RESET}"
|
||||
return text
|
||||
|
||||
|
||||
class JsonFormatter:
|
||||
"""Formatter for JSON output."""
|
||||
|
||||
@staticmethod
|
||||
def format(result: ValidationResult) -> str:
|
||||
"""Format validation result as JSON.
|
||||
|
||||
Args:
|
||||
result: Validation result to format.
|
||||
|
||||
Returns:
|
||||
JSON string.
|
||||
"""
|
||||
return json.dumps(result.to_dict(), indent=2)
|
||||
|
||||
|
||||
def format_result(
|
||||
result: ValidationResult,
|
||||
output_format: str = "text",
|
||||
color: bool = True,
|
||||
ci_mode: bool = False,
|
||||
) -> str:
|
||||
"""Format a validation result.
|
||||
|
||||
Args:
|
||||
result: Validation result to format.
|
||||
output_format: Output format ('text' or 'json').
|
||||
color: Whether to use colors (text format only).
|
||||
ci_mode: CI mode (text format only).
|
||||
|
||||
Returns:
|
||||
Formatted output string.
|
||||
"""
|
||||
if output_format == "json":
|
||||
return JsonFormatter.format(result)
|
||||
return TextFormatter.format(result, color=color, ci_mode=ci_mode)
|
||||
Reference in New Issue
Block a user