-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_loader.py
More file actions
137 lines (109 loc) · 4.98 KB
/
config_loader.py
File metadata and controls
137 lines (109 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Configuration loader for Python Kata-Machine
Loads algorithm configuration from kata.config.py
"""
import os
import sys
import importlib.util
from typing import List, Dict, Any
from pathlib import Path
class ConfigLoader:
"""Loads and validates kata configuration"""
def __init__(self, config_path: str = None):
if config_path is None:
# Find kata-machine root directory (where kata.config.py exists)
kata_root = Path(__file__).parent
while kata_root != kata_root.parent and not (kata_root / "kata.config.py").exists():
kata_root = kata_root.parent
config_path = str(kata_root / "kata.config.py")
self.config_path = Path(config_path)
def load_config(self) -> Dict[str, Any]:
"""Load configuration from kata.config.py"""
if not self.config_path.exists():
raise FileNotFoundError(
f"Configuration file not found: {self.config_path}\n"
f"Please create a kata.config.py file in your project root."
)
# Load the config module
spec = importlib.util.spec_from_file_location("kata_config", self.config_path)
if spec is None or spec.loader is None:
raise ImportError(f"Could not load config from {self.config_path}")
config_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config_module)
self._config_module = config_module
# Extract configuration
config = {
"dsa": self._get_dsa_list(),
"config_path": str(self.config_path),
"available_algorithms": self._get_all_available_algorithms()
}
self._validate_config(config)
return config
def _get_dsa_list(self) -> List[str]:
"""Get the DSA list from the config module"""
if hasattr(self._config_module, 'DSA'):
return self._config_module.DSA
else:
raise AttributeError(
"DSA list not found in kata.config.py\n"
"Please define a DSA list with your selected algorithms."
)
def _get_all_available_algorithms(self) -> List[str]:
"""Get all available algorithm configurations from the module"""
algorithms = set()
for attr_name in dir(self._config_module):
if attr_name.endswith('_DSA') or attr_name == 'DSA':
attr_value = getattr(self._config_module, attr_name)
if isinstance(attr_value, list):
algorithms.update(attr_value)
return sorted(list(algorithms))
def _validate_config(self, config: Dict[str, Any]) -> None:
"""Validate the loaded configuration"""
dsa_list = config["dsa"]
if not isinstance(dsa_list, list):
raise TypeError("DSA must be a list of algorithm names")
if len(dsa_list) == 0:
raise ValueError("DSA list cannot be empty")
# Check for duplicates
if len(dsa_list) != len(set(dsa_list)):
duplicates = [x for x in dsa_list if dsa_list.count(x) > 1]
raise ValueError(f"Duplicate algorithms found in DSA list: {set(duplicates)}")
# Validate algorithm names (basic check)
for algo in dsa_list:
if not isinstance(algo, str):
raise TypeError(f"Algorithm name must be a string, got: {type(algo)}")
if not algo.strip():
raise ValueError("Algorithm name cannot be empty or whitespace")
def get_algorithm_count(self) -> int:
"""Get the number of algorithms configured"""
config = self.load_config()
return len(config["dsa"])
def list_algorithms(self) -> List[str]:
"""Get list of configured algorithms"""
config = self.load_config()
return config["dsa"]
def print_config_summary(self) -> None:
"""Print a summary of the current configuration"""
try:
config = self.load_config()
dsa_list = config["dsa"]
print(f"📋 Kata Configuration Summary")
print(f" Config file: {config['config_path']}")
print(f" Algorithms selected: {len(dsa_list)}")
print(f" Total available: {len(config['available_algorithms'])}")
print()
print("🔧 Selected Algorithms:")
for i, algo in enumerate(dsa_list, 1):
print(f" {i:2d}. {algo}")
print()
except Exception as e:
print(f"❌ Error loading configuration: {e}")
# Convenience function for quick access
def load_kata_config(config_path: str = "kata.config.py") -> Dict[str, Any]:
"""Quick function to load kata configuration"""
loader = ConfigLoader(config_path)
return loader.load_config()
if __name__ == "__main__":
# CLI for testing configuration
loader = ConfigLoader()
loader.print_config_summary()