Skip to content

Commit

Permalink
config loaded.get() now takes a default
Browse files Browse the repository at this point in the history
- use defaults instead of exception_if_missing
- BREAKING CHANGE exception_if_missing no longer available

Signed-off-by: James Nesbitt <[email protected]>
  • Loading branch information
james-nesbitt committed Apr 9, 2021
1 parent f7a63fe commit 77fe768
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion configerus/contrib/get/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ def format(self, key, default_label: str):
label = default_label

loaded = self.config.load(label)
return loaded.get(key, exception_if_missing=True)
return loaded.get(key)
6 changes: 5 additions & 1 deletion configerus/contrib/jsonschema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ def validate(self, validate_target: Any, data):
# returns of any value signal validation as validate only catches exceptions
return

# This case means that we were told to look for a jsonschema source
# in config, which we can find by loading jsonschema as a config label
# and then treating the passed string as a config key for .get()

try:
schema_config = self.config.load(PLUGIN_ID_VALIDATE_JSONSCHEMA_SCHEMA_CONFIG_LABEL)
schema = schema_config.get(validate_key, exception_if_missing=True)
schema = schema_config.get(validate_key)

except Exception as e:
raise NotImplementedError(
Expand Down
4 changes: 4 additions & 0 deletions configerus/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def recursive_format(self, data: Any, default_label: str):
def format_string(self, subject: str, default_label: str):
""" format subject string, looking for and processing any formatting tags in the subject """

# fast stop test. if no start tags are in the string then just abort
if self.START_MATCH not in subject:
return subject

subject_tokenized = subject
for token in [
self.START_MATCH,
Expand Down
25 changes: 16 additions & 9 deletions configerus/loaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def _reload(self, data):
"""
self.data = data

def has(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
exception_if_missing: bool = False, validator: str = ""):
def has(self, key: Any = LOADED_KEY_ROOT):
""" check if a key value exists in the config
using a key, traverse a tree of data and return a boolean for if the key
Expand Down Expand Up @@ -100,13 +99,16 @@ def has(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
Returns:
--------
Boolean : if a value exists in laoded config
Boolean : if a value exists in loaded config
"""
return self.get(key, format=False, validator='', exception_if_missing=False) is not None
try:
tree_get(self.data, key, ignore=['', LOADED_KEY_ROOT])
return True
except KeyError as e:
return False

def get(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
exception_if_missing: bool = False, validator: str = ""):
def get(self, key: Any = LOADED_KEY_ROOT, format: bool = True, validator: str = "", default: Any = None):
""" get a key value from the loaded config
using a key, traverse a tree of data and return the value, optionally
Expand Down Expand Up @@ -161,6 +163,9 @@ def get(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
Validation IS APPLIED if the key could not be matched.
default (Any) : default which will be returned if no matching key is
found. This means that no Exception will be thrown on KeyError
Returns:
(Any) anything in the Dict is a valid return. The return could be a
Expand All @@ -171,7 +176,8 @@ def get(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
Throws:
Can throw a KeyError if the key cannot be found (which also occurs if
all sources produced no data and a non-empty key was passed.)
all sources produced no data and a non-empty key was passed,) ONLY IF
no default value was provided.
"""
value = ""
Expand All @@ -180,12 +186,13 @@ def get(self, key: Any = LOADED_KEY_ROOT, format: bool = True,
value = tree_get(self.data, key, ignore=['', LOADED_KEY_ROOT])

except KeyError as e:
if exception_if_missing:
if default is None:
# hand off the exception
raise e
else:
# Use the default value
logger.debug("Failed to find config key : %s", key)
value = None
value = default

if format and value is not None:
# try to format any values
Expand Down
4 changes: 3 additions & 1 deletion configerus/test/test_2_config_get_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ def test_overrides(self):
""" confirm that keys defined in more than one source get overriden """

self.assertEqual(self.loaded_config.get('4'), "fourth 4")
self.assertIsNone(self.loaded_config.get('5.1', exception_if_missing=False))
self.assertEqual(self.loaded_config.get('5'), "seventh 5 json")

with self.assertRaises(Exception):
self.loaded_config.get('5.1')

def test_get_multiple_keys(self):
""" test the the loaded get of various key formats """

Expand Down
8 changes: 6 additions & 2 deletions configerus/test/test_3_config_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ def test_copy_safety(self):

# check original values
self.assertEqual(config_copy_orig.get('one'), 'orig 1')
self.assertIsNone(config_copy_orig.get('two'))
with self.assertRaises(KeyError):
config_copy_orig.get('two')
# check that copied config didn't modify original
self.assertEqual(config_copy_orig.get('one'), config_copy_late.get('one'))
self.assertEqual(config_copy_orig.get('two'), config_copy_late.get('two'))
with self.assertRaises(KeyError):
config_copy_late.get('two')

self.assertEqual(config1_copy.get('one'), 'copy1 1')
self.assertEqual(config1_copy.get('two'), 'copy1 2')
self.assertEqual(config2_copy.get('one'), 'copy2 1')
self.assertEqual(config2_copy.get('two'), 'copy2 2')
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = configerus
version = 2.0.0
version = 3.0.0
description = Plugin-Based configuration manager
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 77fe768

Please sign in to comment.