Skip to content

Commit 8ac622c

Browse files
committed
Rename class to RepositoryGoogleSecretManager
1 parent 2d17af6 commit 8ac622c

File tree

2 files changed

+48
-74
lines changed

2 files changed

+48
-74
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_string.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# coding: utf-8
22
import os
33
import pytest
4-
from decouple import Config, RepositoryString, UndefinedValueError
4+
from decouple import Config, RepositoryGoogleSecretManager, UndefinedValueError
55

6-
ENVSTRING = '''
6+
ENVSTRING = """
77
KeyTrue=True\nKeyOne=1\nKeyYes=yes
88
KeyY=y
99
KeyOn=on
@@ -18,88 +18,88 @@
1818
# CommentedKey=None
1919
KeyWithSpaces = Some Value With Spaces
2020
KeyWithQuotes="Quoted Value"
21-
'''
21+
"""
2222

2323

24-
@pytest.fixture(scope='module')
24+
@pytest.fixture(scope="module")
2525
def config():
26-
return Config(RepositoryString(ENVSTRING))
26+
return Config(RepositoryGoogleSecretManager(ENVSTRING))
2727

2828

2929
def test_string_comment(config):
3030
with pytest.raises(UndefinedValueError):
31-
config('CommentedKey')
31+
config("CommentedKey")
3232

3333

3434
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)
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)
4040

4141

4242
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)
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)
4949

5050

5151
def test_string_undefined(config):
5252
with pytest.raises(UndefinedValueError):
53-
config('UndefinedKey')
53+
config("UndefinedKey")
5454

5555

5656
def test_string_default_none(config):
57-
assert config('UndefinedKey', default=None) is None
57+
assert config("UndefinedKey", default=None) is None
5858

5959

6060
def test_string_default_bool(config):
61-
assert not config('UndefinedKey', default=False, cast=bool)
62-
assert config('UndefinedKey', default=True, cast=bool)
61+
assert not config("UndefinedKey", default=False, cast=bool)
62+
assert config("UndefinedKey", default=True, cast=bool)
6363

6464

6565
def test_string_default(config):
66-
assert not config('UndefinedKey', default=False)
67-
assert config('UndefinedKey', default=True)
66+
assert not config("UndefinedKey", default=False)
67+
assert config("UndefinedKey", default=True)
6868

6969

7070
def test_string_default_invalid_bool(config):
7171
with pytest.raises(ValueError):
72-
config('UndefinedKey', default='NotBool', cast=bool)
72+
config("UndefinedKey", default="NotBool", cast=bool)
7373

7474

7575
def test_string_empty(config):
76-
assert config('KeyEmpty', default=None) == ''
76+
assert config("KeyEmpty", default=None) == ""
7777

7878

7979
def test_string_support_space(config):
80-
assert config('KeyWithSpaces') == 'Some Value With Spaces'
80+
assert config("KeyWithSpaces") == "Some Value With Spaces"
8181

8282

8383
def test_string_os_environ(config):
84-
os.environ['KeyOverrideByEnv'] = 'This'
85-
assert config('KeyOverrideByEnv') == 'This'
86-
del os.environ['KeyOverrideByEnv']
84+
os.environ["KeyOverrideByEnv"] = "This"
85+
assert config("KeyOverrideByEnv") == "This"
86+
del os.environ["KeyOverrideByEnv"]
8787

8888

8989
def test_string_undefined_but_present_in_os_environ(config):
90-
os.environ['KeyOnlyEnviron'] = ''
91-
assert config('KeyOnlyEnviron') == ''
92-
del os.environ['KeyOnlyEnviron']
90+
os.environ["KeyOnlyEnviron"] = ""
91+
assert config("KeyOnlyEnviron") == ""
92+
del os.environ["KeyOnlyEnviron"]
9393

9494

9595
def test_string_empty_string_means_false(config):
96-
assert not config('KeyEmpty', cast=bool)
96+
assert not config("KeyEmpty", cast=bool)
9797

9898

9999
def test_string_repo_keyerror(config):
100100
with pytest.raises(KeyError):
101-
config.repository['UndefinedKey']
101+
config.repository["UndefinedKey"]
102102

103103

104104
def test_string_quoted_value(config):
105-
assert config('KeyWithQuotes') == 'Quoted Value'
105+
assert config("KeyWithQuotes") == "Quoted Value"

0 commit comments

Comments
 (0)