104 lines
2.3 KiB
Python
Raw Normal View History

2025-11-04 05:17:27 +01:00
import configparser
2025-11-04 08:09:12 +01:00
import os
from typing import Any, Dict
2025-11-04 05:17:27 +01:00
from pr.core.logging import get_logger
2025-11-04 08:09:12 +01:00
logger = get_logger("config")
2025-11-04 05:17:27 +01:00
CONFIG_FILE = os.path.expanduser("~/.prrc")
LOCAL_CONFIG_FILE = ".prrc"
def load_config() -> Dict[str, Any]:
2025-11-04 08:09:12 +01:00
config = {"api": {}, "autonomous": {}, "ui": {}, "output": {}, "session": {}}
2025-11-04 05:17:27 +01:00
global_config = _load_config_file(CONFIG_FILE)
local_config = _load_config_file(LOCAL_CONFIG_FILE)
for section in config.keys():
if section in global_config:
config[section].update(global_config[section])
if section in local_config:
config[section].update(local_config[section])
return config
def _load_config_file(filepath: str) -> Dict[str, Dict[str, Any]]:
if not os.path.exists(filepath):
return {}
try:
parser = configparser.ConfigParser()
parser.read(filepath)
config = {}
for section in parser.sections():
config[section] = {}
for key, value in parser.items(section):
config[section][key] = _parse_value(value)
logger.debug(f"Loaded configuration from {filepath}")
return config
except Exception as e:
logger.error(f"Error loading config from {filepath}: {e}")
return {}
def _parse_value(value: str) -> Any:
value = value.strip()
2025-11-04 08:09:12 +01:00
if value.lower() == "true":
2025-11-04 05:17:27 +01:00
return True
2025-11-04 08:09:12 +01:00
if value.lower() == "false":
2025-11-04 05:17:27 +01:00
return False
if value.isdigit():
return int(value)
try:
return float(value)
except ValueError:
pass
return value
def create_default_config(filepath: str = CONFIG_FILE):
default_config = """[api]
default_model = x-ai/grok-code-fast-1
timeout = 30
temperature = 0.7
max_tokens = 8096
[autonomous]
max_iterations = 50
context_threshold = 30
recent_messages_to_keep = 10
[ui]
syntax_highlighting = true
show_timestamps = false
color_output = true
[output]
format = text
verbose = false
quiet = false
[session]
auto_save = false
max_history = 1000
"""
try:
2025-11-04 08:09:12 +01:00
with open(filepath, "w") as f:
2025-11-04 05:17:27 +01:00
f.write(default_config)
logger.info(f"Created default configuration at {filepath}")
return True
except Exception as e:
logger.error(f"Error creating config file: {e}")
return False