Skip to content

Commit 72c799d

Browse files
committed
Rename class to RepositoryGoogleSecretManager
1 parent 2d17af6 commit 72c799d

File tree

3 files changed

+118
-144
lines changed

3 files changed

+118
-144
lines changed

decouple.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,30 @@ def __getitem__(self, key):
188188
return self.data[key]
189189

190190

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

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

234235
self.data[key] = value
235236

236-
def __contains__(self, key):
237-
"""
238-
Check if a key is present in the repository or the environment.
239-
240-
Args:
241-
key (str): The key to check for presence.
242-
243-
Returns:
244-
bool: True if key is in the repository or os.environ, False otherwise.
245-
"""
246-
return key in os.environ or key in self.data
247-
248-
def __getitem__(self, key):
249-
"""
250-
Retrieve the value associated with the given key.
251-
252-
Args:
253-
key (str): The key to retrieve the value for.
254-
255-
Returns:
256-
str: The value associated with the key.
257-
258-
Raises:
259-
KeyError: If the key is not found in the repository.
260-
"""
261-
return self.data[key]
262-
263237

264238
class AutoConfig(object):
265239
"""

tests/test_gsm.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# coding: utf-8
2+
import os
3+
import pytest
4+
from decouple import Config, RepositoryGoogleSecretManager, UndefinedValueError
5+
6+
ENVSTRING = """
7+
KeyTrue=True\nKeyOne=1\nKeyYes=yes
8+
KeyY=y
9+
KeyOn=on
10+
11+
KeyFalse=False
12+
KeyZero=0
13+
KeyNo=no
14+
KeyN=n
15+
KeyOff=off
16+
KeyEmpty=
17+
18+
# CommentedKey=None
19+
KeyWithSpaces = Some Value With Spaces
20+
KeyWithQuotes="Quoted Value"
21+
"""
22+
23+
24+
@pytest.fixture(scope="module")
25+
def config():
26+
return Config(RepositoryGoogleSecretManager(ENVSTRING))
27+
28+
29+
def test_string_comment(config):
30+
with pytest.raises(UndefinedValueError):
31+
config("CommentedKey")
32+
33+
34+
def test_string_bool_true(config):
35+
assert config("KeyTrue", cast=bool)
36+
assert config("KeyOne", cast=bool)
37+
assert config("KeyYes", cast=bool)
38+
assert config("KeyY", cast=bool)
39+
assert config("KeyOn", cast=bool)
40+
41+
42+
def test_string_bool_false(config):
43+
assert not config("KeyFalse", cast=bool)
44+
assert not config("KeyZero", cast=bool)
45+
assert not config("KeyNo", cast=bool)
46+
assert not config("KeyOff", cast=bool)
47+
assert not config("KeyN", cast=bool)
48+
assert not config("KeyEmpty", cast=bool)
49+
50+
51+
def test_string_undefined(config):
52+
with pytest.raises(UndefinedValueError):
53+
config("UndefinedKey")
54+
55+
56+
def test_string_default_none(config):
57+
assert config("UndefinedKey", default=None) is None
58+
59+
60+
def test_string_default_bool(config):
61+
assert not config("UndefinedKey", default=False, cast=bool)
62+
assert config("UndefinedKey", default=True, cast=bool)
63+
64+
65+
def test_string_default(config):
66+
assert not config("UndefinedKey", default=False)
67+
assert config("UndefinedKey", default=True)
68+
69+
70+
def test_string_default_invalid_bool(config):
71+
with pytest.raises(ValueError):
72+
config("UndefinedKey", default="NotBool", cast=bool)
73+
74+
75+
def test_string_empty(config):
76+
assert config("KeyEmpty", default=None) == ""
77+
78+
79+
def test_string_support_space(config):
80+
assert config("KeyWithSpaces") == "Some Value With Spaces"
81+
82+
83+
def test_string_os_environ(config):
84+
os.environ["KeyOverrideByEnv"] = "This"
85+
assert config("KeyOverrideByEnv") == "This"
86+
del os.environ["KeyOverrideByEnv"]
87+
88+
89+
def test_string_undefined_but_present_in_os_environ(config):
90+
os.environ["KeyOnlyEnviron"] = ""
91+
assert config("KeyOnlyEnviron") == ""
92+
del os.environ["KeyOnlyEnviron"]
93+
94+
95+
def test_string_empty_string_means_false(config):
96+
assert not config("KeyEmpty", cast=bool)
97+
98+
99+
def test_string_repo_keyerror(config):
100+
with pytest.raises(KeyError):
101+
config.repository["UndefinedKey"]
102+
103+
104+
def test_string_quoted_value(config):
105+
assert config("KeyWithQuotes") == "Quoted Value"

tests/test_string.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)