From 7260134ad1c84af5b1236c0087673af82543250f Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 15:13:56 +0000 Subject: [PATCH] Initial upload: EnvSchema v0.1.0 with CI/CD workflow --- envschema/formatters.py | 133 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 envschema/formatters.py diff --git a/envschema/formatters.py b/envschema/formatters.py new file mode 100644 index 0000000..da0cbe7 --- /dev/null +++ b/envschema/formatters.py @@ -0,0 +1,133 @@ +"""Output formatters for validation results.""" + +import json +from typing import Optional + +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) \ No newline at end of file