Changeset - b1f82badf770
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-06-16 18:42:09
brettcsmith@brettcsmith.org
config: Get repository directory from config file.

One less thing users have to fiddle with to get set up.
3 files changed with 34 insertions and 20 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -24,12 +24,13 @@ import urllib.parse as urlparse
 

	
 
import requests.auth
 
import rt
 

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

	
...
 
@@ -78,57 +79,57 @@ class Config:
 
        '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:
 
        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 = 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]:
 
        try:
 
            retval = Path(self.file_config['Beancount']['books dir']).expanduser()
 
        except (KeyError, ValueError):
 
            ok = False
 
        else:
 
            ok = retval.is_absolute()
 
        return retval if ok else None
 
        return self._abspath(self.file_config['Beancount'], 'books dir')
 

	
 
    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)
...
 
@@ -160,16 +161,16 @@ class Config:
 
        try:
 
            return decimal.Decimal(self.file_config['Beancount']['payment threshold'])
 
        except (KeyError, ValueError):
 
            return decimal.Decimal(10)
 

	
 
    def repository_path(self) -> Optional[Path]:
 
        try:
 
            return Path(os.environ['CONSERVANCY_REPOSITORY'])
 
        except (KeyError, ValueError):
 
            return None
 
        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'),
setup.py
Show inline comments
...
 
@@ -2,13 +2,13 @@
 

	
 
from setuptools import setup
 

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

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
tests/test_config.py
Show inline comments
...
 
@@ -83,12 +83,25 @@ 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):
0 comments (0 inline, 0 general)