Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config file warnings when file is missing #931

Merged
merged 1 commit into from
Sep 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions optimade/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from fastapi.middleware.cors import CORSMiddleware

with warnings.catch_warnings(record=True) as w:
from optimade.server.config import CONFIG
from optimade.server.config import CONFIG, DEFAULT_CONFIG_FILE_PATH

config_warnings = w

Expand All @@ -32,14 +32,15 @@
)
from optimade.server.routers.utils import BASE_URL_PREFIXES, JSONAPIResponse


if os.getenv("OPTIMADE_CONFIG_FILE") is None:
if config_warnings:
LOGGER.warn(
f"Invalid config file or no config file provided, running server with default settings. Errors: "
f"{[warnings.formatwarning(w.message, w.category, w.filename, w.lineno, '') for w in config_warnings]}"
)
else:
LOGGER.info(f"Loaded settings from {os.getenv('OPTIMADE_CONFIG_FILE')}.")
LOGGER.info(
f"Loaded settings from {os.getenv('OPTIMADE_CONFIG_FILE', DEFAULT_CONFIG_FILE_PATH)}."
)

if CONFIG.debug: # pragma: no cover
LOGGER.info("DEBUG MODE")
Expand Down
9 changes: 6 additions & 3 deletions optimade/server/main_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fastapi.middleware.cors import CORSMiddleware

with warnings.catch_warnings(record=True) as w:
from optimade.server.config import CONFIG
from optimade.server.config import CONFIG, DEFAULT_CONFIG_FILE_PATH

config_warnings = w

Expand All @@ -24,13 +24,16 @@
from optimade.server.routers import index_info, links, versions
from optimade.server.routers.utils import BASE_URL_PREFIXES, JSONAPIResponse

if os.getenv("OPTIMADE_CONFIG_FILE") is None:
if config_warnings:
LOGGER.warn(
f"Invalid config file or no config file provided, running server with default settings. Errors: "
f"{[warnings.formatwarning(w.message, w.category, w.filename, w.lineno, '') for w in config_warnings]}"
)
else:
LOGGER.info(f"Loaded settings from {os.getenv('OPTIMADE_CONFIG_FILE')}.")
LOGGER.info(
f"Loaded settings from {os.getenv('OPTIMADE_CONFIG_FILE', DEFAULT_CONFIG_FILE_PATH)}."
)


if CONFIG.debug: # pragma: no cover
LOGGER.info("DEBUG MODE")
Expand Down