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
...
 
@@ -13,24 +13,25 @@
 
#
 
# 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,
 
)
 

	
...
 
@@ -119,24 +120,34 @@ class Config:
 
        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:
setup.py
Show inline comments
...
 
@@ -4,24 +4,25 @@ 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
tests/test_config.py
Show inline comments
...
 
@@ -11,24 +11,26 @@
 
# 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',
...
 
@@ -413,12 +415,28 @@ def test_default_fiscal_year_begin():
 

	
 
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)