Changeset - 7f45788235cf
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-13 02:27:52
brettcsmith@brettcsmith.org
config: Start configuration file with books path.

Ultimately I would like to make it possible to configure the software
entirely through this file, rather than the hodgepodge system we have
now. But that can come later.
2 files changed with 44 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -5,24 +5,25 @@
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import configparser
 
import decimal
 
import functools
 
import os
 
import urllib.parse as urlparse
 

	
 
import requests.auth
 
import rt
 

	
 
from pathlib import Path
 
from typing import (
 
    NamedTuple,
 
    Optional,
...
 
@@ -65,44 +66,62 @@ class RTCredentials(NamedTuple):
 
        except OSError:
 
            return cls()
 
        else:
 
            return cls(**values)
 

	
 

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

	
 
    def __init__(self) -> None:
 
        self.file_config = configparser.ConfigParser()
 

	
 
    def load_file(self, config_path: Optional[Path]=None) -> None:
 
        if config_path is None:
 
            config_path = self.config_file_path()
 
        with config_path.open() as config_file:
 
            self.file_config.read_file(config_file)
 

	
 
    def _dir_or_none(self, path: Path) -> Optional[Path]:
 
        try:
 
            path.mkdir(exist_ok=True)
 
        except OSError:
 
            return None
 
        else:
 
            return path
 

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

	
 
    def books_path(self) -> Optional[Path]:
 
        try:
 
            retval = Path(self.file_config['Beancount'].get('books dir'))
 
        except (KeyError, ValueError):
 
            ok = False
 
        else:
 
            ok = retval.is_absolute()
 
        return retval if ok else None
 

	
 
    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:
tests/test_config.py
Show inline comments
...
 
@@ -7,24 +7,25 @@
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import contextlib
 
import decimal
 
import operator
 
import os
 
import re
 

	
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import config as config_mod
 

	
 
RT_AUTH_METHODS = frozenset(['basic', 'gssapi', 'rt'])
...
 
@@ -308,12 +309,36 @@ def test_config_file_path(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 = testutil.test_path('userconfig/conftest/config.ini')
 
    config = config_mod.Config()
 
    assert config.config_file_path('conftest') == expected
 

	
 
@pytest.mark.parametrize('path', [
 
    None,
 
    testutil.test_path('userconfig/conservancy_beancount/config.ini'),
 
])
 
def test_load_file(path):
 
    config = config_mod.Config()
 
    config.load_file(path)
 
    assert config.books_path() == Path('/test/conservancy_beancount')
 

	
 
@pytest.mark.parametrize('path_func', [
 
    lambda path: None,
 
    operator.methodcaller('touch', 0o200),
 
])
 
def test_load_file_error(tmp_path, path_func):
 
    config_path = tmp_path / 'nonexistent.ini'
 
    path_func(config_path)
 
    config = config_mod.Config()
 
    with pytest.raises(OSError):
 
        config.load_file(config_path)
 

	
 
def test_no_books_path():
 
    config = config_mod.Config()
 
    assert config.books_path() is None
0 comments (0 inline, 0 general)