Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config and repositories iterable #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def __call__(self, *args, **kwargs):
"""
return self.get(*args, **kwargs)

def __iter__(self):
return iter(self.repository)


class RepositoryEmpty(object):
def __init__(self, source='', encoding=DEFAULT_ENCODING):
Expand All @@ -117,6 +120,9 @@ def __contains__(self, key):
def __getitem__(self, key):
return None

def __iter__(self):
return iter(())


class RepositoryIni(RepositoryEmpty):
"""
Expand All @@ -139,6 +145,9 @@ def __getitem__(self, key):
except NoOptionError:
raise KeyError(key)

def __iter__(self):
return iter(self.parser[self.SECTION])


class RepositoryEnv(RepositoryEmpty):
"""
Expand All @@ -165,6 +174,9 @@ def __contains__(self, key):
def __getitem__(self, key):
return self.data[key]

def __iter__(self):
return iter(self.data)


class RepositorySecret(RepositoryEmpty):
"""
Expand All @@ -187,6 +199,9 @@ def __contains__(self, key):
def __getitem__(self, key):
return self.data[key]

def __iter__(self):
return iter(self.data)


class AutoConfig(object):
"""
Expand Down Expand Up @@ -225,7 +240,9 @@ def _find_file(self, path):
# reached root without finding any files.
return ''

def _load(self, path):
def _load(self, path=None):
path = path or self.search_path or self._caller_path()

# Avoid unintended permission errors
try:
filename = self._find_file(os.path.abspath(path))
Expand All @@ -241,9 +258,15 @@ def _caller_path(self):
path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
return path

def __iter__(self):
if not self.config:
self._load()

return iter(self.config)

def __call__(self, *args, **kwargs):
if not self.config:
self._load(self.search_path or self._caller_path())
self._load()

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

Expand Down