Skip to content

Commit

Permalink
feat: use xcon.conf.Settings.[environment/service] instead of SERVICE…
Browse files Browse the repository at this point in the history
…_NAME on Config object; etc.

Want to use xcon's Settings object as the place to get default/global values.
  • Loading branch information
joshorr committed Feb 18, 2023
1 parent 1816e63 commit 54709ba
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 447 deletions.
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

os.environ['APP_NAME'] = 'testing'
os.environ['APP_ENV'] = 'unit'
11 changes: 6 additions & 5 deletions tests/normal_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import pytest
from xcon import config
from xboto.resource import dynamodb
from xcon.conf import settings

# have a semi-really looking environmental variable to test with.
os.environ["DJANGO_SETTINGS_MODULE"] = "somemodule.some_app.settings.tests"

service_name_at_import_time = str(config.SERVICE_NAME)
app_env_at_import_time = str(config.APP_ENV)
service_name_at_import_time = os.environ['APP_NAME']
app_env_at_import_time = os.environ['APP_ENV']

os.environ['SOME_ENV_VAR'] = 'hello'


def test_ensure_config_is_at_baseline_at_module_import_time():
Expand All @@ -25,7 +26,7 @@ def directory():
an isolated XContext for you as well.
"""
from xcon.directory import Directory
return Directory(service=config.SERVICE_NAME, env=config.APP_ENV)
return Directory(service=settings.service, env=settings.environment)


@pytest.fixture(autouse=True)
Expand Down
181 changes: 84 additions & 97 deletions tests/normal_tests/test_config.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
DynamoProvider,
SsmParamStoreProvider,
SecretsManagerProvider,
DynamoCacher, default_provider_types,
DynamoCacher,
)
from xcon.providers.dynamo import _ConfigDynamoTable

Expand Down
6 changes: 6 additions & 0 deletions xcon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
from .config import ConfigSettings
from .conf import settings

from zsetup import setup

__version__ = '0.1.0'

setup(
app_main_module_file=__file__, app_version=__version__,
app_name='configManagerExperimentalTest'
)
22 changes: 9 additions & 13 deletions xcon/conf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from xsentinels.default import DefaultType, Default
from xsettings import Settings as _Settings, SettingsField
from typing import (
Dict, Union, Iterable, Type, Any, Optional, List, Sequence
)
from .directory import DirectoryListing, DirectoryOrPath, Directory
from typing import Type, Optional, Sequence
from .directory import DirectoryListing, Directory
from . import providers
from .provider import Provider
from .providers import DynamoCacher
from xsettings.env_settings import EnvVarRetriever
import os


_env_retriever = EnvVarRetriever()
Expand All @@ -21,15 +16,15 @@ def __init__(self):
# empty-objs and so on; on a per-instance basis.
self.defaults = DirectoryListing()

service: Optional[str] = SettingsField(
name='APP_NAME', retriever=_env_retriever
service: str = SettingsField(
name='APP_NAME', retriever=_env_retriever, default_value='global'
)
""" Defaults to `APP_NAME` environment variable. """
""" Defaults to `APP_NAME` environment variable; otherwise will fallback to using 'global'. """

environment: Optional[str] = SettingsField(
name='APP_ENV', retriever=_env_retriever
environment: str = SettingsField(
name='APP_ENV', retriever=_env_retriever, default_value='all'
)
""" Defaults to `APP_ENV` environment variable. """
""" Defaults to `APP_ENV` environment variable; otherwise will fallback to using 'all'. """

disable_default_cacher: bool = SettingsField(
name='XCON_DISABLE_DEFAULT_CACHER', retriever=_env_retriever, default_value=False
Expand Down Expand Up @@ -72,6 +67,7 @@ def __init__(self):
(which by default will use `APP_ENV` environmental variable).
"""

# todo: rename without provider or somehwo indicate cacher is not used too?
env_only_provider: bool = SettingsField(
name='XCONF_ENV_ONLY_PROVIDER',
retriever=_env_retriever,
Expand Down
Loading

0 comments on commit 54709ba

Please sign in to comment.