Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
41 changes: 16 additions & 25 deletions hathor/conf/get_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

from __future__ import annotations

import importlib
import os
from typing import TYPE_CHECKING, NamedTuple, Optional

from structlog import get_logger

from hathorlib.conf.utils import load_module_settings, load_yaml_settings

if TYPE_CHECKING:
from hathor.conf.settings import HathorSettings as Settings

Expand All @@ -29,17 +30,17 @@
class _SettingsMetadata(NamedTuple):
source: str
is_yaml: bool
settings: Settings
settings: 'Settings'


_settings_singleton: Optional[_SettingsMetadata] = None


def get_global_settings() -> Settings:
def get_global_settings() -> 'Settings':
return HathorSettings()


def HathorSettings() -> Settings:
def HathorSettings() -> 'Settings':
"""
Returns the configuration named tuple.

Expand Down Expand Up @@ -68,7 +69,7 @@ def get_settings_source() -> str:
return _settings_singleton.source


def _load_settings_singleton(source: str, *, is_yaml: bool) -> Settings:
def _load_settings_singleton(source: str, *, is_yaml: bool) -> 'Settings':
global _settings_singleton

if _settings_singleton is not None:
Expand All @@ -79,29 +80,19 @@ def _load_settings_singleton(source: str, *, is_yaml: bool) -> Settings:

return _settings_singleton.settings

settings_loader = _load_yaml_settings if is_yaml else _load_module_settings
if not is_yaml:
log = logger.new()
log.warn(
"Setting a config module via the 'HATHOR_CONFIG_FILE' env var will be deprecated soon. "
"Use the '--config-yaml' CLI option or the 'HATHOR_CONFIG_YAML' env var to set a yaml filepath instead."
)
from hathor.conf.settings import HathorSettings as Settings

settings_loader = load_yaml_settings if is_yaml else load_module_settings
_settings_singleton = _SettingsMetadata(
source=source,
is_yaml=is_yaml,
settings=settings_loader(source)
settings=settings_loader(Settings, source)
)

return _settings_singleton.settings


def _load_module_settings(module_path: str) -> Settings:
log = logger.new()
log.warn(
"Setting a config module via the 'HATHOR_CONFIG_FILE' env var will be deprecated soon. "
"Use the '--config-yaml' CLI option or the 'HATHOR_CONFIG_YAML' env var to set a yaml filepath instead."
)
settings_module = importlib.import_module(module_path)
settings = getattr(settings_module, 'SETTINGS')
from hathor.conf.settings import HathorSettings as Settings
assert isinstance(settings, Settings)
return settings


def _load_yaml_settings(filepath: str) -> Settings:
from hathor.conf.settings import HathorSettings as Settings
return Settings.from_yaml(filepath=filepath)
Loading
Loading