Changeset - f9ea7edae3d7
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-07-29 19:46:14
brettcsmith@brettcsmith.org
config: Add Config.books_repo() method.
3 files changed with 30 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
 
"""User configuration for Conservancy bookkeeping tools"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# 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 datetime
 
import decimal
 
import functools
 
import os
 
import re
 
import urllib.parse as urlparse
 

	
 
import git  # type:ignore[import]
 
import requests.auth
 
import rt
 

	
 
from pathlib import Path
 
from typing import (
 
    Mapping,
 
    NamedTuple,
 
    Optional,
 
    Tuple,
 
    Type,
 
)
 

	
 
from . import books
 
from . import rtutil
 

	
 
class RTCredentials(NamedTuple):
 
    server: Optional[str] = None
 
    user: Optional[str] = None
 
    passwd: Optional[str] = None
 
    auth: Optional[str] = None
 

	
 
    @classmethod
 
    def from_env(cls) -> 'RTCredentials':
 
        values = dict(cls._field_defaults)
 
        for key in values:
 
            env_key = 'RT{}'.format(key.upper())
 
            try:
 
                values[key] = os.environ[env_key]
 
            except KeyError:
 
                pass
 
        return cls(**values)
 

	
 
    @classmethod
 
    def from_rtrc(cls) -> 'RTCredentials':
 
        values = dict(cls._field_defaults)
 
        rtrc_path = Path.home() / '.rtrc'
 
        try:
 
            with rtrc_path.open() as rtrc_file:
 
                for line in rtrc_file:
 
                    try:
 
                        key, value = line.split(None, 1)
 
                    except ValueError:
 
                        pass
 
                    else:
 
                        if key in values:
 
                            values[key] = value.rstrip('\n')
 
        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()
 
        self.file_config.read_string("[Beancount]\n")
 

	
 
    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 load_string(self, config_str: str) -> None:
 
        self.file_config.read_string(config_str)
 

	
 
    def _abspath(self, source: Mapping[str, str], key: str) -> Optional[Path]:
 
        try:
 
            retval = Path(source[key])
 
        except (KeyError, ValueError):
 
            ok = False
 
        else:
 
            if source is not os.environ:
 
                retval = retval.expanduser()
 
            ok = retval.is_absolute()
 
        return retval if ok else None
 

	
 
    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:
 
        retval = self._abspath(os.environ, key)
 
        if retval is None:
 
            retval = default or (Path.home() / self._ENVIRON_DEFAULT_PATHS[key])
 
        return retval
 

	
 
    def books_loader(self) -> Optional[books.Loader]:
 
        books_path = self.books_path()
 
        if books_path is None:
 
            return None
 
        else:
 
            return books.Loader(books_path, self.fiscal_year_begin())
 

	
 
    def books_path(self) -> Optional[Path]:
 
        return self._abspath(self.file_config['Beancount'], 'books dir')
 

	
 
    def books_repo(self) -> Optional[git.Repo]:
 
        """Return a git.Repo object for the books directory
 

	
 
        Returns None if the books directory is not a valid Git repository.
 
        """
 
        try:
 
            return git.Repo(self.file_config['Beancount']['books dir'])
 
        except (KeyError, git.exc.GitError):
 
            return 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 fiscal_year_begin(self) -> books.FiscalYear:
 
        s = self.file_config.get('Beancount', 'fiscal year begin', fallback='3 1')
 
        match = re.match(r'([01]?[0-9])(?:\s*[-./ ]\s*([0-3]?[0-9]))?$', s.strip())
 
        if match is None:
 
            raise ValueError(f"fiscal year begin {s!r} has unknown format")
 
        try:
 
            month = int(match.group(1))
 
            day = int(match.group(2) or 1)
 
            # To check date validity we use an arbitrary year that's
 
            # 1. definitely using the modern calendar
 
            # 2. far enough in the past to not have books (pre-Unix epoch)
 
            # 3. not a leap year
 
            datetime.date(1959, month, day)
 
        except ValueError as e:
 
            raise ValueError(f"fiscal year begin {s!r} is invalid date: {e.args[0]}")
 
        else:
 
            return books.FiscalYear(month, day)
 

	
 
    def payment_threshold(self) -> decimal.Decimal:
 
        try:
 
            return decimal.Decimal(self.file_config['Beancount']['payment threshold'])
 
        except (KeyError, ValueError):
 
            return decimal.Decimal(10)
 

	
 
    def repository_path(self) -> Optional[Path]:
 
        retval = self._abspath(self.file_config['Beancount'], 'repository dir')
 
        if retval is None:
 
            retval = self._abspath(os.environ, 'CONSERVANCY_REPOSITORY')
 
        return retval
 

	
 
    def rt_credentials(self) -> RTCredentials:
 
        all_creds = zip(
 
            RTCredentials.from_env(),
 
            RTCredentials.from_rtrc(),
 
            RTCredentials(auth='rt'),
 
        )
 
        return RTCredentials._make(v0 or v1 or v2 for v0, v1, v2 in all_creds)
 

	
 
    def rt_client(self,
 
                  credentials: RTCredentials=None,
 
                  client: Type[rt.Rt]=rt.Rt,
 
    ) -> Optional[rt.Rt]:
 
        if credentials is None:
 
            credentials = self.rt_credentials()
 
        if credentials.server is None:
 
            return None
 
        urlparts = urlparse.urlparse(credentials.server)
 
        rest_path = urlparts.path.rstrip('/') + '/REST/1.0/'
 
        url = urlparse.urlunparse(urlparts._replace(path=rest_path))
 
        if credentials.auth == 'basic':
 
            auth = requests.auth.HTTPBasicAuth(credentials.user, credentials.passwd)
 
            retval = client(url, http_auth=auth)
 
        else:
 
            retval = client(url, credentials.user, credentials.passwd)
 
        if retval.login():
 
            return retval
 
        else:
 
            return None
 

	
 
    @functools.lru_cache(4)
 
    def _rt_wrapper(self, credentials: RTCredentials, client: Type[rt.Rt]) -> Optional[rtutil.RT]:
 
        wrapper_client = self.rt_client(credentials, client)
 
        if wrapper_client is None:
 
            return None
 
        cache_dir_path = self.cache_dir_path()
 
        if cache_dir_path is None:
 
            cache_db = None
 
        else:
 
            cache_name = '{}@{}.sqlite3'.format(
 
                credentials.user,
 
                urlparse.quote(str(credentials.server), ''),
 
            )
 
            cache_path = cache_dir_path / cache_name
 
            try:
 
                cache_path.touch(0o600)
 
            except OSError:
 
                # RTLinkCache.setup() will handle the problem.
 
                pass
 
            cache_db = rtutil.RTLinkCache.setup(cache_path)
 
        return rtutil.RT(wrapper_client, cache_db)
 

	
 
    def rt_wrapper(self,
 
                  credentials: RTCredentials=None,
 
                  client: Type[rt.Rt]=rt.Rt,
 
    ) -> Optional[rtutil.RT]:
setup.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
from setuptools import setup
 

	
 
setup(
 
    name='conservancy_beancount',
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.6.2',
 
    author='Software Freedom Conservancy',
 
    author_email='info@sfconservancy.org',
 
    license='GNU AGPLv3+',
 

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
 
        'beancount>=2.2',  # Debian:beancount
 
        'GitPython>=2.0',  # Debian:python3-git
 
        # 1.4.1 crashes when trying to save some documents.
 
        'odfpy>=1.4.0,!=1.4.1',  # Debian:python3-odf
 
        'PyYAML>=3.0',  # Debian:python3-yaml
 
        'regex',  # Debian:python3-regex
 
        'rt>=2.0',
 
    ],
 
    setup_requires=[
 
        'pytest-mypy',
 
        'pytest-runner',  # Debian:python3-pytest-runner
 
    ],
 
    tests_require=[
 
        'mypy>=0.770',  # Debian:python3-mypy
 
        'pytest',  # Debian:python3-pytest
 
    ],
 

	
 
    packages=[
 
        'conservancy_beancount',
 
        'conservancy_beancount.plugin',
 
        'conservancy_beancount.reports',
 
    ],
 
    entry_points={
 
        'console_scripts': [
 
            'accrual-report = conservancy_beancount.reports.accrual:entry_point',
 
            'fund-report = conservancy_beancount.reports.fund:entry_point',
 
            'ledger-report = conservancy_beancount.reports.ledger:entry_point',
 
            'opening-balances = conservancy_beancount.tools.opening_balances:entry_point',
 
        ],
 
    },
 
)
tests/test_config.py
Show inline comments
 
"""Test Config class"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# 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 contextlib
 
