-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configuration logic extracted to simple class. Now it is common for d…
…ata sources and cache classes.
- Loading branch information
Dani Ramirez
committed
Jan 30, 2019
1 parent
edff45b
commit 07054fc
Showing
13 changed files
with
137 additions
and
105 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
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 @@ | ||
|
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,48 @@ | ||
import logging | ||
|
||
from .exceptions import LongitudeConfigError | ||
|
||
|
||
class LongitudeConfigurable: | ||
""" | ||
Any subclass will have a nice get_config(key) method to retrieve configuration values | ||
""" | ||
_default_config = {} | ||
_config = {} | ||
|
||
def __init__(self, config=None): | ||
if config is not None and not isinstance(config, dict): | ||
raise TypeError('Config object must be a dictionary') | ||
|
||
self._config = config or {} | ||
self.logger = logging.getLogger(__class__.__module__) | ||
default_keys = set(self._default_config.keys()) | ||
config_keys = set(config.keys()) if config is not None else set([]) | ||
unexpected_config_keys = list(config_keys.difference(default_keys)) | ||
using_defaults_for = list(default_keys.difference(config_keys)) | ||
|
||
unexpected_config_keys.sort() | ||
using_defaults_for.sort() | ||
|
||
for k in unexpected_config_keys: | ||
self.logger.warning("%s is an unexpected config value" % k) | ||
|
||
for k in using_defaults_for: | ||
self.logger.info("%s key is using default value" % k) | ||
|
||
def get_config(self, key): | ||
""" | ||
Getter for configuration values | ||
:param key: Key in the configuration dictionary | ||
:return: Current value of the chosen key | ||
""" | ||
|
||
if key not in self._default_config.keys(): | ||
raise LongitudeConfigError("%s is not a valid config value. Check your defaults as reference.") | ||
try: | ||
return self._config[key] | ||
except (TypeError, KeyError): | ||
try: | ||
return self._default_config[key] | ||
except KeyError: | ||
return None |
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 @@ | ||
class LongitudeBaseException(Exception): | ||
pass | ||
|
||
|
||
class LongitudeRetriesExceeded(LongitudeBaseException): | ||
pass | ||
|
||
|
||
class LongitudeQueryCannotBeExecutedException(LongitudeBaseException): | ||
pass | ||
|
||
|
||
class LongitudeWrongQueryException(LongitudeBaseException): | ||
pass | ||
|
||
|
||
class LongitudeConfigError(LongitudeBaseException): | ||
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
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
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,34 @@ | ||
from unittest import TestCase | ||
|
||
from src.core.common.exceptions import LongitudeConfigError | ||
from src.core.common.config import LongitudeConfigurable | ||
|
||
|
||
class TestConfig(TestCase): | ||
def test_config(self): | ||
# Config must be a dictionary | ||
with self.assertRaises(TypeError): | ||
LongitudeConfigurable(config=[]) | ||
with self.assertRaises(TypeError): | ||
LongitudeConfigurable(config="") | ||
with self.assertRaises(TypeError): | ||
LongitudeConfigurable(config=0) | ||
|
||
# Any values can go in the configuration dictionary but not expected ones trigger a warning | ||
config = {"some_config_value": 0, "some_another_config_value": "tomato"} | ||
with self.assertLogs(level='WARNING') as log_test: | ||
ds = LongitudeConfigurable(config) | ||
self.assertEqual(log_test.output, | ||
[ | ||
'WARNING:src.core.common.config:some_another_config_value is an unexpected config value', | ||
'WARNING:src.core.common.config:some_config_value is an unexpected config value']) | ||
|
||
# Values in the config can be retrieved using get_config. If no default or config is defined, None is returned. | ||
ds._default_config['some_config_value'] = 42 | ||
ds._default_config['some_none_value'] = None | ||
self.assertEqual(0, ds.get_config('some_config_value')) | ||
self.assertEqual(None, ds.get_config('some_none_value')) | ||
|
||
# We do not allow trying to get a config value out of the default keys | ||
with self.assertRaises(LongitudeConfigError): | ||
self.assertIsNone(ds.get_config('some_random_value_that_does_not_exist_in_config_or_defaults')) |
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