Changeset - 51137815d304
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-21 13:07:14
brettcsmith@brettcsmith.org
config: fiscal_year_begin returns a FiscalYear.
2 files changed with 6 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -24,24 +24,25 @@ import urllib.parse as urlparse
 

	
 
import requests.auth
 
import rt
 

	
 
from pathlib import Path
 
from typing import (
 
    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:
...
 
@@ -121,41 +122,41 @@ class Config:
 

	
 
    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) -> Tuple[int, int]:
 
    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 (month, day)
 
            return books.FiscalYear(month, day)
 

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

	
 
    def repository_path(self) -> Optional[Path]:
 
        try:
 
            return Path(os.environ['CONSERVANCY_REPOSITORY'])
 
        except (KeyError, ValueError):
 
            return None
 

	
 
    def rt_credentials(self) -> RTCredentials:
 
        all_creds = zip(
tests/test_config.py
Show inline comments
...
 
@@ -369,13 +369,15 @@ def test_fiscal_year_begin(value, month, day):
 
    '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()
 
    assert config.fiscal_year_begin() == (3, 1)
 
    actual = config.fiscal_year_begin()
 
    assert actual.month == 3
 
    assert actual.day == 1
0 comments (0 inline, 0 general)