-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
209 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class BaseComponent(object): | ||
|
||
factories = () | ||
services = () | ||
|
||
def __init__(self, namespace=None): | ||
self.namespace = namespace | ||
|
||
def get_factories(self): | ||
return self.factories | ||
|
||
def get_services(self): | ||
return self.services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,4 @@ | ||
import logging | ||
# backward compatibility | ||
from kore.components.base import BaseComponent as BasePluginComponent | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class BasePluginComponent(object): | ||
|
||
factories = () | ||
services = () | ||
|
||
def __init__(self, namespace=None): | ||
self.namespace = namespace | ||
|
||
def get_factories(self): | ||
return self.factories | ||
|
||
def get_services(self): | ||
return self.services | ||
__all__ = ['BasePluginComponent', ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class BaseConfigSection(object): | ||
|
||
@property | ||
def __dict__(self, key): | ||
raise NotImplementedError | ||
|
||
def __iter__(self): | ||
raise NotImplementedError | ||
|
||
def __getitem__(self, key): | ||
raise NotImplementedError | ||
|
||
def keys(self): | ||
raise NotImplementedError | ||
|
||
def get(self, key, default=None): | ||
try: | ||
return self[key] | ||
except KeyError: | ||
return default | ||
|
||
def get_section(self, name): | ||
raise NotImplementedError | ||
|
||
|
||
class BaseConfig(BaseConfigSection): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from collections import defaultdict | ||
|
||
from kore.configs.base import BaseConfig | ||
|
||
|
||
class DictConfig(defaultdict, BaseConfig): | ||
|
||
def __missing__(self, key): | ||
return defaultdict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
|
||
from six import iteritems | ||
from kore.configs.base import BaseConfigSection, BaseConfig | ||
|
||
|
||
class BaseEnv(object): | ||
|
||
prefix = NotImplemented | ||
|
||
def __iter__(self): | ||
for key in self._iter_envvars(): | ||
yield key.replace(self.prefix, "").lower() | ||
|
||
def __getitem__(self, key): | ||
envvar = self._get_envvar(key) | ||
return os.environ[envvar] | ||
|
||
@property | ||
def __dict__(self): | ||
data = {} | ||
for key in self._iter_envvars(): | ||
subkey = key.replace(self.prefix, "").lower() | ||
data[subkey] = os.environ[key] | ||
return data | ||
|
||
def get(self, key, default=None): | ||
envvar = self._get_envvar(key) | ||
try: | ||
return os.environ[envvar] | ||
except KeyError: | ||
return default | ||
|
||
def keys(self): | ||
return list(self) | ||
|
||
def _get_envvar(self, key): | ||
return ''.join([self.prefix, key]).upper() | ||
|
||
def _iter_envvars(self): | ||
for key, value in iteritems(os.environ): | ||
if key.startswith(self.prefix): | ||
yield key | ||
|
||
|
||
class EnvSection(BaseEnv, BaseConfigSection): | ||
|
||
separator = '_' | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
@property | ||
def prefix(self): | ||
return ''.join([self.name, self.separator]) | ||
|
||
def get_section(self, name): | ||
envvar = self._get_envvar(name) | ||
return EnvSection(envvar) | ||
|
||
|
||
class EnvConfig(BaseEnv, BaseConfig): | ||
|
||
def __init__(self, *args, **kwargs): | ||
prefix = kwargs.get('env_prefix', '') | ||
|
||
self.prefix = prefix | ||
|
||
def get_section(self, name): | ||
envvar = self._get_envvar(name) | ||
return EnvSection(envvar) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,4 @@ | ||
import logging | ||
# backeward compativility | ||
from kore.configs.base import BaseConfig as BasePluginConfig | ||
|
||
from kore.configs.models import BaseConfig | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class BasePluginConfig(BaseConfig): | ||
|
||
def __getitem__(self, key): | ||
raise NotImplementedError | ||
|
||
def get(self, key, default=None): | ||
try: | ||
return self[key] | ||
except KeyError: | ||
return default | ||
__all__ = ['BasePluginConfig', ] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from kore import config_factory | ||
|
||
from kore.configs.env import EnvSection | ||
|
||
|
||
class TestEnvConfig(object): | ||
|
||
def test_get_section(self): | ||
config_type = 'env' | ||
config_opt = { | ||
'bar': 'baz', | ||
} | ||
|
||
config = config_factory.create(config_type, **config_opt) | ||
|
||
result = config.get_section('UNDEFINED') | ||
|
||
assert result.__class__ is EnvSection | ||
|
||
def test_get_section_default_value(self): | ||
config_type = 'env' | ||
config_opt = { | ||
'bar': 'baz', | ||
} | ||
|
||
config = config_factory.create(config_type, **config_opt) | ||
|
||
section = config.get_section('UNDEFINED') | ||
|
||
result = section.get('UNDEFINED', 'undefined') | ||
|
||
assert result == 'undefined' | ||
|
||
def test_section_option(self, monkeypatch): | ||
monkeypatch.setenv('TESTING_KEY', 'value') | ||
config_type = 'env' | ||
config_opt = { | ||
'bar': 'baz', | ||
} | ||
config = config_factory.create(config_type, **config_opt) | ||
section = config.get_section('TESTING') | ||
|
||
result = section['key'] | ||
|
||
assert result == 'value' | ||
|
||
def test_section_option_upper(self, monkeypatch): | ||
monkeypatch.setenv('TESTING_KEY', 'value') | ||
config_type = 'env' | ||
config_opt = { | ||
'bar': 'baz', | ||
} | ||
config = config_factory.create(config_type, **config_opt) | ||
section = config.get_section('TESTING') | ||
|
||
result = dict(section) | ||
|
||
assert result == { | ||
'key': 'value', | ||
} | ||
|
||
def test_get_value(self, monkeypatch): | ||
monkeypatch.setenv('TESTING_KEY', 'value') | ||
config_type = 'env' | ||
config_opt = { | ||
'bar': 'baz', | ||
} | ||
config = config_factory.create(config_type, **config_opt) | ||
|
||
result = config.get('key2', 'undefined') | ||
|
||
assert result == 'undefined' |
2 changes: 1 addition & 1 deletion
2
tests/unit/configs/test_models.py → tests/unit/configs/test_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters