API Reference
This section provides detailed API documentation for ConfigBuddy.
Core Classes
Config
- class configbuddy.core.config.Config(data, source=None)[source]
Bases:
object
Base class for representing configuration file contents.
This class provides functionality to load, save, and visualize configuration data from various file formats including YAML, JSON, and INI.
- __str__()[source]
Convert configuration to a string representation.
- Return type:
- Returns:
String representation of the configuration in a tree structure.
- diff_with(config)[source]
Compare this configuration with another using ConfigDiffer.
A convenience method that uses ConfigDiffer to analyze differences between this configuration and another one.
- Parameters:
config (
Config
) – Another Config object to compare with- Return type:
- Returns:
ConfigDiff object containing the differences between configurations
- classmethod from_file(path)[source]
Create a Config object from a file.
- Parameters:
path (
str
|Path
) – Path to the configuration file. Can be a string or Path object.- Return type:
- Returns:
A new Config instance containing the loaded configuration data.
- Raises:
ValueError – If the file format is not supported.
- merge_with(config, strategy='deep')[source]
Merge this configuration with another using ConfigMerger.
A convenience method that uses ConfigMerger to merge this configuration with another one. Similar to diff_with, but for merging.
- Parameters:
- Returns:
Config: The merged configuration object
list[MergeConflict]: List of merge conflicts detected during merging
- Return type:
A tuple containing
Example
>>> config1 = Config.from_file('base.yaml') >>> config2 = Config.from_file('override.yaml') >>> merged, conflicts = config1.merge_with(config2) >>> if conflicts: ... print("Merge conflicts detected:", conflicts) >>> merged.save('merged.yaml')
ConfigDiff
- class configbuddy.core.config.ConfigDiff(added, removed, modified, unchanged)[source]
Bases:
object
Class representing differences between configuration files.
- added
Dictionary of keys and values that were added in the new config
- removed
Dictionary of keys and values that were removed from the base config
- modified
Dictionary of keys mapping to tuples of (old_value, new_value)
- unchanged
Dictionary of keys and values that remained the same
ConfigMerger
- class configbuddy.core.merger.ConfigMerger[source]
Bases:
object
Class for merging multiple configuration files.
This class provides functionality to merge multiple Config objects, with support for different merge strategies and conflict detection.
The merger supports two strategies: - deep: Recursively merges nested dictionaries - shallow: Only merges top-level keys
When conflicts are detected during merging, they are tracked and returned along with the merged result. A conflict occurs when multiple configs have different values for the same key.
- static merge(configs, strategy='deep')[source]
Merge multiple configuration files.
- Parameters:
- Returns:
Config: The merged configuration object
list[MergeConflict]: List of merge conflicts detected during merging
- Return type:
A tuple containing
- Raises:
ValueError – If no configs provided or if strategy is not ‘deep’ or ‘shallow’
ConfigValidator
- class configbuddy.core.validator.ConfigValidator(schema)[source]
Bases:
object
Class for validating configuration files.
This class provides functionality to validate configuration data against a JSON schema.
- classmethod from_config(config)[source]
Create a validator from an existing configuration.
- Parameters:
config (
Config
) – Configuration object to generate schema from- Return type:
- Returns:
ConfigValidator instance with schema generated from config
- classmethod from_file(schema_path)[source]
Create a validator from a schema file.
- Parameters:
- Return type:
- Returns:
ConfigValidator instance initialized with the schema from file
Visualizer
- class configbuddy.core.visualizer.BaseVisualizer[source]
Bases:
ABC
Base class for configuration visualizers.
This abstract class defines the interface that all visualizer implementations must follow. It provides common functionality for displaying configuration data in different formats.
- class configbuddy.core.visualizer.ConfigData(*args, **kwargs)[source]
Bases:
Protocol
Protocol for configuration data.
This protocol defines the required interface for configuration data objects that can be visualized by the TreeVisualizer.
- data
Dictionary containing the configuration data
- source
Optional path to the source configuration file
- class configbuddy.core.visualizer.DiffVisualizer[source]
Bases:
BaseVisualizer
Visualizer for configuration differences.
This visualizer displays the differences between two configurations, highlighting added, removed, and modified values in different colors.
- to_string(diff)[source]
Convert the configuration differences visualization to a string.
- Parameters:
diff (
ConfigDiff
) – ConfigDiff object containing added, removed, modified, and unchanged items- Return type:
- Returns:
String representation of the configuration differences visualization
- visualize(diff)[source]
Display the configuration differences in the console.
- Parameters:
diff (
ConfigDiff
) – ConfigDiff object containing added, removed, modified, and unchanged items- Return type:
- class configbuddy.core.visualizer.TreeVisualizer[source]
Bases:
BaseVisualizer
Visualizer for configuration data in tree structure.
This visualizer displays configuration data as a colored tree structure, with different colors for each level of nesting and different data types.
- __init__()[source]
Initialize the TreeVisualizer with a predefined set of colors for different tree levels.
- to_string(config)[source]
Convert the configuration tree visualization to a string.
- Parameters:
config (
ConfigData
) – Configuration data object implementing the ConfigData protocol- Return type:
- Returns:
String representation of the configuration tree visualization
- visualize(config)[source]
Display the configuration data as a tree in the console.
- Parameters:
config (
ConfigData
) – Configuration data object implementing the ConfigData protocol- Return type:
Types
- class configbuddy.core.types.ConfigDiff(added, removed, modified, unchanged)[source]
Bases:
object
Class representing differences between configuration files.
- added
Dictionary of keys and values that were added in the new config
- removed
Dictionary of keys and values that were removed from the base config
- modified
Dictionary of keys mapping to tuples of (old_value, new_value)
- unchanged
Dictionary of keys and values that remained the same
- class configbuddy.core.types.MergeConflict(key, values, sources)[source]
Bases:
object
Class representing a merge conflict between configurations.
- key
The configuration key where the conflict occurred
- values
List of conflicting values from different sources
- sources
List of source identifiers where the conflicting values came from