Core Validator Engine¶
The core engine of the Python Code Validator.
This module contains the main orchestrator class, StaticValidator, which is responsible for managing the entire validation lifecycle. It loads the source code and a set of JSON rules, then uses a factory-based component system to execute each rule and report the results.
The core is designed to be decoupled from the specific implementations of rules, selectors, and constraints, allowing for high extensibility.
Example
To run a validation, you would typically use the CLI, but the core can also be used programmatically:
from code_validator import StaticValidator, AppConfig, LogLevel
from code_validator.output import Console, setup_logging
from pathlib import Path
import logging
logger = logging.getLogger(__name__)
console = Console(logger)
config = AppConfig(
solution_path=Path("path/to/solution.py"),
rules_path=Path("path/to/rules.json"),
log_level=LogLevel.INFO,
is_silent=False,
stop_on_first_fail=False
)
validator = StaticValidator(config, console)
is_valid = validator.run()
if is_valid:
print("Validation Passed!")
else:
print(f"Validation Failed. Errors in: {validator.failed_rules_id}")
- class code_validator.core.StaticValidator(config: AppConfig, console: Console)¶
Bases:
objectOrchestrates the static validation process.
This class is the main entry point for running a validation session. It manages loading of source files and rules, parsing the code into an AST, and iterating through the rules to execute them.
- _rule_factory¶
The factory responsible for creating rule objects.
- Type:
- _source_code¶
The raw text content of the Python file being validated.
- Type:
str
- _ast_tree¶
The Abstract Syntax Tree of the source code.
- Type:
ast.Module | None
- property failed_rules_id: list[Rule]¶
A list of rule IDs that failed during the last run.
- Type:
list[int]
- run() bool¶
Runs the entire validation process from start to finish.
This is the main public method of the class. It orchestrates the sequence of loading, parsing, and rule execution.
- Returns:
True if all validation rules passed, False otherwise.
- Return type:
bool
- Raises:
RuleParsingError – Propagated from loading/parsing steps.
FileNotFoundError – Propagated from loading steps.