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

Integrated version for #142 #155

Closed
wants to merge 5 commits into from
Closed
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
38 changes: 25 additions & 13 deletions decouple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import string
from shlex import shlex
from io import open
from collections import OrderedDict
from collections import ChainMap, OrderedDict

# Useful for very coarse version differentiation.
PYVERSION = sys.version_info
Expand All @@ -18,9 +18,9 @@
text_type = unicode

if PYVERSION >= (3, 2, 0):
read_config = lambda parser, file: parser.read_file(file)
def read_config(parser, file): return parser.read_file(file)
else:
read_config = lambda parser, file: parser.readfp(file)
def read_config(parser, file): return parser.readfp(file)


DEFAULT_ENCODING = 'UTF-8'
Expand All @@ -30,6 +30,7 @@
TRUE_VALUES = {"y", "yes", "t", "true", "on", "1"}
FALSE_VALUES = {"n", "no", "f", "false", "off", "0"}


def strtobool(value):
if isinstance(value, bool):
return value
Expand Down Expand Up @@ -89,7 +90,8 @@ def get(self, option, default=undefined, cast=undefined):
value = self.repository[option]
else:
if isinstance(default, Undefined):
raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
raise UndefinedValueError(
'{} not found. Declare it as envvar or define a default value.'.format(option))

value = default

Expand Down Expand Up @@ -144,6 +146,7 @@ class RepositoryEnv(RepositoryEmpty):
"""
Retrieves option keys from .env files with fall back to os.environ.
"""

def __init__(self, source, encoding=DEFAULT_ENCODING):
self.data = {}

Expand Down Expand Up @@ -201,6 +204,7 @@ class AutoConfig(object):
"""
SUPPORTED = OrderedDict([
('settings.ini', RepositoryIni),
('settings.common.ini', RepositoryIni),
('.env', RepositoryEnv),
])

Expand All @@ -212,10 +216,13 @@ def __init__(self, search_path=None):

def _find_file(self, path):
# look for all files in the current path
filenames = []
for configfile in self.SUPPORTED:
filename = os.path.join(path, configfile)
if os.path.isfile(filename):
return filename
filenames.append(filename)
if filenames:
return filenames

# search the parent
parent = os.path.dirname(path)
Expand All @@ -228,12 +235,16 @@ def _find_file(self, path):
def _load(self, path):
# Avoid unintended permission errors
try:
filename = self._find_file(os.path.abspath(path))
filenames = self._find_file(os.path.abspath(path))
except Exception:
filename = ''
Repository = self.SUPPORTED.get(os.path.basename(filename), RepositoryEmpty)

self.config = Config(Repository(filename, encoding=self.encoding))
filenames = ['']
chain = ChainMap()
for filename in filenames:
Repository = self.SUPPORTED.get(
os.path.basename(filename), RepositoryEmpty)
chain = chain.new_child(Repository(
filename, encoding=self.encoding))
self.config = Config(chain)

def _caller_path(self):
# MAGIC! Get the caller's module path.
Expand All @@ -254,6 +265,7 @@ def __call__(self, *args, **kwargs):

# Helpers


class Csv(object):
"""
Produces a csv parser that return a list of transformed elements.
Expand All @@ -277,7 +289,7 @@ def __call__(self, value):
if value is None:
return self.post_process()

transform = lambda s: self.cast(s.strip(self.strip))
def transform(s): return self.cast(s.strip(self.strip))

splitter = shlex(value, posix=True)
splitter.whitespace = self.delimiter
Expand Down Expand Up @@ -310,7 +322,7 @@ def __call__(self, value):
transform = self.cast(value)
if transform not in self._valid_values:
raise ValueError((
'Value not in list: {!r}; valid values are {!r}'
).format(value, self._valid_values))
'Value not in list: {!r}; valid values are {!r}'
).format(value, self._valid_values))
else:
return transform