Skip to content

Commit

Permalink
Rename class to RepositoryGoogleSecretManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Dresdn committed Jan 20, 2024
1 parent 2d17af6 commit 8ac622c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 74 deletions.
52 changes: 13 additions & 39 deletions decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,30 @@ def __getitem__(self, key):
return self.data[key]


class RepositoryString(RepositoryEmpty):
class RepositoryGoogleSecretManager(RepositoryEnv):
"""
Repository class to retrieve options from a string.
Repository class for retrieving configuration options from Google Secret Manager.
Parses a string formatted like a `.env` file into a dictionary of options.
This class is an extension of the `RepositoryEmpty` class that provides a
way to read configuration keys from an environment string.
This class extends `RepositoryEnv` to specifically handle configurations stored in
Google Secret Manager. It parses strings formatted in a similar way to `.env` files,
converting them into a dictionary of configuration options.
Attributes:
data (dict): A dictionary to hold the parsed key-value pairs.
data (dict): A dictionary holding the parsed key-value pairs from the Google
Secret Manager source.
"""

def __init__(self, source):
"""
Initializes the RepositoryString with a given string source.
Initialize RepositoryGoogleSecretManager with a Google Secret Manager source.
The provided string should have one "KEY=value" pair per line, similar
to a `.env` file format. Lines starting with `#` are considered as
comments and ignored. Surrounding whitespace is stripped from keys
and values.
The source string is expected to have one "KEY=value" pair per line, akin to
the `.env` file format. Lines beginning with `#` are treated as comments and
are disregarded. Keys and values are trimmed of surrounding whitespace for
accurate parsing.
Args:
source (str): The string source to parse.
source (str): The string source from Google Secret Manager to be parsed.
"""
self.data = {}
source_lines = source.split('\n')
Expand All @@ -233,33 +234,6 @@ def __init__(self, source):

self.data[key] = value

def __contains__(self, key):
"""
Check if a key is present in the repository or the environment.
Args:
key (str): The key to check for presence.
Returns:
bool: True if key is in the repository or os.environ, False otherwise.
"""
return key in os.environ or key in self.data

def __getitem__(self, key):
"""
Retrieve the value associated with the given key.
Args:
key (str): The key to retrieve the value for.
Returns:
str: The value associated with the key.
Raises:
KeyError: If the key is not found in the repository.
"""
return self.data[key]


class AutoConfig(object):
"""
Expand Down
70 changes: 35 additions & 35 deletions tests/test_string.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# coding: utf-8
import os
import pytest
from decouple import Config, RepositoryString, UndefinedValueError
from decouple import Config, RepositoryGoogleSecretManager, UndefinedValueError

ENVSTRING = '''
ENVSTRING = """
KeyTrue=True\nKeyOne=1\nKeyYes=yes
KeyY=y
KeyOn=on
Expand All @@ -18,88 +18,88 @@
# CommentedKey=None
KeyWithSpaces = Some Value With Spaces
KeyWithQuotes="Quoted Value"
'''
"""


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def config():
return Config(RepositoryString(ENVSTRING))
return Config(RepositoryGoogleSecretManager(ENVSTRING))


def test_string_comment(config):
with pytest.raises(UndefinedValueError):
config('CommentedKey')
config("CommentedKey")


def test_string_bool_true(config):
assert config('KeyTrue', cast=bool)
assert config('KeyOne', cast=bool)
assert config('KeyYes', cast=bool)
assert config('KeyY', cast=bool)
assert config('KeyOn', cast=bool)
assert config("KeyTrue", cast=bool)
assert config("KeyOne", cast=bool)
assert config("KeyYes", cast=bool)
assert config("KeyY", cast=bool)
assert config("KeyOn", cast=bool)


def test_string_bool_false(config):
assert not config('KeyFalse', cast=bool)
assert not config('KeyZero', cast=bool)
assert not config('KeyNo', cast=bool)
assert not config('KeyOff', cast=bool)
assert not config('KeyN', cast=bool)
assert not config('KeyEmpty', cast=bool)
assert not config("KeyFalse", cast=bool)
assert not config("KeyZero", cast=bool)
assert not config("KeyNo", cast=bool)
assert not config("KeyOff", cast=bool)
assert not config("KeyN", cast=bool)
assert not config("KeyEmpty", cast=bool)


def test_string_undefined(config):
with pytest.raises(UndefinedValueError):
config('UndefinedKey')
config("UndefinedKey")


def test_string_default_none(config):
assert config('UndefinedKey', default=None) is None
assert config("UndefinedKey", default=None) is None


def test_string_default_bool(config):
assert not config('UndefinedKey', default=False, cast=bool)
assert config('UndefinedKey', default=True, cast=bool)
assert not config("UndefinedKey", default=False, cast=bool)
assert config("UndefinedKey", default=True, cast=bool)


def test_string_default(config):
assert not config('UndefinedKey', default=False)
assert config('UndefinedKey', default=True)
assert not config("UndefinedKey", default=False)
assert config("UndefinedKey", default=True)


def test_string_default_invalid_bool(config):
with pytest.raises(ValueError):
config('UndefinedKey', default='NotBool', cast=bool)
config("UndefinedKey", default="NotBool", cast=bool)


def test_string_empty(config):
assert config('KeyEmpty', default=None) == ''
assert config("KeyEmpty", default=None) == ""


def test_string_support_space(config):
assert config('KeyWithSpaces') == 'Some Value With Spaces'
assert config("KeyWithSpaces") == "Some Value With Spaces"


def test_string_os_environ(config):
os.environ['KeyOverrideByEnv'] = 'This'
assert config('KeyOverrideByEnv') == 'This'
del os.environ['KeyOverrideByEnv']
os.environ["KeyOverrideByEnv"] = "This"
assert config("KeyOverrideByEnv") == "This"
del os.environ["KeyOverrideByEnv"]


def test_string_undefined_but_present_in_os_environ(config):
os.environ['KeyOnlyEnviron'] = ''
assert config('KeyOnlyEnviron') == ''
del os.environ['KeyOnlyEnviron']
os.environ["KeyOnlyEnviron"] = ""
assert config("KeyOnlyEnviron") == ""
del os.environ["KeyOnlyEnviron"]


def test_string_empty_string_means_false(config):
assert not config('KeyEmpty', cast=bool)
assert not config("KeyEmpty", cast=bool)


def test_string_repo_keyerror(config):
with pytest.raises(KeyError):
config.repository['UndefinedKey']
config.repository["UndefinedKey"]


def test_string_quoted_value(config):
assert config('KeyWithQuotes') == 'Quoted Value'
assert config("KeyWithQuotes") == "Quoted Value"

0 comments on commit 8ac622c

Please sign in to comment.