import decimal
 
import operator
 
import os
 
import re
 

	
 
import git
 

	
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import config as config_mod
 

	
 
RT_AUTH_METHODS = frozenset(['basic', 'gssapi', 'rt'])
 

	
 
RT_ENV_KEYS = (
 
    'RTSERVER',
 
    'RTUSER',
 
    'RTPASSWD',
 
    'RTAUTH',
 
)
 

	
 
RT_ENV_CREDS = (
 
    'https://example.org/envrt',
 
    'envuser',
 
    'env  password',
 
    'gssapi',
 
)
 

	
 
RT_FILE_CREDS = (
 
    'https://example.org/filert',
 
    'fileuser',
 
    'file  password',
 
    'basic',
 
)
 

	
 
RT_GENERIC_CREDS = config_mod.RTCredentials(
 
    'https://example.org/genericrt',
 
    'genericuser',
 
    'generic password',
 
    None,
 
)
 

	
 
@pytest.fixture
 
def rt_environ():
 
    return dict(zip(RT_ENV_KEYS, RT_ENV_CREDS))
 

	
 
def _update_environ(updates):
 
    for key, value in updates.items():
 
        if value is None:
 
            os.environ.pop(key, None)
 
        else:
 
            os.environ[key] = str(value)
 

	
 
@contextlib.contextmanager
 
