Skip to content

Commit dfe71ca

Browse files
committed
Make config and repositories iterable
1 parent 0573e6f commit dfe71ca

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

decouple.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def __call__(self, *args, **kwargs):
106106
"""
107107
return self.get(*args, **kwargs)
108108

109+
def __iter__(self):
110+
return iter(self.repository)
111+
109112

110113
class RepositoryEmpty(object):
111114
def __init__(self, source='', encoding=DEFAULT_ENCODING):
@@ -117,6 +120,9 @@ def __contains__(self, key):
117120
def __getitem__(self, key):
118121
return None
119122

123+
def __iter__(self):
124+
return iter(())
125+
120126

121127
class RepositoryIni(RepositoryEmpty):
122128
"""
@@ -139,6 +145,9 @@ def __getitem__(self, key):
139145
except NoOptionError:
140146
raise KeyError(key)
141147

148+
def __iter__(self):
149+
return iter(self.parser[self.SECTION])
150+
142151

143152
class RepositoryEnv(RepositoryEmpty):
144153
"""
@@ -165,6 +174,9 @@ def __contains__(self, key):
165174
def __getitem__(self, key):
166175
return self.data[key]
167176

177+
def __iter__(self):
178+
return iter(self.data)
179+
168180

169181
class RepositorySecret(RepositoryEmpty):
170182
"""
@@ -187,6 +199,9 @@ def __contains__(self, key):
187199
def __getitem__(self, key):
188200
return self.data[key]
189201

202+
def __iter__(self):
203+
return iter(self.data)
204+
190205

191206
class AutoConfig(object):
192207
"""
@@ -225,7 +240,9 @@ def _find_file(self, path):
225240
# reached root without finding any files.
226241
return ''
227242

228-
def _load(self, path):
243+
def _load(self, path=None):
244+
path = path or self.search_path or self._caller_path()
245+
229246
# Avoid unintended permission errors
230247
try:
231248
filename = self._find_file(os.path.abspath(path))
@@ -241,9 +258,15 @@ def _caller_path(self):
241258
path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
242259
return path
243260

261+
def __iter__(self):
262+
if not self.config:
263+
self._load()
264+
265+
return iter(self.config)
266+
244267
def __call__(self, *args, **kwargs):
245268
if not self.config:
246-
self._load(self.search_path or self._caller_path())
269+
self._load()
247270

248271
return self.config(*args, **kwargs)
249272

tests/test_env.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,37 @@ def test_env_with_quote(config):
140140
def test_env_repo_keyerror(config):
141141
with pytest.raises(KeyError):
142142
config.repository['UndefinedKey']
143+
144+
def test_env_iter(config):
145+
assert [
146+
'KeyTrue',
147+
'KeyOne',
148+
'KeyYes',
149+
'KeyOn',
150+
'KeyY',
151+
'KeyFalse',
152+
'KeyZero',
153+
'KeyNo',
154+
'KeyN',
155+
'KeyOff',
156+
'KeyEmpty',
157+
'PercentNotEscaped',
158+
'NoInterpolation',
159+
'IgnoreSpace',
160+
'RespectSingleQuoteSpace',
161+
'RespectDoubleQuoteSpace',
162+
'KeyOverrideByEnv',
163+
164+
'KeyWithSingleQuoteEnd',
165+
'KeyWithSingleQuoteMid',
166+
'KeyWithSingleQuoteBegin',
167+
'KeyWithDoubleQuoteEnd',
168+
'KeyWithDoubleQuoteMid',
169+
'KeyWithDoubleQuoteBegin',
170+
'KeyIsSingleQuote',
171+
'KeyIsDoubleQuote',
172+
'KeyHasTwoSingleQuote',
173+
'KeyHasTwoDoubleQuote',
174+
'KeyHasMixedQuotesAsData1',
175+
'KeyHasMixedQuotesAsData2',
176+
] == list(config)

0 commit comments

Comments
 (0)