Skip to content

Commit e17832e

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

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-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

0 commit comments

Comments
 (0)