Configuration Diffο
Advanced configuration diff and drift detection for Confii.
This module provides comprehensive diff functionality and drift detection to compare configurations and detect discrepancies.
- class confii.config_diff.ConfigDiff(key: str, diff_type: DiffType, old_value: Any = None, new_value: Any = None, path: str = '')[source]ο
Bases:
objectRepresents a difference between two configurations.
This class encapsulates a single difference between two configuration dictionaries, including the key, type of change, and both old and new values.
- keyο
The configuration key that differs (e.g., βhostβ)
- diff_typeο
Type of difference (ADDED, REMOVED, MODIFIED, UNCHANGED)
- old_valueο
Original value in the first configuration
- new_valueο
New value in the second configuration
- pathο
Full dot-separated path to the key (e.g., βdatabase.hostβ)
- nested_diffsο
List of nested ConfigDiff objects for complex nested changes
Example
>>> diff = ConfigDiff( ... key="host", ... diff_type=DiffType.MODIFIED, ... old_value="localhost", ... new_value="remote", ... path="database.host", ... ) >>> print(f"{diff.path}: {diff.diff_type.value}") database.host: modified
- __init__(key: str, diff_type: DiffType, old_value: Any = None, new_value: Any = None, path: str = '') None[source]ο
Initialize a configuration diff.
- Parameters:
key β Configuration key that differs
diff_type β Type of difference (ADDED, REMOVED, MODIFIED, UNCHANGED)
old_value β Original value in the first configuration
new_value β New value in the second configuration
path β Full dot-separated path to the key (for nested configurations)
- class confii.config_diff.ConfigDiffer[source]ο
Bases:
objectAdvanced configuration differ with structured diff output.
This class provides utilities for comparing two configurations and generating structured diff results. It supports nested configurations and provides detailed information about all differences.
Example
>>> config1 = {"database": {"host": "localhost", "port": 5432}} >>> config2 = {"database": {"host": "remote", "port": 5432, "ssl": True}} >>> diffs = ConfigDiffer.diff(config1, config2) >>> summary = ConfigDiffer.diff_summary(diffs) >>> print(f"Found {summary['total']} differences")
- static diff(config1: Dict[str, Any], config2: Dict[str, Any], path: str = '') List[ConfigDiff][source]ο
Generate structured diff between two configurations.
- Parameters:
config1 β First configuration dictionary
config2 β Second configuration dictionary
path β Current path prefix (for nested keys)
- Returns:
List of ConfigDiff objects representing all differences
Example
>>> config1 = {"database": {"host": "localhost"}} >>> config2 = {"database": {"host": "remote"}} >>> diffs = ConfigDiffer.diff(config1, config2) >>> diffs[0].key # 'host' >>> diffs[0].diff_type # DiffType.MODIFIED
- class confii.config_diff.ConfigDriftDetector(intended_config: Dict[str, Any])[source]ο
Bases:
objectDetects configuration drift (intended vs. actual state).
Configuration drift occurs when the actual configuration differs from the intended or target configuration. This class helps identify such discrepancies, which is useful for compliance, auditing, and troubleshooting.
- intended_configο
The intended/target configuration state
Example
>>> intended = {"database": {"host": "prod-db.example.com"}} >>> actual = {"database": {"host": "dev-db.example.com"}} >>> detector = ConfigDriftDetector(intended) >>> drift = detector.detect_drift(actual) >>> if drift: ... print(f"Configuration drift detected: {len(drift)} differences") ... for diff in drift: ... print( ... f" {diff.path}: expected {diff.old_value}, got {diff.new_value}" ... )
See also
ConfigDiffer: For comparing two arbitrary configurations
- __init__(intended_config: Dict[str, Any]) None[source]ο
Initialize drift detector.
- Parameters:
intended_config β The intended/target configuration state
- detect_drift(actual_config: Dict[str, Any]) List[ConfigDiff][source]ο
Detect drift between intended and actual configuration.
- Parameters:
actual_config β The actual current configuration
- Returns:
List of ConfigDiff objects representing drift
Example
>>> detector = ConfigDriftDetector(intended_config) >>> drift = detector.detect_drift(actual_config) >>> if drift: ... print(f"Configuration drift detected: {len(drift)} differences")