Skip to content

Commit

Permalink
fix(config): broken list values with 0 or 1 element (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocervell authored Apr 27, 2024
1 parent 4631024 commit 7ef7a5e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions secator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ def get(self, key=None, print=True):
Config.print_yaml(yaml_str)
return value

def set(self, key, value):
def set(self, key, value, set_partial=True):
"""Set a value in the configuration using a dotted path.
Args:
key (str | None): Dotted key path.
value (Any): Value.
set_partial (bool): Set in partial config.
"""
# Get existing value
existing_value = self.get(key, print=False)
Expand Down Expand Up @@ -224,6 +225,10 @@ def set(self, key, value):
value = value[1:-1]
if ',' in value:
value = [c.strip() for c in value.split(',')]
elif value:
value = [value]
else:
value = []
elif isinstance(existing_value, dict):
if isinstance(value, str):
if value.startswith('{') and value.endswith('}'):
Expand All @@ -240,11 +245,14 @@ def set(self, key, value):
value = float(value)
elif isinstance(existing_value, Path):
value = Path(value)
except ValueError:
except ValueError as e:
from secator.utils import debug
debug(f'Could not cast value {value} to expected type {type(existing_value).__name__}: {str(e)}', sub='config')
pass
finally:
target[final_key] = value
partial[final_key] = value
if set_partial:
partial[final_key] = value

def save(self, target_path: Path = None, partial=True):
"""Save config as YAML on disk.
Expand Down Expand Up @@ -450,7 +458,7 @@ def apply_env_overrides(self, print_errors=True):
if key in self._keymap:
path = '.'.join(k.lower() for k in self._keymap[key])
value = os.environ[var]
self.set(path, value)
self.set(path, value, set_partial=False)
if not self.validate(print_errors=False) and print_errors:
console.print(f'[bold red]{var} (override failed)[/]')
elif print_errors:
Expand Down

0 comments on commit 7ef7a5e

Please sign in to comment.