Skip to content

Commit

Permalink
base: convert dash in undescore when using environ
Browse files Browse the repository at this point in the history
When a section title contains a dash, it was impossible to override values using environ because of shell incompatibilities
I would rather replace dash with undescore to avoid this
  • Loading branch information
dzen committed Oct 20, 2017
1 parent 95ce344 commit 1d0ee49
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ ChangeLog
Next version
------------

*Bugfix:*

* Allows to override a configuration containing a dash.


1.7.0 (2017-02-23)
------------------

Expand Down
4 changes: 4 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ The ``ConfigGetter`` class
are looked up under ``<SECTION>_<KEY>`` instead of ``<NAMESPACE>_<SECTION>_<KEY>``; use this setup with
care, since getconf might load variables that weren't intended for this application.

.. warning:: Using dash in section or key would prevent from overriding values using environment variables.
Dash are converted to underscore internally, but if you have the same variable using underscore, it would
override both of them.

.. method:: getstr(key[, default=''])

Retrieve a key from available environments.
Expand Down
2 changes: 1 addition & 1 deletion getconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _env_key(self, key, section=''):
args = (key,)
if self.namespace is not NO_NAMESPACE:
args = (self.namespace,) + args
return '_'.join(arg.upper() for arg in args)
return '_'.join(arg.upper() for arg in args).replace('-', '_')

def _read_env(self, key):
"""Handle environ-related logic."""
Expand Down
1 change: 1 addition & 0 deletions tests/config/example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ noascii = Åuŧølīß

[no-interpolation]
nointerpolation = %(noascii)
with-dashes = no-dash
9 changes: 9 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ def test_environ_settings(self):
self.assertEqual('blah', getter.getstr('foo', 'foo'))
self.assertEqual('', getter.getstr('bar'))

def test_environ_dash_declaration(self):
"""Test when a section declaration contains a dash"""
getter = getconf.ConfigGetter('TESTNS', [self.example_path])
with Environ(TESTNS_NO_INTERPOLATION_NOINTERPOLATION="no-interpolation"):
self.assertEqual('no-interpolation', getter.getstr('no-interpolation.nointerpolation'))

with Environ(TESTNS_NO_INTERPOLATION_WITH_DASHES="value"):
self.assertEqual('value', getter.getstr('no-interpolation.with-dashes'))

def test_environ_section(self):
"""Test fetching section.key from environment."""
getter = getconf.ConfigGetter('TESTNS')
Expand Down

0 comments on commit 1d0ee49

Please sign in to comment.