diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8f6bd52..3d76251 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,7 @@ unreleased * Removed useless console_script entry point. * Changed loader detection to only consider the final extension in a filename. * Support and test egg specifications with no named entry point (because it's 'main'). +* Support basic interpolation (``here`` and ``__file__``) in the built-in INI loader. 0.2.0 (2015-06-14) ----------------------------------------- diff --git a/src/montague/ini.py b/src/montague/ini.py index d7d7390..d137ddc 100644 --- a/src/montague/ini.py +++ b/src/montague/ini.py @@ -35,6 +35,10 @@ def __init__(self, path): def _read(self): # We need to keep the parser around so the logging conversion can use it. + path_defaults = { + 'here': os.path.dirname(self.path), + '__file__': self.path, + } self._parser = SafeConfigParser() self._parser.read(self.path) self._globals = self._parser.defaults() @@ -45,7 +49,7 @@ def _read(self): if option in self._globals: continue try: - section_data[option] = self._parser.get(section, option) + section_data[option] = self._parser.get(section, option, vars=path_defaults) except InterpolationError: section_data[option] = self._parser.get(section, option, raw=True) return data diff --git a/tests/config_files/simple_config.ini b/tests/config_files/simple_config.ini index 55926fb..6baaa9f 100644 --- a/tests/config_files/simple_config.ini +++ b/tests/config_files/simple_config.ini @@ -3,6 +3,8 @@ foo = bar [application:main] use = package:montague_testapps#basic_app +ini_dir = %(here)s +ini_file = %(__file__)s [application:egg] use = egg:montague_testapps#other diff --git a/tests/test_ini_handling.py b/tests/test_ini_handling.py index 00c433b..ff45a07 100644 --- a/tests/test_ini_handling.py +++ b/tests/test_ini_handling.py @@ -22,7 +22,11 @@ def test_read_config(): 'foo': 'bar', }, 'application': { - 'main': {'use': 'package:montague_testapps#basic_app'}, + 'main': { + 'use': 'package:montague_testapps#basic_app', + 'ini_dir': ini_dir, + 'ini_file': ini_path, + }, 'egg': {'use': 'egg:montague_testapps#other'}, 'filtered-app': { 'filter-with': 'filter',