Skip to content

Commit

Permalink
feat(settings): getpath now resolve path from the loaded config path …
Browse files Browse the repository at this point in the history
…if any

Closes #33
  • Loading branch information
YuukanOO committed Sep 18, 2019
1 parent 240c394 commit be10214
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pytlas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self, config=None, additional_lookup=None):
"""
super().__init__('settings', additional_lookup or {})

self._loaded_from_path = None
if config:
self.config = config
else:
Expand All @@ -111,6 +112,7 @@ def load_from_file(self, path):
"""
abspath = os.path.abspath(path)
self._logger.info('Loading configuration from "%s"', abspath)
self._loaded_from_path = abspath
self.config.read(abspath)

def set(self, setting, value, section=DEFAULT_SECTION):
Expand Down Expand Up @@ -219,7 +221,8 @@ def getlist(self, setting, default=[], section=DEFAULT_SECTION): # pylint: disab
return val.split(',') if val else default

def getpath(self, setting, default=None, section=DEFAULT_SECTION):
"""Gets an absolute path for a setting.
"""Gets an absolute path for a setting. If the value is not an absolute
path, it will be resolved based on the loaded config file directory.
It uses the `get` under the hood so the same rules applies.
Expand All @@ -234,7 +237,18 @@ def getpath(self, setting, default=None, section=DEFAULT_SECTION):
"""
val = self.get(setting, default, section=section)

return os.path.abspath(val) if val else None
if not val:
return None

if os.path.isabs(val):
return val

rel_to = os.curdir

if self._loaded_from_path:
rel_to = os.path.dirname(self._loaded_from_path)

return os.path.abspath(os.path.join(rel_to, val))


# Holds the global settings store of the application
Expand Down
18 changes: 18 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def test_it_should_load_settings_from_a_path(self):
s.load_from_file(os.path.join(
os.path.dirname(__file__), '__test.conf'))
expect(s.get('some_key', section='some_section')).to.equal('some_value')
expect(s._loaded_from_path).to.equal(os.path.abspath(os.path.join(
os.path.dirname(__file__), '__test.conf')))

def test_it_should_set_the_setting_even_if_the_section_does_not_exists_yet(self):
s = SettingsStore()
Expand Down Expand Up @@ -146,6 +148,22 @@ def test_it_should_returns_an_absolute_path_when_asked_to(self):
expect(s.getpath('a key', section='paths')).to.equal(
os.path.abspath('var/data'))

def test_it_should_leave_absolute_path_as_it(self):
s = SettingsStore(additional_lookup={
'PATHS_A_KEY': os.path.abspath('skills'),
})
s.load_from_file(os.path.abspath('my_pytlas/pytlas.ini'))
expect(s.getpath('a key', section='paths')).to.equal(
os.path.abspath('skills'))

def test_it_should_compute_a_path_relative_to_the_loaded_config_one(self):
s = SettingsStore(additional_lookup={
'PATHS_A_KEY': '../skills',
})
s.load_from_file(os.path.abspath('my_pytlas/pytlas.ini'))
expect(s.getpath('a key', section='paths')).to.equal(
os.path.abspath('my_pytlas/../skills'))


class TestWriteToStore:

Expand Down

0 comments on commit be10214

Please sign in to comment.