def update_environ(**kwargs):
 
    revert = {key: os.environ.get(key) for key in kwargs}
 
    _update_environ(kwargs)
 
    try:
 
        yield
 
    finally:
 
        _update_environ(revert)
 

	
 
@contextlib.contextmanager
 
def update_umask(mask):
 
    old_mask = os.umask(mask)
 
    try:
 
        yield old_mask
 
    finally:
 
        os.umask(old_mask)
 

	
 
def test_repository_from_file():
 
    path_s = '/home/good'
 
    with update_environ(CONSERVANCY_REPOSITORY='bad'):
 
        config = config_mod.Config()
 
        config.load_string(f"[Beancount]\nrepository dir = {path_s}\n")
 
        assert config.repository_path() == Path(path_s)
 

	
 
def test_repository_expands_user():
 
    path_s = 'tilderepo'
 
    config = config_mod.Config()
 
    config.load_string(f"[Beancount]\nrepository dir = ~/{path_s}\n")
 
    assert config.repository_path() == Path.home() / path_s
 

	
 
def test_repository_from_environment():
 
    config = config_mod.Config()
 
    assert config.repository_path() == testutil.test_path('repository')
 

	
 
def test_no_repository():
 
    with update_environ(CONSERVANCY_REPOSITORY=None):
 
        config = config_mod.Config()
 
        assert config.repository_path() is None
 

	
 
def test_no_rt_credentials():
 
    with update_environ(HOME=testutil.TESTS_DIR):
 
        config = config_mod.Config()
 
        rt_credentials = config.rt_credentials()
 
    assert rt_credentials.server is None
 
    assert rt_credentials.user is None
 
    assert rt_credentials.passwd is None
 
    assert rt_credentials.auth == 'rt'
...
 
@@ -329,96 +331,112 @@ def test_payment_threshold(config_threshold):
 
])
 
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 = 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
 

	
 
def test_books_path_expands_user():
 
    config = config_mod.Config()
 
    config.load_string('[Beancount]\nbooks dir = ~/userbooks\n')
 
    assert config.books_path() == (Path.home() / 'userbooks')
 

	
 
@pytest.mark.parametrize('value,month,day', [
 
    ('2', 2, 1),
 
    ('3 ', 3, 1),
 
    ('  4', 4, 1),
 
    (' 5 ', 5, 1),
 
    ('6 1', 6, 1),
 
    ('  06  03  ', 6, 3),
 
    ('6-05', 6, 5),
 
    ('06 - 10', 6, 10),
 
    ('6/15', 6, 15),
 
    ('06  /  20', 6, 20),
 
    ('10.25', 10, 25),
 
    (' 10 . 30 ', 10, 30),
 
])
 
def test_fiscal_year_begin(value, month, day):
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\nfiscal year begin = {value}\n')
 
    assert config.fiscal_year_begin() == (month, day)
 

	
 
@pytest.mark.parametrize('value', [
 
    'text',
 
    '1900',
 
    '13',
 
    '010',
 
    '2 30',
 
    '4-31',
 
])
 
def test_bad_fiscal_year_begin(value):
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\nfiscal year begin = {value}\n')
 
    with pytest.raises(ValueError):
 
        config.fiscal_year_begin()
 

	
 
def test_default_fiscal_year_begin():
 
    config = config_mod.Config()
 
    actual = config.fiscal_year_begin()
 
    assert actual.month == 3
 
    assert actual.day == 1
 

	
 
def test_books_loader():
 
    books_path = testutil.test_path('books')
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\nbooks dir = {books_path}\n')
 
    loader = config.books_loader()
 
    entries, errors, _ = loader.load_fy_range(2020, 2020)
 
    assert entries
 
    assert not errors
 

	
 
def test_books_loader_without_books():
 
    assert config_mod.Config().books_loader() is None
 

	
 
def test_books_repo(tmp_path):
 
    repo_path = tmp_path / 'books_repo'
 
    expected = git.Repo.init(repo_path)
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\nbooks dir = {repo_path}')
 
    assert config.books_repo() == expected
 

	
 
def test_books_repo_no_dir():
 
    config = config_mod.Config()
 
    assert config.books_repo() is None
 

	
 
def test_books_dir_not_repo():
 
    config = config_mod.Config()
 
    config.load_string(f'[Beancount]\nbooks dir = {os.devnull}')
 
    assert config.books_repo() is None
0 comments (0 inline, 0 general)