Skip to content

Commit

Permalink
Fix config file warnings when specified file is missing (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Sep 1, 2021
1 parent 4adbaa6 commit 79d932a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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

0 comments on commit 79d932a

Please sign in to comment.