/ archive / python / kamaji / config.py
config.py
 1  """
 2  Configuration management for Kamaji.
 3  """
 4  
 5  import os
 6  import json
 7  from pathlib import Path
 8  from typing import Optional
 9  
10  CONFIG_DIR = Path.home() / ".kamaji"
11  CONFIG_FILE = CONFIG_DIR / "config.json"
12  
13  DEFAULT_CONFIG = {
14      "base_url": "http://192.222.50.154:11434",
15      "model": "gpt-oss:120b",
16      "temperature": 0.7,
17      "max_tokens": None,
18  }
19  
20  
21  def ensure_config_dir():
22      """Ensure config directory exists."""
23      CONFIG_DIR.mkdir(exist_ok=True)
24  
25  
26  def load_config() -> dict:
27      """Load configuration from file or create default."""
28      ensure_config_dir()
29  
30      if CONFIG_FILE.exists():
31          with open(CONFIG_FILE, 'r') as f:
32              config = json.load(f)
33              # Merge with defaults in case new keys were added
34              return {**DEFAULT_CONFIG, **config}
35  
36      # Create default config
37      save_config(DEFAULT_CONFIG)
38      return DEFAULT_CONFIG.copy()
39  
40  
41  def save_config(config: dict):
42      """Save configuration to file."""
43      ensure_config_dir()
44      with open(CONFIG_FILE, 'w') as f:
45          json.dump(config, f, indent=2)
46  
47  
48  def get_config_value(key: str) -> Optional[str]:
49      """Get a specific config value."""
50      config = load_config()
51      return config.get(key)
52  
53  
54  def set_config_value(key: str, value: str):
55      """Set a specific config value."""
56      config = load_config()
57      config[key] = value
58      save_config(config)
59  
60  
61  def show_config():
62      """Display current configuration."""
63      config = load_config()
64      print("\nCurrent Kamaji Configuration:")
65      print("=" * 50)
66      for key, value in config.items():
67          print(f"{key:15} = {value}")
68      print("=" * 50)
69      print(f"\nConfig file: {CONFIG_FILE}")