Skip to content

Commit

Permalink
Merge pull request #79 from beasteers/filename_defaults
Browse files Browse the repository at this point in the history
added filename default values
  • Loading branch information
sampsyo committed Apr 22, 2020
2 parents 98f8a25 + b632673 commit cf20608
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
29 changes: 21 additions & 8 deletions confuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,15 +1125,21 @@ def value(self, view, template=None):
May raise a `NotFoundError` if the value is missing (and the
template requires it) or a `ConfigValueError` for invalid values.
"""
if view.exists():
try:
value, _ = view.first()
return self.convert(value, view)
elif self.default is REQUIRED:
except NotFoundError:
pass

# get default value, raise if required
return self.get_default_value(view.name)

def get_default_value(self, key_name='default'):
if self.default is REQUIRED:
# Missing required value. This is an error.
raise NotFoundError(u"{0} not found".format(view.name))
else:
# Missing value, but not required.
return self.default
raise NotFoundError(u"{} not found".format(key_name))
# Missing value, but not required.
return self.default

def convert(self, value, view):
"""Convert the YAML-deserialized value to a value of the desired
Expand Down Expand Up @@ -1578,7 +1584,11 @@ def resolve_relative_to(self, view, template):
return view.parent.get(next_template)[self.relative_to]

def value(self, view, template=None):
path, source = view.first()
try:
path, source = view.first()
except NotFoundError:
return self.get_default_value(view.name)

if not isinstance(path, BASESTRING):
self.fail(
u'must be a filename, not {0}'.format(type(path).__name__),
Expand Down Expand Up @@ -1615,8 +1625,11 @@ class Path(Filename):
template.
"""
def value(self, view, template=None):
value = super(Path, self).value(view, template)
if value is None:
return
import pathlib
return pathlib.Path(super(Path, self).value(view, template))
return pathlib.Path(value)


class TypeTemplate(Template):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _read(fn):
long_description=_read("README.rst"),
long_description_content_type='text/x-rst',
install_requires=['pyyaml'],
tests_require=['tox'],
tests_require=['tox', 'pathlib'],
py_modules=['confuse'],
cmdclass={'test': test},
classifiers=[
Expand Down
39 changes: 39 additions & 0 deletions test/test_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ def test_invalid_sequence_type(self):


class FilenameTest(unittest.TestCase):
def test_default_value(self):
config = _root({})
valid = config['foo'].get(confuse.Filename('foo/bar'))
self.assertEqual(valid, 'foo/bar')

def test_default_none(self):
config = _root({})
valid = config['foo'].get(confuse.Filename(None))
self.assertEqual(valid, None)

def test_missing_required_value(self):
config = _root({})
with self.assertRaises(confuse.NotFoundError):
config['foo'].get(confuse.Filename())

def test_filename_relative_to_working_dir(self):
config = _root({'foo': 'bar'})
valid = config['foo'].get(confuse.Filename(cwd='/dev/null'))
Expand Down Expand Up @@ -419,6 +434,30 @@ def test_filename_wrong_type(self):
config['foo'].get(confuse.Filename())


class PathTest(unittest.TestCase):
def test_path_value(self):
import pathlib
config = _root({'foo': 'foo/bar'})
valid = config['foo'].get(confuse.Path())
self.assertEqual(valid, pathlib.Path(os.path.abspath('foo/bar')))

def test_default_value(self):
import pathlib
config = _root({})
valid = config['foo'].get(confuse.Path('foo/bar'))
self.assertEqual(valid, pathlib.Path('foo/bar'))

def test_default_none(self):
config = _root({})
valid = config['foo'].get(confuse.Path(None))
self.assertEqual(valid, None)

def test_missing_required_value(self):
config = _root({})
with self.assertRaises(confuse.NotFoundError):
config['foo'].get(confuse.Path())


class BaseTemplateTest(unittest.TestCase):
def test_base_template_accepts_any_value(self):
config = _root({'foo': 4.2})
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deps =
nose
nose-show-skipped
pyyaml
pathlib


[_flake8]
Expand Down

0 comments on commit cf20608

Please sign in to comment.