openalex_mapper / config_loader.py
MaxNoichl's picture
Migrate OpenAlex integration off PyAlex
b38d551
raw
history blame contribute delete
960 Bytes
import json
import os
from functools import lru_cache
from pathlib import Path
DEFAULT_CONFIG_FILES = (
"openalex_config.local.json",
)
@lru_cache(maxsize=1)
def load_local_config():
"""Load local config values without overriding existing environment variables."""
config_file = os.environ.get("OPENALEX_CONFIG_FILE")
candidate_paths = [Path(config_file)] if config_file else [Path(name) for name in DEFAULT_CONFIG_FILES]
loaded = {}
for path in candidate_paths:
if not path.exists():
continue
with path.open("r", encoding="utf-8") as handle:
loaded = json.load(handle)
if not isinstance(loaded, dict):
raise ValueError(f"Config file {path} must contain a JSON object at the top level.")
for key, value in loaded.items():
if key not in os.environ and value is not None:
os.environ[key] = str(value)
break
return loaded