Changeset - 18800b249d2c
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-17 18:52:23
brettcsmith@brettcsmith.org
config: Let user specify books dir with ~.
2 files changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/config.py
Show inline comments
...
 
@@ -111,25 +111,25 @@ class Config:
 
            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'].get('books dir'))
 
            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
 

	
 
    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)
 
        )
tests/test_config.py
Show inline comments
...
 
@@ -345,24 +345,29 @@ def test_load_file(path):
 
])
 
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),
0 comments (0 inline, 0 general)