Skip to content
Merged
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
38 changes: 21 additions & 17 deletions src/paperqa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from pydantic.fields import FieldInfo
from pydantic_settings import BaseSettings, CliSettingsSource, SettingsConfigDict

import paperqa.configs
from paperqa._ldp_shims import (
HAS_LDP_INSTALLED,
Agent,
Expand Down Expand Up @@ -927,24 +928,25 @@ def from_name(
)
return Settings(_cli_settings_source=cli_source(args=True))

# First, try to find the config file in the user's .config directory
user_config_path = pqa_directory("settings") / f"{config_name}.json"

pkg_config_path = (
# Use importlib.resources.files() which is recommended for Python 3.9+
importlib.resources.files(paperqa.configs)
/ f"{config_name}.json"
)
if user_config_path.exists():
# First, try to find the config file in the user's .config directory
json_path = user_config_path

# If not found, fall back to the package's default config
try:
# Use importlib.resources.files() which is recommended for Python 3.9+
pkg_config_path = (
importlib.resources.files("paperqa.configs") / f"{config_name}.json"
)
if pkg_config_path.is_file():
json_path = cast("pathlib.Path", pkg_config_path)
except FileNotFoundError as e:
raise FileNotFoundError(
f"No configuration file found for {config_name}"
) from e
else:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously without this else we would overwrite the user_config_path location -- @mskarlin does this seem like a bug to you?

# If not found, fall back to the package's default config
try:
if pkg_config_path.is_file():
json_path = cast("pathlib.Path", pkg_config_path)
except FileNotFoundError as e:
raise FileNotFoundError(
f"No configuration file {config_name!r} found at user config path"
f" {user_config_path} or bundled config path {pkg_config_path}."
) from e

if json_path:
# we do the ole switcheroo
Expand All @@ -956,8 +958,10 @@ def from_name(
**(tmp.model_dump()),
_cli_settings_source=cli_source(args=True) if cli_source else None,
)

raise FileNotFoundError(f"No configuration file found for {config_name}")
raise FileNotFoundError(
f"No configuration file {config_name!r} found at user config path"
f" {user_config_path} or bundled config path {pkg_config_path}."
)

def get_llm(self) -> LiteLLMModel:
return LiteLLMModel(
Expand Down