|
import configparser
|
|
import os
|
|
from typing import Any, Dict
|
|
|
|
from pr.core.logging import get_logger
|
|
|
|
logger = get_logger("config")
|
|
|
|
CONFIG_FILE = os.path.expanduser("~/.prrc")
|
|
LOCAL_CONFIG_FILE = ".prrc"
|
|
|
|
|
|
def load_config() -> Dict[str, Any]:
|
|
config = {"api": {}, "autonomous": {}, "ui": {}, "output": {}, "session": {}}
|
|
|
|
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()
|
|
|
|
if value.lower() == "true":
|
|
return True
|
|
if value.lower() == "false":
|
|
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:
|
|
with open(filepath, "w") as f:
|
|
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
|