Add/Update wolf/config.py by Wolf
This commit is contained in:
51
wolf/config.py
Normal file
51
wolf/config.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
DEFAULT_CONFIG_PATH = Path.home() / ".hermes" / "wolf-config.yaml"
|
||||
DEFAULT_LOG_DIR = Path.home() / ".hermes" / "wolf"
|
||||
|
||||
class Config:
|
||||
"""
|
||||
Configuration loader for Wolf.
|
||||
"""
|
||||
def __init__(self, config_path=None):
|
||||
self.config_path = Path(config_path or DEFAULT_CONFIG_PATH)
|
||||
self.data = {}
|
||||
self.load()
|
||||
|
||||
def load(self):
|
||||
if not self.config_path.exists():
|
||||
logging.warning(f"Config file not found: {self.config_path}")
|
||||
return
|
||||
|
||||
try:
|
||||
import yaml
|
||||
with open(self.config_path, 'r') as f:
|
||||
self.data = yaml.safe_load(f)
|
||||
except ImportError:
|
||||
logging.warning("PyYAML not found, using simple line parser for config.")
|
||||
with open(self.config_path, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#') or ':' not in line:
|
||||
continue
|
||||
key, value = line.split(':', 1)
|
||||
self.data[key.strip()] = value.strip()
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self.data.get(key, default)
|
||||
|
||||
def setup_logging(log_dir=None):
|
||||
log_dir = Path(log_dir or DEFAULT_LOG_DIR)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
log_file = log_dir / "wolf.log"
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler(log_file),
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
Reference in New Issue
Block a user