Changeset - a156617b4d84
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-10 14:53:39
brettcsmith@brettcsmith.org
config: Add Config.config_file_path() method.
2 files changed with 40 insertions and 4 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -69,6 +69,11 @@ class RTCredentials(NamedTuple):
 

	
 

	
 
class Config:
 
    _ENVIRON_DEFAULT_PATHS = {
 
        'XDG_CACHE_HOME': Path('.cache'),
 
        'XDG_CONFIG_HOME': Path('.config'),
 
    }
 

	
 
    def _dir_or_none(self, path: Path) -> Optional[Path]:
 
        try:
 
            path.mkdir(exist_ok=True)
...
 
@@ -77,21 +82,29 @@ class Config:
 
        else:
 
            return path
 

	
 
    def cache_dir_path(self, name: str='conservancy_beancount') -> Optional[Path]:
 
    def _path_from_environ(self, key: str, default: Optional[Path]=None) -> Path:
 
        try:
 
            cache_root = Path(os.environ['XDG_CACHE_HOME'])
 
            retval = Path(os.environ[key])
 
        except (KeyError, ValueError):
 
            ok = False
 
        else:
 
            # Per the spec, non-absolute paths should be ignored.
 
            ok = cache_root.is_absolute()
 
            ok = retval.is_absolute()
 
        if not ok:
 
            cache_root = Path.home() / '.cache'
 
            retval = default or (Path.home() / self._ENVIRON_DEFAULT_PATHS[key])
 
        return retval
 

	
 
    def cache_dir_path(self, name: str='conservancy_beancount') -> Optional[Path]:
 
        cache_root = self._path_from_environ('XDG_CACHE_HOME')
 
        return (
 
            self._dir_or_none(cache_root)
 
            and self._dir_or_none(cache_root / name)
 
        )
 

	
 
    def config_file_path(self, name: str='conservancy_beancount') -> Path:
 
        config_root = self._path_from_environ('XDG_CONFIG_HOME')
 
        return Path(config_root, name, 'config.ini')
 

	
 
    def payment_threshold(self) -> decimal.Decimal:
 
        return decimal.Decimal(0)
 

	
tests/test_config.py
Show inline comments
...
 
@@ -19,6 +19,8 @@ import decimal
 
import os
 
import re
 

	
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
...
 
@@ -294,3 +296,24 @@ def test_payment_threshold():
 
    threshold = config_mod.Config().payment_threshold()
 
    assert threshold == 0
 
    assert isinstance(threshold, (int, decimal.Decimal))
 

	
 
@pytest.mark.parametrize('config_path', [
 
    None,
 
    '',
 
    'nonexistent/relative/path',
 
])
 
def test_config_file_path(config_path):
 
    expected = Path('~/.config/conservancy_beancount/config.ini').expanduser()
 
    with update_environ(XDG_CONFIG_HOME=config_path):
 
        config = config_mod.Config()
 
        assert config.config_file_path() == expected
 

	
 
def test_config_file_path_respects_xdg_config_home():
 
    with update_environ(XDG_CONFIG_HOME='/etc'):
 
        config = config_mod.Config()
 
        assert config.config_file_path() == Path('/etc/conservancy_beancount/config.ini')
 

	
 
def test_config_file_path_with_subdir():
 
    expected = Path('~/.config/conftest/config.ini').expanduser()
 
    config = config_mod.Config()
 
    assert config.config_file_path('conftest') == expected
0 comments (0 inline, 0 general)