Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,23 @@ def replacer(match: re.Match[str]) -> str:
return config


DEFAULT_CONFIG_DIR = Path(__file__).parent / "config"


# Load JSON configuration file
def load_json_config(filename):
try:
# If environment variable is set, use the directory specified by it
config_path = DEFAULT_CONFIG_DIR / filename
# A file in DEEPWIKI_CONFIG_DIR overrides the bundled one. Files that are
# not there (e.g. only generator.json is customized) keep the defaults.
if CONFIG_DIR:
config_path = Path(CONFIG_DIR) / filename
else:
# Otherwise use default directory
config_path = Path(__file__).parent / "config" / filename
custom_path = Path(CONFIG_DIR) / filename
if custom_path.exists():
config_path = custom_path
else:
logger.info(
f"{filename} not found in DEEPWIKI_CONFIG_DIR ({CONFIG_DIR}), using the bundled default"
)

logger.info(f"Loading configuration from {config_path}")

Expand Down
27 changes: 27 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,30 @@ def test_iterate_files_included_dirs_ignores_directories_above_root(tmp_path):

files = set(iterate_files(root_dir=str(root), included_dirs=["folder"]))
assert files == {"folder/code.py"}


def test_load_json_config_prefers_file_in_config_dir(monkeypatch, tmp_path):
import json

import api.config as config

(tmp_path / "generator.json").write_text(
json.dumps({"default_provider": "ollama", "providers": {}})
)
monkeypatch.setattr(config, "CONFIG_DIR", str(tmp_path))

assert config.load_json_config("generator.json") == {
"default_provider": "ollama",
"providers": {},
}


def test_load_json_config_falls_back_to_bundled_file(monkeypatch, tmp_path):
import api.config as config

(tmp_path / "generator.json").write_text("{}")
monkeypatch.setattr(config, "CONFIG_DIR", str(tmp_path))

embedder = config.load_json_config("embedder.json")
assert "embedder" in embedder
assert embedder["embedder"]["client_class"] == "OpenAIClient"
Loading